1. 集群规划
网络创建
sudo docker network create --driver bridge --subnet=172.18.0.0/16 --gateway=172.18.0.1 zknet
sudo docker network ls
sudo docker network inspect zknet
2. 获取镜像
# 查看可用的稳定版本
sudo docker search zookeeper
sudo docker pull zookeeper:3.4.10
sudo docker image ls |grep -E 'zookeeper'
3. 准备工作
# 存储卷目录创建 mkdir -p /usr/local/zkCluster/node1 mkdir -p /usr/local/zkCluster/node2 mkdir -p /usr/local/zkCluster/node3
4. 启动容器
创建容器 ```bash sudo docker run -d -p 2181:2181 —name zookeeper_node1 —privileged —restart always \ —network zknet —ip 172.18.0.101 \ -v /usr/local/zkCluster/node1/volumes/data:/data \ -v /usr/local/zkCluster/node1/volumes/datalog:/datalog \ -v /usr/local/zkCluster/node1/volumes/logs:/logs \ -e ZOO_MY_ID=1 \ -e “ZOO_SERVERS=server.1=172.18.0.101:2888:3888 server.2=172.18.0.102:2888:3888 server.3=172.18.0.103:2888:3888” \ zookeeper:3.4.10
sudo docker run -d -p 2182:2181 —name zookeeper_node2 —privileged —restart always \ —network zknet —ip 172.18.0.102 \ -v /usr/local/zkCluster/node2/volumes/data:/data \ -v /usr/local/zkCluster/node2/volumes/datalog:/datalog \ -v /usr/local/zkCluster/node2/volumes/logs:/logs \ -e ZOO_MY_ID=2 \ -e “ZOO_SERVERS=server.1=172.18.0.101:2888:3888 server.2=172.18.0.102:2888:3888 server.3=172.18.0.103:2888:3888” \ zookeeper:3.4.10
sudo docker run -d -p 2183:2181 —name zookeeper_node3 —privileged —restart always \ —network zknet —ip 172.18.0.103 \ -v /usr/local/zkCluster/node3/volumes/data:/data \ -v /usr/local/zkCluster/node3/volumes/datalog:/datalog \ -v /usr/local/zkCluster/node3/volumes/logs:/logs \ -e ZOO_MY_ID=3 \ -e “ZOO_SERVERS=server.1=172.18.0.101:2888:3888 server.2=172.18.0.102:2888:3888 server.3=172.18.0.103:2888:3888” \ zookeeper:3.4.10
查看容器
sudo docker ps -a |grep -E ‘zookeeper*’
- **启动容器**
```bash
for node in `seq 1 3`; do \
sudo docker start zookeeper_node${node}; \
done
重启容器
for node in `seq 1 3`; do \ sudo docker restart zookeeper_node${node}; \ done
停止容器
for node in `seq 1 3`; do \ sudo docker stop zookeeper_node${node}; \ done
删除容器
for node in `seq 1 3`; do \ sudo docker rm zookeeper_node${node}; \ done
清理数据
rm -rf /usr/local/zkCluster/node*/* rm -rf /usr/local/zkCluster/node2/* rm -rf /usr/local/zkCluster/node3/*
5. 验证
```bash
进入容器
sudo docker exec -it zookeeper_node1 bin/zkCli.sh
查看zk节点状态
sudo docker exec -it zookeeper_node1 bin/zkServer.sh status
查看zk集群状态
for node in seq 1 3
; do \
sudo docker exec -it zookeeper_node${node} bin/zkServer.sh status; \
done
查看日志
sudo docker logs zookeeper_node1 sudo docker logs -f -t —tail=50 zookeeper_node1 ```
参考
博客园:Docker下安装ZooKeeper(单机 & 集群)
https://www.cnblogs.com/LUA123/p/11428113.html