用于Docker Compose部署ZooKeeper集群的yaml文件:
version: '3'networks: zookeeper-networks: driver: bridgeservices: zookeeper1: image: zookeeper container_name: zookeeper1 restart: always ports: - 9001:2181 volumes: - "/usr/local/docker-compose/zookeeper/zookeeper1/data:/data" - "/usr/local/docker-compose/zookeeper/zookeeper1/datalog:/datalog" - "/usr/local/docker-compose/zookeeper/zookeeper1/logs:/logs" - "/usr/local/docker-compose/zookeeper/zookeeper1/conf:/conf" environment: ZOO_MY_ID: 1 ZOO_SERVERS: server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888 networks: - zookeeper-networks zookeeper2: image: zookeeper container_name: zookeeper2 restart: always ports: - 9002:2181 volumes: - "/usr/local/docker-compose/zookeeper/zookeeper2/data:/data" - "/usr/local/docker-compose/zookeeper/zookeeper2/datalog:/datalog" - "/usr/local/docker-compose/zookeeper/zookeeper2/logs:/logs" - "/usr/local/docker-compose/zookeeper/zookeeper2/conf:/conf" environment: ZOO_MY_ID: 2 ZOO_SERVERS: server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888 networks: - zookeeper-networks zookeeper3: image: zookeeper container_name: zookeeper3 restart: always ports: - 9003:2181 volumes: - "/usr/local/docker-compose/zookeeper/zookeeper3/data:/data" - "/usr/local/docker-compose/zookeeper/zookeeper3/datalog:/datalog" - "/usr/local/docker-compose/zookeeper/zookeeper3/logs:/logs" - "/usr/local/docker-compose/zookeeper/zookeeper3/conf:/conf" environment: ZOO_MY_ID: 3 ZOO_SERVERS: server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888 networks: - zookeeper-networks
创建用于存放配置文件的目录:
mkdir -p /usr/local/docker-compose/zookeeper/zookeeper1/conf /usr/local/docker-compose/zookeeper/zookeeper2/conf /usr/local/docker-compose/zookeeper/zookeeper3/con
创建配置文件:
vim /usr/local/docker-compose/zookeeper/zookeeper1/conf/zoo.cfg
配置文件的内容如下所示:
tickTime=2000initLimit=10syncLimit=5dataDir=/datadataLogDir=/datalogclientPort=2181# 扩展类型启用,设置为true才能创建TTL类型的节点extendedTypesEnabled=true# 集群配置server.1=zookeeper1:2888:3888server.2=zookeeper2:2888:3888server.3=zookeeper3:2888:3888
tickTime:ZooKeeper中最小的时间单位长度 (默认2000ms)。initLimit:follower节点启动后与leader节点完成数据同步的时间(指定为tickTime的倍数,默认10倍,即20s)。syncLimit:leader节点和follower节点进行心跳检测的最大延迟时间(指定为tickTime的倍数,默认5倍,即10s)。dataDir:表示ZooKeeper存储快照文件的目录(默认为/tmp/zookeeper)。dataLogDir:表示ZooKeeper事务日志的存储路径,默认指定在dataDir目录下 。clientPort:表示ZooKeeper客户端和ZooKeeper服务端建立连接的端口号(默认 2181)。
将配置文件复制到其他ZooKeeper节点挂载的配置文件目录:
cp /usr/local/docker-compose/zookeeper/zookeeper1/conf/zoo.cfg /usr/local/docker-compose/zookeeper/zookeeper2/conf/cp /usr/local/docker-compose/zookeeper/zookeeper1/conf/zoo.cfg /usr/local/docker-compose/zookeeper/zookeeper3/conf/
开始部署ZooKeeper集群:
docker compose -f /root/composefile/zookeeper/zookeeper.yaml up -d
-f指定yaml文件位置,-d表示后台运行。
docker ps和docker compose ls命令都可以查询服务是否启动成功:
Docker部署在192.168.1.9上。
使用另外一台有ZooKeeper文件的虚拟机进行测试。
./zkCli.sh -timeout 5000 -server 192.168.1.9:9001
测试创建TTL类型的节点。
[zk: 192.168.1.9:9001(CONNECTED) 1] ls /[zookeeper][zk: 192.168.1.9:9001(CONNECTED) 2] create -t 10 /kavenCreated /kaven[zk: 192.168.1.9:9001(CONNECTED) 3] ls /[kaven, zookeeper][zk: 192.168.1.9:9001(CONNECTED) 4] ls /[kaven, zookeeper][zk: 192.168.1.9:9001(CONNECTED) 5] ls /[kaven, zookeeper][zk: 192.168.1.9:9001(CONNECTED) 6] ls /[kaven, zookeeper][zk: 192.168.1.9:9001(CONNECTED) 7] ls /[kaven, zookeeper][zk: 192.168.1.9:9001(CONNECTED) 8] ls /[zookeeper]
使用Docker Compose部署ZooKeeper集群就介绍到这里,如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。