在docker上启动kafka,是因为最近我们开发的程序需要部署到k8s,在k8s上连接kafka,连接失败,因此我先搭建个环境,以供后续还原问题,解决问题。
文章目录
一、docker上启动kafka
1.下载zookeeper镜像
docker pull wurstmeister/zookeeper
2.下载kafka镜像
docker pull wurstmeister/kafka
3.启动zookeeper
docker run -d --name zookeeper -p 2181:2181-t wurstmeister/zookeeper
4.启动kafka
docker run -d --name kafka --publish 9092:9092--link zookeeper:zookeeper -e KAFKA_BROKER_ID=1-e HOST_IP=192.168.65.7-e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.65.7:9092 -e KAFKA_ADVERTISED_HOST_NAME=192.168.65.7 -e KAFKA_ADVERTISED_PORT=9082 --restart=always -t wurstmeister/kafka
部分参数含义:
KAFKA_BROKER_ID=1 kafka在集群中的唯一标识
KAFKA_ZOOKEEPER_CONNECT=zookeeper:12181 监听zookeeper的地址
KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.65.7:19092 kafka服务器的地址
5.进入kafka容器
1.先查看kafka镜像ID:
docker ps -a
2.进入容器
docker exec -it ${CONTAINER ID} /bin/bash
3.进入kafka的bin目录下
cd /opt/kafka_2.18/bin
4.创建topic
./kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 1 --topic mykafka
5.运行生产者
./kafka-console-producer.sh --broker-list localhost:9092 --topic mykafka
6.消费topic
./kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --from-beginning --topic mykafka
6.问题记录
1.当第一次启动kafka时报以下错误,连接超时的错,我报这错是因为我启动命令错了:
ERROR Fatal error during KafkaServer startup. Prepare to shutdown(kafka.server.KafkaServer)
kafka.zookeeper.ZooKeeperClientTimeoutException: Timed out waiting for connection while in state: CONNECTING At kafka.zookeeper.ZooKeeperClient.waitUntilConnected(ZooKeeperClient.scala:271)
at kafka.zookeeper.ZooKeeperClient.<init>(ZooKeeperClient.scala:125)
at kafka.zk.KafkaZkClient$.apply(KafkaZkClient.scala:1948)
at kafka.server.KafkaServer.createZkClient$1(KafkaServer.scala:431)
at kafka.server.KafkaServer.initZkClient(KafkaServer.scala:456)
at kafka.server.KafkaServer.startup(KafkaServer.scala:191)
at kafka.Kafka$.main(Kafka.scala:109)
at kafka.Kafka.main(Kafka.scala)[2022-01-2007:33:19,615] INFO shutting down(kafka.server.KafkaServer)[2022-01-2007:33:19,645] INFO App info kafka.server for0unregistered(org.apache.kafka.common.utils.AppInfoParser)[2022-01-2007:33:19,646] INFO shut down completed(kafka.server.KafkaServer)[2022-01-2007:33:19,647] ERROR Exiting Kafka.(kafka.Kafka$)[2022-01-2007:33:19,657] INFO shutting down(kafka.server.KafkaServer)
INFO Opening socket connection to server 192.168.65.7/192.168.65.7:2181. Will not attempt to authenticate using SASL(unknown error)(org.apache.zookeeper.ClientCnxn)
报错的启动命令:
docker run -d --name kafka --publish 9092:9092--link zookeeper --env KAFKA_ZOOKEEPER_CONNECT=192.168.65.7:2181--env KAFKA_ADVERTISED_HOST_NAME=192.168.65.7--env KAFKA_ADVERTISED_PORT=9092 wurstmeister/kafka
总结
启动kafka遇到了很多问题,一直以为自己是网络不通,防火墙没开之类的,最后发现一直是自己的运行命令不正确,菜鸟的我,还需继续努力呀。
版权归原作者 YUE_SUNSHINE 所有, 如有侵权,请联系我们删除。