docker 部署 zookeeper+kafka 集群
主机三台
172.16.100.61
172.16.100.62
172.16.100.63
Docker 版本 当前最新版
# 部署zk有2种方法
## 注意 \后不要跟空格
一 . 端口映射
172.16.100.61 执行
docker run -tid --name=zk-1 \ --restart=always \ --privileged=true \ -p 2888:2888 \ -p 3888:3888 \ -p 2181:2181 \ -e ZOO_MY_ID=1 \ -e ZOO_SERVERS=server.1="0.0.0.0:2888:3888 server.2=172.16.100.62:2888:3888 server.3=172.16.100.63:2888:3888" \ -v /data/kafka_cluster/zookeeper/data:/data \ zookeeper:latest
172.16.100.62 执行
docker run -tid --name=zk-2 \ --restart=always \ --privileged=true \ -p 2888:2888 \ -p 3888:3888 \ -p 2181:2181 \ -e ZOO_MY_ID=2 \ -e ZOO_SERVERS=server.1="172.16.100.61:2888:3888 server.2=0.0.0.0:2888:3888 server.3=172.16.100.63:2888:3888" \ -v /data/kafka_cluster/zookeeper/data:/data \ zookeeper:latest
172.16.100.63 执行
docker run -tid --name=zk-3 \ --restart=always \ --privileged=true \ -p 2888:2888 \ -p 3888:3888 \ -p 2181:2181 \ -e ZOO_MY_ID=3 \ -e ZOO_SERVERS=server.1="172.16.100.61:2888:3888 server.2=172.16.100.62:2888:3888 server.3=0.0.0.0:2888:3888" \ -v /data/kafka_cluster/zookeeper/data:/data \
zookeeper:latest
#查看集群状态
docker exec -it zk-1 bash ./bin/zkServer.sh status
二.直接走宿主机IP
每台机器都执行一次,只需要修改 ZOO_MY_ID 即可 只要ZOO_SERVERS 和你的ID一致即可
docker run -tid --name=zk-3 \ --restart=always \ --privileged=true \ --net=host \ -e ZOO_MY_ID=1 \ -e ZOO_SERVERS=server.1="172.16.100.61:2888:3888 server.2=172.16.100.62:2888:3888 server.3=172.16.100.63:2888:3888" \ -v /data/kafka_cluster/zookeeper/data:/data \ zookeeper:latest
### 上述zk检查过后没有问题就可以进行下一步了 ###
解析主机名
注意: kafka-logs-data-1 是主机的名字 如果你的主机是xxx 那这里应该改为kafka-logs-xxx
同时我映射了本地的hosts到容器里,所有容器获取到的主机名就是宿主机的名字
在61执行
docker run -itd --name=kafka \ --restart=always \ --net=host \ --privileged=true \ -v /etc/hosts:/etc/hosts \ -v /data/kafka_cluster/kafka/data:/kafka/kafka-logs-data-1 \ -v /data/kafka_cluster/kafka/logs:/opt/kafka/logs \ -e KAFKA_ADVERTISED_HOST_NAME=172.16.100.61 \ -e HOST_IP=172.16.100.61 \ -e KAFKA_ADVERTISED_PORT=9092 \ -e KAFKA_ZOOKEEPER_CONNECT=172.16.100.61:2181,172.16.100.62:2181,172.16.100.63:2181 \ -e KAFKA_BROKER_ID=0 \ -e KAFKA_HEAP_OPTS="-Xmx8196M -Xms8196M" \ wurstmeister/kafka:0.10.1.1
在62执行
docker run -itd --name=kafka \ --restart=always \ --net=host \ --privileged=true \ -v /etc/hosts:/etc/hosts \ -v /data/kafka_cluster/kafka/data:/kafka/kafka-logs-data-2 \ -v /data/kafka_cluster/kafka/logs:/opt/kafka/logs \ -e KAFKA_ADVERTISED_HOST_NAME=172.16.100.62 \ -e HOST_IP=172.16.100.62 \ -e KAFKA_ADVERTISED_PORT=9092 \ -e KAFKA_ZOOKEEPER_CONNECT=172.16.100.61:2181,172.16.100.62:2181,172.16.100.63:2181 \ -e KAFKA_BROKER_ID=1 \ -e KAFKA_HEAP_OPTS="-Xmx8196M -Xms8196M" \ wurstmeister/kafka:0.10.1.1
在63执行
docker run -itd --name=kafka \ --restart=always \ --net=host \ --privileged=true \ -v /etc/hosts:/etc/hosts \ -v /data/kafka_cluster/kafka/data:/kafka/kafka-logs-data-3 \ -v /data/kafka_cluster/kafka/logs:/opt/kafka/logs \ -e KAFKA_ADVERTISED_HOST_NAME=172.16.100.63 \ -e HOST_IP=172.16.100.63 \ -e KAFKA_ADVERTISED_PORT=9092 \ -e KAFKA_ZOOKEEPER_CONNECT=172.16.100.61:2181,172.16.100.62:2181,172.16.100.63:2181 \ -e KAFKA_BROKER_ID=2 \ -e KAFKA_HEAP_OPTS="-Xmx8196M -Xms8196M" \ wurstmeister/kafka:0.10.1.1
##创建topic test
#订阅
docker exec -it kafka bash /opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server 172.16.100.61:9092,172.16.100.62:9092,172.16.100.63:9092 --topic test --from-beginning
#发送消息
docker exec -it kafka bash /opt/kafka/bin/kafka-console-producer.sh --broker-list 172.16.100.61:9092,172.16.100.62:9092,172.16.100.63:9092 --topic test