docker pull zookeeper
docker run --privileged=true -d --name zookeeper --publish 2181:2181 -d zookeeper:latest
docker run --privileged=true -d --name zookeeper2 --publish 2182:2181 -d zookeeper:latest
docker run --privileged=true -d --name zookeeper3 --publish 2183:2181 -d zookeeper:latest
容器id(已自己的为准) | name | 虚拟机端口 | 容器端口 | 容器ip | myid | 职责(看启动顺序,详情见zookeeper集群leader选取机制) | |
7c............. | zookeeper | 2181 | 2181 | 172.17.0.2 | 1 | leader | |
ee................ | zookeeper2 | 2182 | 2181 | 172.17.0.3 | 2 | follower | |
ce.................... | zookeeper3 | 2183 | 2181 | 172.17.0.4 | 3 | follower |
在zoo.cfg文件尾部添加以下配置
server.1=172.17.0.2:2888:3888;2181
server.2=172.17.0.3:2888:3888;2181
server.3=172.17.0.4:2888:3888;2181
[root@localhost ~]# docker inspect --format='{{.NetworkSettings.IPAddress}}' zookeeper
172.17.0.2
[root@localhost ~]# docker inspect --format='{{.NetworkSettings.IPAddress}}' zookeeper2
172.17.0.3
[root@localhost ~]# docker inspect --format='{{.NetworkSettings.IPAddress}}' zookeeper3
172.17.0.4
[root@localhost ~]#
考虑到每次启动都需要执行N次启动命令,比较麻烦,这里直接写两个简单的启动&关闭脚本节省时间吧,脚本如下。
1.批量启动脚本zookeeperbatchstart.sh
echo "=================================================="
docker restart 7c
echo "==================zookeeper1 start================"
docker restart ee
echo "==================zookeeper2 start================"
docker restart ce
echo "==================zookeeper3 start================"
2.批量关闭脚本zookeeperbatchstop.sh
echo "=================================================="
docker stop 7c
echo "==================zookeeper1 stop ================"
docker stop ee
echo "==================zookeeper2 stop ================"
docker stop ce
echo "==================zookeeper3 stop ================"