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