根据项目需要,三台机器搭建一个zookeeper集群,10.10.10.1(虚拟IP,下同)为主节点,10.10.10.2和10.10.10.3为从节点。
1、Zookeeper安装依赖jdk,参照一步骤安装jdk,三台机器同步安装。
2、上传zookeeper安装包到三台机器的/data下。
3、解压安装包
解压命令为:tar -zxvf zookeeper-3.4.6.tar.gz
4、在解压后的/data/zookeeper-3.4.6下新建一个data目录
mkdir/data/zookeeper-3.4.6/data
5、在新建的/data/zookeeper-3.4.6/data中创建一个myid文件,用于指定节点id。
三台机器的myid文件依次加上序号,如下:



6、修改配置文件
配置文件在/data/zookeeper-3.4.6/conf下,将zoo_sample.cfg改名为zoo.cfg
mv ./zoo_sample.cfg zoo.cfg

编辑zoo.cfg(三台机器的配置一样)
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/zookeeper-3.4.6/data
clientPort=2181
maxClientCnxns=6000
autopurge.snapRetainCount=3
autopurge.purgeInterval=1
server.1=txla1001:2888:3888
server.2=txla1002:2888:3888
server.3=txla1003:2888:3888
其中,标红处为三台机器的hostname;server.1、server.2、server.3,其中的数字要与myid配置文件中数字保持一致,该id指定机器id在启动时用于选举使用。
7、启动集群
注意:启动集群之前需要先将各节点的防火墙关闭。
关闭防火墙命令:
sudo systemctl stop firewalld.service && sudo systemctl disable firewalld.service
各节点进入zookeeper的bin目录使用./zkServer.sh start 命令启动zookeeper。

使用./zkServer.sh status查看启动状态



如上图,集群启动成功。