1、安裝docker
2、拉取kafka和zk的鏡像
docker pull wurstmeister/kafka docker pull zookeeper:3.5.7
3、創建通信網絡
docker network create kafkanet
4、創建kafka和zk容器
4.1 創建zk容器
docker run --net=kafkanet --name yhq_zookeeper1 -p 21810:2181 -d zookeeper:3.5.7
4.2 查看zk的網絡
docker inspect yhq_zookeeper1
4.3 創建kafka容器
docker run --net=kafkanet --name yhq_kafka1 -p 9093:9092 \ --link yhq_zookeeper1 \ -e KAFKA_ZOOKEEPER_CONNECT=172.20.0.2:2181 \ -e KAFKA_ADVERTISED_HOST_NAME=192.168.146.190 \ -e KAFKA_ADVERTISED_PORT=9092 \ -d wurstmeister/kafka
KAFKA_ADVERTISED_HOST_NAME 參數需要設置為宿主機地址192.168.146.190。
KAFKA_ZOOKEEPER_CONNECT 參數設置hbl-zookeeper容器內部地址和端口(同一宿主機內的容器互相訪問要用容器內地址,查看指令為docker inspect hbl_zookeeper,在Networks字段可以看到容器內ip地址)。
4.4 查看通信網絡,zk和kafka容器都加入到網絡中
4.5 電腦連接虛擬機上的docker,添加路由:
route add 172.20.0.0 mask 255.255.0.0 192.168.146.190
5、kafka容器操作
docker exec -it 7cd bash #kafka容器ip以7cd開頭,可以通過docker ps命令查看容器
進入到kafka容器后,查看opt/kafka_2.12-2.4.1/config/server.properties和zookeeper.properties ,可以看到已經配置好zk等相關配置

broker.id=-1 ############################# Zookeeper ############################# # Zookeeper connection string (see zookeeper docs for details). # This is a comma separated host:port pairs, each corresponding to a zk # server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002". # You can also append an optional chroot string to the urls to specify the # root directory for all kafka znodes. zookeeper.connect=172.20.0.2:2181 # Timeout in ms for connecting to zookeeper zookeeper.connection.timeout.ms=6000 ############################# Group Coordinator Settings ############################# expensive, rebalances during application startup. group.initial.rebalance.delay.ms=0 advertised.port=9092 advertised.host.name=192.168.146.190 port=9092

dataDir=/tmp/zookeeper #zookeeper數據目錄 # the port at which the clients will connect clientPort=2181 # disable the per-ip limit on the number of connections since this is a non-production config maxClientCnxns=0 # Disable the adminserver by default to avoid port conflicts. # Set the port to something non-conflicting if choosing to enable this admin.enableServer=false # admin.serverPort=8080 bash-4.4#
kafka 查看當前的topic列表
kafka-topics.sh --list --zookeeper 172.20.0.2:2181
創建一個topic
kafka-topics.sh --create --zookeeper 172.20.0.2:2181 --replication-factor 1 --partitions 1 --topic createtopic1
6、zookeeper容器操作
同樣的方式,進入到zookeeper容器
容器創建時,zookeeper已經啟動成功,我們在kafka執行操縱后,查看kafka在zookeeper中節點信息
zkCli.sh