Kubernetes-Kafka无法写有关主题的消息

时间:2019-01-09 12:55:02

标签: docker kubernetes apache-kafka apache-zookeeper

我正在尝试在kafka主题上写数据,但是遇到一些错误。以下是我的配置和错误详细信息。

Kubernetes服务:

kubectl get services
NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
kafka-service   NodePort    10.105.214.246   <none>        9092:30998/TCP               17m
kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP                      4d
zoo1            ClusterIP   10.101.3.128     <none>        2181/TCP,2888/TCP,3888/TCP   20m

Kubernetes豆荚:

kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
kafka-broker0-69c97b67f-4pmw9             1/1     Running   1          1m
zookeeper-deployment-1-796f9d9bcc-cr756   1/1     Running   0          20m

Kafka Docker进程:

docker ps | grep kafka
f79cd0196083        wurstmeister/kafka@sha256:d04dafd2b308f26dbeed8454f67c321579c2818c1eff5e8f695e14a19b1d599b                         "start-kafka.sh"         About a minute ago   Up About a minute                       k8s_kafka_kafka-broker0-69c97b67f-4pmw9_default_a747d38a-0da6-11e9-bd84-fa163e7d3173_1
75393e9e25c1        k8s.gcr.io/pause-amd64:3.1                                                                                         "/pause"                 About a minute ago   Up About a minute                       k8s_POD_kafka-broker0-69c97b67f-4pmw9_default_a747d38a-0da6-11e9-bd84-fa163e7d3173_0

主题test已在Kafka中成功创建,如下所示:

docker exec k8s_kafka_kafka-broker0-69c97b67f-4pmw9_default_a747d38a-0da6-11e9-bd84-fa163e7d3173_1 /opt/kafka_2.12-2.1.0/bin/kafka-topics.sh --list --zookeeper zoo1:2181
OR
docker exec k8s_kafka_kafka-broker0-69c97b67f-4pmw9_default_a747d38a-0da6-11e9-bd84-fa163e7d3173_1 /opt/kafka_2.12-2.1.0/bin/kafka-topics.sh --list --zookeeper 10.101.3.128:2181

以上命令的输出:

test

由于可以使用该主题在其上写数据,因此我已使用主机IP 10.225.36.98或服务IP 10.105.214.246执行以下命令:

kubectl exec kafka-broker0-69c97b67f-4pmw9 -c kafka -i -t -- 
/opt/kafka_2.12-2.1.0/bin/kafka-console-producer.sh [ --broker-list 
10.225.36.98:30998  --topic test ]
>{"k":"v"}

但是他们都没有为我工作,并抛出以下异常:

[2019-01-01 09:26:52,215] ERROR Error when sending message to topic test with key: null, value: 9 bytes with error: 
(org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)
org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.
>[2019-01-01 09:27:59,513] WARN [Producer clientId=console-producer] 
Connection to node -1 (/10.225.36.98:30998) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)

当尝试使用主机名kafka在代理上写时:

kubectl exec kafka-broker0-69c97b67f-4pmw9 -c kafka -i -t -- /opt/kafka_2.12-2.1.0/bin/kafka-console-producer.sh [ --broker-list kafka:9092  --topic test ]

[2019-01-01 09:34:41,293] WARN Couldn't resolve server kafka:9092 from bootstrap.servers as DNS resolution failed for kafka
(org.apache.kafka.clients.ClientUtils)
org.apache.kafka.common.KafkaException: Failed to construct kafka producer

由于主机和服务IP无法正常工作,我尝试使用Pod IP,但出现 test=LEADER_NOT_AVAILABLE 错误。

kubectl exec kafka-broker0-69c97b67f-4pmw9 -c kafka -i -t -- /opt/kafka_2.12-2.1.0/bin/kafka-console-producer.sh [ --broker-list 172.17.0.7:9092  --topic test ]
>{"k":"v"}

[2019-01-01 09:52:30,733] WARN [Producer clientId=console-producer] Error while fetching metadata with correlation id 1 : {test=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)

搜索Google之后,我找到了获取Zookeeper中可用经纪人列表的命令。所以我试图从容器中运行它并停留在以下错误上:

