序
本文将介绍如何在docker中部署zookeeper集群。
部署环境
CentOS 7 host A 192.168.199.200
CentOS 7 host B 192.168.199.201
CentOS 7 host C 192.168.199.202
部署
host A 部署zookeeper
docker run -d \
--name=zookeeper \
--net=host \
-e SERVER_ID=1 \
-e ADDITIONAL_ZOOKEEPER_1=server.1=192.168.199.200:2888:3888 \
-e ADDITIONAL_ZOOKEEPER_2=server.2=192.168.199.201:2888:3888 \
-e ADDITIONAL_ZOOKEEPER_3=server.3=192.168.199.202:2888:3888 \
-e ADDITIONAL_ZOOKEEPER_4=clientPort=2181 \
garland/zookeeper
host B 部署zookeeper
docker run -d \
--name=zookeeper \
--net=host \
-e SERVER_ID=2 \
-e ADDITIONAL_ZOOKEEPER_1=server.1=192.168.199.200:2888:3888 \
-e ADDITIONAL_ZOOKEEPER_2=server.2=192.168.199.201:2888:3888 \
-e ADDITIONAL_ZOOKEEPER_3=server.3=192.168.199.202:2888:3888 \
-e ADDITIONAL_ZOOKEEPER_4=clientPort=2181 \
garland/zookeeper
host C 部署zookeeper
docker run -d \
--name=zookeeper \
--net=host \
-e SERVER_ID=3 \
-e ADDITIONAL_ZOOKEEPER_1=server.1=192.168.199.200:2888:3888 \
-e ADDITIONAL_ZOOKEEPER_2=server.2=192.168.199.201:2888:3888 \
-e ADDITIONAL_ZOOKEEPER_3=server.3=192.168.199.202:2888:3888 \
-e ADDITIONAL_ZOOKEEPER_4=clientPort=2181 \
garland/zookeeper
查看部署结果
host A follower
[root@localhost zookeeper]# echo stat | nc 127.0.0.1 2181
Zookeeper version: 3.4.6-1569965, built on 02/20/2014 09:09 GMT
Clients:
/127.0.0.1:60706[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/0
Received: 2
Sent: 1
Connections: 1
Outstanding: 0
Zxid: 0x0
Mode: follower
Node count: 4
host B leader
[root@localhost ~]# echo stat | nc 127.0.0.1 2181
Zookeeper version: 3.4.6-1569965, built on 02/20/2014 09:09 GMT
Clients:
/127.0.0.1:38910[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/0
Received: 2
Sent: 1
Connections: 1
Outstanding: 0
Zxid: 0x100000000
Mode: leader
Node count: 4
host C follower
[root@localhost ~]# echo stat | nc 127.0.0.1 2181
Zookeeper version: 3.4.6-1569965, built on 02/20/2014 09:09 GMT
Clients:
/127.0.0.1:38236[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/0
Received: 2
Sent: 1
Connections: 1
Outstanding: 0
Zxid: 0x100000000
Mode: follower
Node count: 4
踩坑
坑1
WARNING IPv4 forwarding is disabled. Networking will not work
解决办法:
# vi /etc/sysctl.conf
或者
# vi /usr/lib/sysctl.d/00-system.conf
添加如下代码:
net.ipv4.ip_forward=1
重启network服务
# systemctl restart network
查看是否修改成功
# sysctl net.ipv4.ip_forward
如果返回为“net.ipv4.ip_forward = 1”则表示成功了
坑2
This ZooKeeper instance is not currently serving requests
解决办法:
zookeeper配置中除了 2181 端口还有 2888 和 3888 把这三个端口都开放,然后重加载firewall防火墙配置,重启zookeeper果然集群成功启动。
firewall-cmd --zone=public --add-port=2181/tcp --permanent
firewall-cmd --zone=public --add-port=2888/tcp --permanent
firewall-cmd --zone=public --add-port=3888/tcp --permanent
firewall-cmd --reload
后续进阶
后续将使用swarm + docker-compose 在overlay network 模式下进行一键构建,请听下回分解。