启动一个虚拟机 centos7
配置yum源
安装docker
yum -y install docker
启动 Docker 后台服务
service docker start
执行如下命令,确认docker可用
docker image ls
使用docker命令搜索kafka和zookeeper命令
docker search kafkadocker search zookeeper
下载排名较高的镜像,时间较长
docker pull docker.io/wurstmeister/zookeeperdocker pull wurstmeister/kafka
确认镜像下载成功
[root@master ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/wurstmeister/kafka latest f1905dce9659 7 weeks ago 431 MBdocker.io/wurstmeister/zookeeper latest 3f43f72cb283 17 months ago 510 MB
启动zookeeper
docker run -d --name zookeeper -p 2181:2181 -t docker.io/wurstmeister/zookeeper
启动可能报错:
Error response from daemon: oci runtime error: container_linux.go:235:
解决办法: 将所有rpm更新到最新
yum -y update
更新完后先删掉之前的container
docker container ls -adocker container rm
重新启动zookeeper
查看端口,确认启动成功了
netstat -nlp|grep 2181
启动kafka
docker run -d --name kafka -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=172.28.128.14:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://172.28.128.14:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -t docker.io/wurstmeister/kafka
netstat -nlp|grep 9092
通过如下命令确认zk和kafka是否启动成功了
docker ps -a
进入kafka容器, 629c0f149e27是Kafka容器 ID,每次不一样
docker exec -it 735c810c2d55 /bin/bash
测试功能 --测试完成
创建topic/opt/kafka/bin/kafka-topics.sh --create --zookeeper 172.28.128.14:2181 --replication-factor 1 --partitions 1 --topic function_test消费者/opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server 172.28.128.14:9092 --topic function_test生产者/opt/kafka/bin/kafka-console-producer.sh --broker-list 172.28.128.14:9092 --topic function_test查看topic列表/opt/kafka/bin/kafka-topics.sh --list --zookeeper 172.28.128.14:2181删除topic/opt/kafka/bin/kafka-topics.sh --delete --zookeeper 172.28.128.14:2181 --topic function_test
退出容器
exit
此时完成了单个节点的kafka容器,多个节点的kafka集群容器部署参考下面步骤
分别创建3个Kafka节点,并注册到ZK上:
# kafak0docker run -d --name kafka01 -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=172.28.128.14:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://172.28.128.14:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -h kafka01 -t docker.io/wurstmeister/kafka# kafka1docker run -d --name kafka02 -p 9093:9093 -e KAFKA_BROKER_ID=1 -e KAFKA_ZOOKEEPER_CONNECT=172.28.128.14:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://172.28.128.14:9093 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9093 -h kafka02 -t docker.io/wurstmeister/kafka# kafka2docker run -d --name kafka03 -p 9094:9094 -e KAFKA_BROKER_ID=2 -e KAFKA_ZOOKEEPER_CONNECT=172.28.128.14:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://172.28.128.14:9094 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9094 -h kafka03 -t docker.io/wurstmeister/kafka
执行如下命令。确认容器启动成功
至此通过docker搭建kafka集群成功。