bash-4.4# ./opt/zookeeper/bin/zkCli.sh -server zoo1:2181 ls /brokers/ids
Connecting to zoo1:2181

Zookeeper的异常:

2019-01-01 09:18:05,215 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2019-01-01 09:18:05,219 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=zookeeper-deployment-1-796f9d9bcc-cr756
2019-01-01 09:18:05,220 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_151
2019-01-01 09:18:05,223 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2019-01-01 09:18:05,223 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-1.8-openjdk/jre
2019-01-01 09:18:05,223 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/opt/zookeeper/bin/../build/classes:/opt/zookeeper/b
in/../build/lib/*.jar:/opt/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/opt/zookeeper/bin/../lib/slf4j-api-
1.6.1.jar:/opt/zookeeper/bin/../lib/netty-3.10.5.Final.jar:/opt/zookeeper/bin/../lib/log4j-
1.2.16.jar:/opt/zookeeper/bin/../lib/jline-0.9.94.jar:/opt/zookeeper/bin/../zookeeper-3.4.10.jar:/opt/zookeeper/bin/../src/java/lib/*.jar:/opt/zookeeper/bin/../conf:
2019-01-01 09:18:05,223 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/lib/jvm/java-1.8-
openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64:/usr/lib/jvm/java-1.8-openjdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2019-01-01 09:18:05,223 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2019-01-01 09:18:05,224 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2019-01-01 09:18:05,224 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2019-01-01 09:18:05,224 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2019-01-01 09:18:05,224 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-693.11.6.el7.x86_64
2019-01-01 09:18:05,224 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2019-01-01 09:18:05,224 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2019-01-01 09:18:05,224 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/
2019-01-01 09:18:05,225 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=zoo1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@25f38edc
2019-01-01 09:18:05,259 [myid:] - INFO  [main-SendThread(zoo1.default.svc.cluster.local:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server zoo1.default.svc.cluster.local/10.101.3.128:2181. Will not attempt to authenticate using SASL (unknown error)
2019-01-01 09:18:35,280 [myid:] - WARN  [main-SendThread(zoo1.default.svc.cluster.local:2181):ClientCnxn$SendThread@1108] - Client session timed out, have not heard from server in 30027ms for sessionid 0x0
2019-01-01 09:18:35,282 [myid:] - INFO  [main-SendThread(zoo1.default.svc.cluster.local:2181):ClientCnxn$SendThread@1156] - Client session timed out, have not heard from server in 30027ms for sessionid 0x0, closing socket connection and attempting reconnect
Exception in thread "main" org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /brokers/ids
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:99)
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
    at org.apache.zookeeper.ZooKeeper.getChildren(ZooKeeper.java:1532)
    at org.apache.zookeeper.ZooKeeper.getChildren(ZooKeeper.java:1560)
    at org.apache.zookeeper.ZooKeeperMain.processZKCmd(ZooKeeperMain.java:731)
    at org.apache.zookeeper.ZooKeeperMain.processCmd(ZooKeeperMain.java:599)
    at org.apache.zookeeper.ZooKeeperMain.run(ZooKeeperMain.java:362)
    at org.apache.zookeeper.ZooKeeperMain.main(ZooKeeperMain.java:290)

我还尝试创建LoadBalancer类型的Kafka服务,但是未将LoadBalancer IP分配给该服务。

解决此问题的参考:

  1. https://rmoff.net/2018/08/02/kafka-listeners-explained/
  2. https://github.com/wurstmeister/kafka-docker/wiki/Connectivity#additional-listener-information
  3. https://github.com/kubernetes/contrib/issues/2891
  4. https://dzone.com/articles/ultimate-guide-to-installing-kafka-docker-on-kuber
  5. https://github.com/wurstmeister/kafka-docker/issues/85

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试以下命令将数据发送到主题:

    docker exec k8s_kafka_kafka-broker0-69c97b67f-4pmw9_default_a747d38a-0da6-11e9-bd84-fa163e7d3173_1 
/opt/kafka_2.12-2.1.0/bin/kafka-console-producer.sh 
--broker-list kafka-service:30998 --topic test