kafka在3.x版本后增加KRaft作为自己的注册中心,可以不依赖外部的zk;这里上一篇已经部署好了zk,kafka依然使用zk作为注册中心。
这里使用kafka是为集成zipkin收发微服务接口链路日志数据,只需要部署1个实列即可够用。
编写脚本yaml
vi kafka.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: kafkanamespace: default
spec:replicas: 1strategy:type: RollingUpdaterollingUpdate:maxSurge: 1maxUnavailable: 0revisionHistoryLimit: 5selector:matchLabels:project: kafkaapp: kafkatemplate:metadata:labels:project: kafkaapp: kafkaspec:containers:- name: kafkaimage: bitnami/kafka:3.4.0imagePullPolicy: IfNotPresentports:- containerPort: 9092name: webprotocol: TCPenv:- name: MY_POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: MY_POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name
# # 已zookeeper方式启动注释开始- name: KAFKA_ENABLE_KRAFTvalue: "no"- name: KAFKA_CFG_ZOOKEEPER_CONNECTvalue: zk-cs:2181/kafka # 这里使用zk的service- name: KAFKA_PORT_NUMBERvalue: "9092"- name: KAFKA_BROKER_IDvalue: "1" - name: KAFKA_CFG_ADVERTISED_LISTENERS# 注意必须是监听 service:端口 否则会成为实列:端口 如:kafka-974d87ccf-gnsg9:9092 导致应用不能通过service找到kafka 提示 unknowhostvalue: "PLAINTEXT://kafka:$(KAFKA_PORT_NUMBER)"- name: KAFKA_CFG_LISTENERSvalue: "PLAINTEXT://:$(KAFKA_PORT_NUMBER)"
# - name: KAFKA_ADVERTISED_LISTENERS
# value: PLAINTEXT://:9092
# - name: KAFKA_LISTENERS
# value: PLAINTEXT://:9092- name: KAFKA_CONFLUENT_TOPIC_REPLICATION_FACTORvalue: "1"# 已zookeeper方式启动注释结束# 默认已KRaft方式启动
# - name: KAFKA_CFG_ADVERTISED_LISTENERS# 需配置hosts域名映射
# value: PLAINTEXT://kafka-server:30092#- name: KAFKA_HEAP_OPTS# value: -Xmx2048m -Xms2048m- name: ALLOW_PLAINTEXT_LISTENERvalue: "yes"
---
apiVersion: v1
kind: Service
metadata:name: kafkanamespace: defaultlabels:app: kafka
spec:type: NodePortselector:project: kafkaapp: kafkaports:- port: 9092targetPort: 9092nodePort: 30092
执行部署
kubectl apply -f kafka.yaml
查看是否注册到ZK中,登录zk的pod
zkCli.sh
查看kafka是否注册成功
ls /
查看到 [kafka,zookeeper],kafka节点已经注册成功
get /kafka