本章主要讲解Zookeeper集群的搭建,搭建方式采用Docker容器的方式。
开始之间先讲解几个要点:
我们下面搭建一个三个节点的Zookeeper集群节点主机名称分别为zoo1,zoo2,zoo3
1.准备一份配置文件zoo.cfg,内容如下:
tickTime=2000
dataDir=/var/lib/zookeeper/data
dataLogDir=/var/lib/zookeeper/datalog
clientPort=2181
initLimit=5
syncLimit=2
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888
2.准备三分serverId文件
echo 1 > /usr/local/docker_app/zookeeper/zoo1/data/myid
echo 2 > /usr/local/docker_app/zookeeper/zoo2/data/myid
echo 3 > /usr/local/docker_app/zookeeper/zoo3/data/myid
3.创建docker私有网络
docker network create zknet --subnet=172.19.0.0/16
4.在/usr/local/docker_app/zookeeper准备一个hosts文件,内容如下:
172.19.0.2 zoo1
172.19.0.3 zoo2
172.19.0.4 zoo3
5.启动三个Zookeeper节点
# 启动zoo1节点
docker run -d \
-v /usr/local/docker_app/zookeeper/zoo1/data:/var/lib/zookeeper/data \
-v /usr/local/docker_app/zookeeper/zoo1/datalog:/var/lib/zookeeper/datalog \
-v /usr/local/docker_app/zookeeper/conf/zoo.cfg:/conf/zoo.cfg \
-v /usr/local/docker_app/zookeeper/hosts:/etc/hosts \
-p 2181:2181 \
--hostname=zoo1 \
--name=zookeeper1 \
--net zknet \
--ip 172.19.0.2 \
zookeeper
# 启动zoo2节点
docker run -d \
-v /usr/local/docker_app/zookeeper/zoo2/data:/var/lib/zookeeper/data \
-v /usr/local/docker_app/zookeeper/zoo2/datalog:/var/lib/zookeeper/datalog \
-v /usr/local/docker_app/zookeeper/conf/zoo.cfg:/conf/zoo.cfg \
-v /usr/local/docker_app/zookeeper/hosts:/etc/hosts \
-p 2182:2181 \
--hostname=zoo2 \
--name=zookeeper2 \
--net zknet \
--ip 172.19.0.3 \
zookeeper
# 启动zoo3节点
docker run -d \
-v /usr/local/docker_app/zookeeper/zoo3/data:/var/lib/zookeeper/data \
-v /usr/local/docker_app/zookeeper/zoo3/datalog:/var/lib/zookeeper/datalog \
-v /usr/local/docker_app/zookeeper/conf/zoo.cfg:/conf/zoo.cfg \
-v /usr/local/docker_app/zookeeper/hosts:/etc/hosts \
-p 2183:2181 \
--hostname=zoo3 \
--name=zookeeper3 \
--net zknet \
--ip 172.19.0.4 \
zookeeper
6.集群启动成功查看节点状态
# 进入容器
docker exec -it zookeeper1 bash
zkServer.sh status