Docker安装kafka

安装

# 拉取镜像
docker pull wurstmeister/zookeeper
docker pull wurstmeister/kafka

# 启动zookeeper容器(Zookeeper用于崩溃检测,实现Topic发现,和维护Topic的生产和消费状态)
docker run -t -d -p 2181:2181 --name zookeeper wurstmeister/zookeeper

# 启动kafka容器
docker run  -d --name kafka -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=192.168.0.253:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.0.253:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -t wurstmeister/kafka

这里面主要设置了4个参数,根据自己的机子Ip进行修改即可

KAFKA_BROKER_ID=0               
KAFKA_ZOOKEEPER_CONNECT={主机IP}:2181
KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://{主机IP}:9092
KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092

中间两个参数的 {主机IP} 改为宿主机器的IP地址(服务器改为服务器外网ip),如果不这么设置,可能会导致在别的机器上访问不到kafka。最后的0.0.0.0:9092 无需修改。

测试

# docker exec -it kafka /bin/bash 
bash-4.4# cd
/opt/kafka/bin
bash-4.4# ls
connect-distributed.sh               kafka-producer-perf-test.sh
connect-mirror-maker.sh              kafka-reassign-partitions.sh
connect-standalone.sh                kafka-replica-verification.sh
kafka-acls.sh                        kafka-run-class.sh
kafka-broker-api-versions.sh         kafka-server-start.sh
kafka-configs.sh                     kafka-server-stop.sh
kafka-console-consumer.sh            kafka-streams-application-reset.sh
kafka-console-producer.sh            kafka-topics.sh
kafka-consumer-groups.sh             kafka-verifiable-consumer.sh
kafka-consumer-perf-test.sh          kafka-verifiable-producer.sh
kafka-delegation-tokens.sh           trogdor.sh
kafka-delete-records.sh              windows
kafka-dump-log.sh                    zookeeper-security-migration.sh
kafka-leader-election.sh             zookeeper-server-start.sh
kafka-log-dirs.sh                    zookeeper-server-stop.sh
kafka-mirror-maker.sh                zookeeper-shell.sh
kafka-preferred-replica-election.sh

# 创建topic
./kafka-topics.sh --zookeeper 192.168.0.253:2181 --create --topic app --partitions 2 --replication-factor 1

# 查看topic
./kafka-topics.sh --zookeeper 192.168.0.253:2181 --list

# 生产消息
bash-4.4# ./kafka-console-producer.sh --broker-list 192.168.0.253:9092 --topic app

# 消费消息
bash-4.4# ./kafka-console-consumer.sh --bootstrap-server 192.168.0.253:9092 --topic app --from-beginning
posted @ 2020-11-05 17:35  哈喽哈喽111111  阅读(353)  评论(0编辑  收藏  举报