docker compose 搭建zookeeper集群
时间: 2023-07-08 19:56:04 浏览: 195
要使用Docker Compose搭建Zookeeper集群,可以按照以下步骤进行操作:
1. 创建一个Docker Compose文件,例如`docker-compose.yml`,内容如下:
```
version: '3'
services:
zookeeper1:
image: zookeeper:3.6.3
container_name: zookeeper1
restart: always
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
ports:
- "2181:2181"
- "2888:2888"
- "3888:3888"
volumes:
- ./data/zookeeper1:/data
- ./logs/zookeeper1:/datalog
zookeeper2:
image: zookeeper:3.6.3
container_name: zookeeper2
restart: always
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
ports:
- "2182:2181"
- "2889:2888"
- "3889:3888"
volumes:
- ./data/zookeeper2:/data
- ./logs/zookeeper2:/datalog
zookeeper3:
image: zookeeper:3.6.3
container_name: zookeeper3
restart: always
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
ports:
- "2183:2181"
- "2890:2888"
- "3890:3888"
volumes:
- ./data/zookeeper3:/data
- ./logs/zookeeper3:/datalog
```
这里定义了三个Zookeeper节点,分别是`zookeeper1`、`zookeeper2`和`zookeeper3`,使用的镜像是官方的`zookeeper:3.6.3`,并分别映射了Zookeeper的端口和数据卷。
2. 创建数据卷目录,例如`./data/zookeeper1`、`./data/zookeeper2`和`./data/zookeeper3`,用于存储Zookeeper的数据。
3. 创建日志目录,例如`./logs/zookeeper1`、`./logs/zookeeper2`和`./logs/zookeeper3`,用于存储Zookeeper的日志。
4. 执行`docker-compose up -d`命令启动Zookeeper集群。
5. 可以通过`docker-compose ps`命令查看Zookeeper节点的状态。
至此,Zookeeper集群就搭建完成了。
阅读全文