1. 编写docker-compose.yml
version: '2'
services:
zoo1:
image: zookeeper:3.7.1
restart: always
container_name: zoo1
ports:
- "2181:2181"
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
zoo2:
image: zookeeper:3.7.1
restart: always
container_name: zoo2
ports:
- "2182:2181"
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
zoo3:
image: zookeeper:3.7.1
restart: always
container_name: zoo3
ports:
- "2183:2181"
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
2. 启动zookeeper集群
COMPOSE_PROJECT_NAME=zk_cluster docker-compose up -d
3. 查看zookeeper集群
COMPOSE_PROJECT_NAME=zk_cluster docker-compose ps
4. 从外部执行命令
docker exec -it zoo1 zkServer.sh status
docker exec -it zoo2 zkServer.sh status
docker exec -it zoo3 zkServer.sh status