我的 minikube 中的 kafka 和 zookeeper 有以下配置:
apiVersion: v1
kind: Service
metadata:
name: kafka-service
namespace: kafka
spec:
selector:
app: kafka
ports:
- protocol: TCP
port: 9092
name: kafka-port
- protocol: TCP
port: 9094
name: kafka-port-out
- protocol: TCP
port: 2181
name: kafka-zk
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: kafka-deployment
namespace: kafka
spec:
replicas: 1
selector:
matchLabels:
app: kafka
k8s-app: kube-dns
template:
metadata:
labels:
app: kafka
k8s-app: kube-dns
spec:
containers:
- name: kafka-container
image: bitnami/kafka:latest
env:
- name: 'ALLOW_PLAINTEXT_LISTENER'
value: 'yes'
- name: 'KAFKA_CFG_ZOOKEEPER_CONNECT'
value: 'zookeeper-service:2181'
- name: 'KAFKA_CFG_LISTENERS'
value: 'PLAINTEXT://:9092'
- name: 'KAFKA_CFG_ADVERTISED_LISTENERS' # if I comment this and the next line it works only locally
value: 'PLAINTEXT://kafka-service.kafka:9092'
ports:
- containerPort: 9092
name: kafka-port
- containerPort: 9094
name: kafka-port-out
- containerPort: 5555
name: kafka-port-jmx
- containerPort: 2181
name: kafka-zk
这是我的动物园管理员的配置:
apiVersion: v1
kind: Service
metadata:
name: zookeeper-service
namespace: kafka
spec:
selector:
app: zookeeper
ports:
- protocol: TCP
port: 2181
name: zookeeper-port
- protocol: TCP
port: 2888
name: zookeeper-peer
- protocol: TCP
port: 3888
name: leader-election
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: zookeeper-deployment
namespace: kafka
spec:
replicas: 1
selector:
matchLabels:
app: zookeeper
k8s-app: kube-dns
template:
metadata:
labels:
app: zookeeper
k8s-app: kube-dns
spec:
containers:
- name: zookeeper-container
image: bitnami/zookeeper:latest
env:
- name: 'ALLOW_ANONYMOUS_LOGIN'
value: 'yes'
- name: 'ZOOKEEPER_ID'
value: '1'
ports:
- containerPort: 2181
name: zookeeper-port
- containerPort: 2888
name: zookeeper-peer
- containerPort: 3888
name: leader-election
我还有另一个部署:
apiVersion: apps/v1
kind: Deployment
metadata:
name: kafkacat-deployment
# namespace: debug # I was using it from another namespace, but it was not working so I've tried to use the same
namespace: kafka
spec:
replicas: 1
selector:
matchLabels:
app: kafkacat
k8s-app: kube-dns
template:
metadata:
labels:
app: kafkacat
k8s-app: kube-dns
spec:
containers:
- name: kafkacat-container
image: edenhill/kafkacat:1.5.0
然后我尝试远程登录它,它可以工作。
telnet kafka-service.kafka 9092
Trying 10.101.87.127...
Connected to kafka-service.kafka.svc.cluster.local.
Escape character is '^]'.
这是 nslookup
nslookup kafka-service.kafka
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: kafka-service.kafka.svc.cluster.local
Address: 10.101.87.127
但是当我尝试达到它时,这就是我得到的:
kafkacat -b kafka-service.kafka:9092 -L
% ERROR: Failed to acquire metadata: Local: Timed out
所以我猜问题出在 Kafka 配置中:如果我评论 env var KAFKA_CFG_ADVERTISED_LISTENERS
,它的工作方式如下:
# kafkacat -b kafka-service.kafka:9092 -L
Metadata for all topics (from broker -1: kafka-service.kafka:9092/bootstrap):
1 brokers:
broker 1001 at kafka-deployment-858c5c7f98-tt7sr:9092
2 topics:
topic "my_topic" with 1 partitions:
partition 0, leader 1001, replicas: 1001, isrs: 1001
topic "__consumer_offsets" with 50 partitions:
partition 0, leader 1001, replicas: 1001, isrs: 1001
如果我尝试产生一条消息:
kafkacat -b kafka-service.kafka:9092 -P -t my_topic
oi
% ERROR: Local: Host resolution failure: kafka-deployment-858c5c7f98-w2dm5:9092/1001: Failed to resolve 'kafka-deployment-858c5c7f98-w2dm5:9092': Temporary failure in name resolution (after 15382734ms in state INIT)
然后,如果我尝试消费:
kafkacat -b kafka-service.kafka:9092 -C -t my_topic
% ERROR: Local: Host resolution failure: kafka-deployment-858c5c7f98-w2dm5:9092/1001: Failed to resolve 'kafka-deployment-858c5c7f98-w2dm5:9092': Temporary failure in name resolution (after 15406287ms in state INIT)
我尝试配置,KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://kafka-service.kafka.svc.cluster.local:9092
但是当我尝试使用 kafkacat 获取信息时仍然超时。
这就是为什么通告的侦听器是本地机器无法访问的主机名。如何修复集群中的 Kafka 配置?