使用 docker-compose 进行 Zookeeper 集群搭建
使用 docker-compose 3.0 API 进行搭建,可实现无缝 docker swarm 部署。
mkdir -p service-zookeeper
vim docker-compose.yml
复制以下内容:
version: '3.1'
services:
zoo1:
image: zookeeper
restart: always
hostname: zoo1
networks:
- zoo-net
ports:
- 2181:2181
volumes:
- zoo1-data:/data
- zoo1-log:/datalog
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=0.0.0.0:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
ZOO_4LW_COMMANDS_WHITELIST: "*"
zoo2:
image: zookeeper
restart: always
hostname: zoo2
networks:
- zoo-net
ports:
- 2182:2181
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=0.0.0.0:2888:3888;2181 server.3=zoo3:2888:3888;2181
ZOO_4LW_COMMANDS_WHITELIST: "*"
zoo3:
image: zookeeper
restart: always
hostname: zoo3
networks:
- zoo-net
ports:
- 2183:2181
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=0.0.0.0:2888:3888;2181
ZOO_4LW_COMMANDS_WHITELIST: "*"
volumes:
zoo1-data:
external: false
zoo1-log:
external: false
networks:
zoo-net:
上述是 docker-compose.yml 文件,直接执行 sudo docker-compose up -d
,集群搭建成功。目前只对 zoo1 的数据进行了容器磁盘挂载。
检查集群状态:
docker-compose ps
# check cluster status
docker run -it --rm \
--link zoo1:zk1 \
--link zoo2:zk2 \
--link zoo3:zk3 \
--net service-zookeeper_zoo-net \
zookeeper:3.5.5 zkCli.sh -server zk1:2181,zk2:2181,zk3:2181
进入控制台后,先按一个回车,然后,ls /
,可开始查看 zookeeper 集群存储的数据。