一、集群部署Kafka
Kafka部署在以下节点上
[root@k8s-master nfs-client]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane,master 17d v1.20.0
k8s-node1 Ready <none> 16d v1.20.0
k8s-node2 Ready <none> 16d v1.20.0
二、部署svc
1、部署 Service Headless,用于zookeeper间相互通信
1)、.yaml文件
点击查看代码
apiVersion: v1
kind: Service
metadata:
name: kafka-headless
namespace: middleware
labels:
app: kafka
spec:
type: ClusterIP
clusterIP: None # 创建无头服务,如果需要对外暴露端口可自行创建service
ports:
- name: kafka
port: 9092
targetPort: kafka
selector:
app: kafka
2)、应用.yaml文件
kubectl apply -f kafka-svc-headless.yaml
1、部署 Service,用于外部访问 Zookeeper
1)、.yaml文件
点击查看代码
[root@k8s-master svc]# vim kafka-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: kafka-svc
namespace: middleware
labels:
app: kafka
spec:
type: ClusterIP
ports:
- name: kafka
port: 9092
targetPort: kafka
selector:
app: kafka
2)、应用.yaml文件
kubectl apply -f kafka-svc.yaml
三、部署StatefulSet
1、.yaml 文件
点击查看代码
[root@k8s-master conf]# cat kafka.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: kafka-test
namespace: middleware
labels:
app: kafka
spec:
selector:
matchLabels:
app: kafka
serviceName: kafka-headless
podManagementPolicy: "Parallel"
replicas: 3
updateStrategy:
type: "RollingUpdate"
template:
metadata:
name: "kafka"
labels:
app: kafka
spec:
securityContext:
fsGroup: 1001
runAsUser: 1001
affinity:
podAntiAffinity: # Pod反亲和性
preferredDuringSchedulingIgnoredDuringExecution: # 软策略,使Pod分布在不同的节点上
- weight: 1 # 权重,有多个策略通过权重控制调度
podAffinityTerm:
topologyKey: app.kubernetes.io/name # 通过app.kubernetes.io/name作为域调度
labelSelector:
matchExpressions:
- key: app.kubernetes.io/component
operator: In
values:
- zookeeper
containers:
- name: kafka
image: "docker.io/bitnami/kafka:2.3.0-debian-9-r4"
imagePullPolicy: "IfNotPresent"
env:
- name: MY_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: KAFKA_CFG_ZOOKEEPER_CONNECT
value: "zk-headless" # Zookeeper Service 名称
- name: KAFKA_PORT_NUMBER
value: "9092"
- name: KAFKA_CFG_LISTENERS
value: "PLAINTEXT://:$(KAFKA_PORT_NUMBER)"
- name: KAFKA_CFG_ADVERTISED_LISTENERS
value: 'PLAINTEXT://$(MY_POD_NAME).kafka-headless:$(KAFKA_PORT_NUMBER)'
- name: ALLOW_PLAINTEXT_LISTENER
value: "yes"
- name: KAFKA_HEAP_OPTS
value: "-Xmx512m -Xms512m"
- name: KAFKA_CFG_LOGS_DIRS
value: /opt/bitnami/kafka/data
- name: JMX_PORT
value: "9988"
ports:
- name: kafka
containerPort: 9092
livenessProbe:
tcpSocket:
port: kafka
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 2
readinessProbe:
tcpSocket:
port: kafka
initialDelaySeconds: 5
periodSeconds: 10
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 6
volumeMounts:
- name: data
mountPath: /bitnami/kafka
volumeClaimTemplates:
- metadata:
name: data
spec:
storageClassName: kafka-nfs-storage # 指定为上面创建的 storageclass
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: 1Gi
2)、应用.yaml文件
kubectl apply -f kafka.yaml
四、部署kafka-manager
1、.yaml 文件
点击查看代码
[root@k8s-master conf]# cat kafka-manager.yaml
apiVersion: v1
kind: Service
metadata:
name: kafka-manager
namespace: middleware
labels:
app: kafka-manager
spec:
type: NodePort
ports:
- name: kafka
port: 9000
targetPort: 9000
nodePort: 30900
selector:
app: kafka-manager
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: kafka-manager
namespace: middleware
labels:
app: kafka-manager
spec:
replicas: 1
selector:
matchLabels:
app: kafka-manager
template:
metadata:
labels:
app: kafka-manager
spec:
containers:
- name: kafka-manager
image: zenko/kafka-manager:1.3.3.22
imagePullPolicy: IfNotPresent
ports:
- name: kafka-manager
containerPort: 9000
protocol: TCP
env:
- name: ZK_HOSTS
value: "zk-service:2181"
livenessProbe:
httpGet:
path: /api/health
port: kafka-manager
readinessProbe:
httpGet:
path: /api/health
port: kafka-manager
2)、应用.yaml文件
kubectl apply -f kafka-manager.yaml
参考文献:
https://www.cnblogs.com/hsyw/p/16753968.html
https://www.cnblogs.com/shareHistory/p/15843422.html
https://www.jianshu.com/p/50a9d38ada27