本文将搭建一组三节点的zookeeper集群
1、首先要准备三台linux服务器,并安装java环境linux安装jdk教程: linux安装jdk1.8并配置环境变量_玖玖遛的博客-CSDN博客
2、下载zookeeper并解压wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
tar xf zookeeper-3.4.6.tar.gz
3、编辑zookeeper配置文件cd zookeeper-3.4.6/conf
zk为我们准备了样例配置文件 zoo_sample.cfg 实际运行时读取的配置文件为 zoo.cfg
cp zoo_sample.cfg zoo.cfg 以样例配置文件为基准创建 zoo.cfg
vi zoo.cfg 编辑配置文件
在文件最后加上
server.1=192.168.200.11:2888:3888
server.2=192.168.200.12:2888:3888
server.3=192.168.200.13:2888:3888
并保存 ,上面ip根据自己的服务器修改,三台机器的配置都是一样的
然后最好把持久化目录 dataDir 也修改一下
zookeeper集群的ip要自己配,选leader时是节点过半原则
正常运行时,leader启个2888端口,其他节点连它通信
主挂掉时,各节点通过3888端口通信选主
修改后的配置如图,三个zookeeper节点的该配置一致
3.2完整配置文件内容和解释
# The number of milliseconds of each tick 心跳间隔
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take 从服务和主服务建立初始链接等待时间
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement 数据同步等待时间
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes. 持久化目录
dataDir=/var/zookeeper
# the port at which the clients will connect 客户端连zk的端口
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients 最大客户端连接数
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=192.168.200.11:2888:3888
server.2=192.168.200.12:2888:3888
server.3=192.168.200.13:2888:3888
4为每个节点建立自己的myid
myid文件要建立在持久化目录下(dataDir=/var/zookeeper),并且每个节点的myid要不一样,因为zookeeper启动时会根据myid的大小选出主节点,谁的初始myid更大,谁更容易当主。
并且在zookeeper集群运行时,主节点挂了之后进行选主,若节点间的zxid一致,也会去比较myid大小,来选出谁更适合当主
建立myId命令如下
node1:
mkdir -p /var/zookeeper
echo 1 > /var/zookeeper/myid
node2:
mkdir -p /var/zookeeper
echo 2 > /var/zookeeper/myid
node3:
mkdir -p /var/zookeeper
echo 3 > /var/zookeeper/myid
vi /etc/profile
在最后加入
export ZOOKEEPER_HOME=/usr/zookeeper/zookeeper-3.4.6
export PATH=$PATH:$ZOOKEEPER_HOME/bin
source /etc/profile 使环境变量立刻生效
6.启动注意事项,要开放防火墙的三个端口 2181,2888,3888
/sbin/iptables -I INPUT -p tcp --dport 2181 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 2888 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 3888 -j ACCEPT
zkServer.sh start-foreground 窗口运行
zkServer.sh start 后台运行
启动三个节点
zkServer.sh start-foreground
zkServer.sh status 查询运行状态
完成



