搭建zookeeper集群

zk 采用投票选举机制,配置集群的个数以奇数个数进行配置,以达到zookeeper 高可用状态

1.解压zookeeper 并复制三份

//解压
tar -zxvf zookeeper-3.4.10.tar.gz 

//重命名
 mv zookeeper-3.4.10 zk
//复制三份到自己定义的目录
cp -r zk /usr/local/zkcluster/zk01 cp -r zk /usr/local/zkcluster/zk02 cp -r zk /usr/local/zkcluster/zk03

2.为每一个zk创建data 目录 ,并且data目录下设置myid 的文件,myid 文件中填写自己定义的编号,集群中唯一的整数zhid

//进入目录下
cd /usr/local/zkcluster/zk01
//创建data 目录,进行存放日志
mkdir data
//在data目录下设置myid 的文件,myid 文件中填写自己定义的编号,我这里设置为1
echo 1 >data/myid
//进入目录下
cd /usr/local/zkcluster/zk02
 //创建data 目录,进行存放日志
mkdir data
//在data目录下设置myid 的文件,myid 文件中填写自己定义的编号,我这里设置为2
echo 2 >data/myid
//进入目录下
cd /usr/local/zkcluster/zk03
 //创建data 目录,进行存放日志
mkdir data
//在data目录下设置myid 的文件,myid 文件中填写自己定义的编号,我这里设置为3
echo 3 >data/myid

创建目录展示如图

myid 里为自己设置的序号

 3.分别进入zk 目录下的conf 目录下,与单机版一样,需要修改配置文件

cd conf

cp zoo_sample.cfg zoo.cfg

4.vim zoo.cfg---其中需要修改的是红色部分的

  (1).data r日志目录,修改修改成改台zk的data目录

  (2).修改端口,必修保证每台zk 端口不重复

  (3).新增server ,集群有多少个zk 就配置多少个,对应ip 与端口。端口必须不一样,端口是zk 之间内部通讯的

# 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=/usr/local/zkcluster/zk01/data
# the port at which the clients will connect
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.241.129:2881:3881
server.2=192.168.241.129:2882:3882
server.3=192.168.241.129:2883:3883

5.启动这3台zk,写批处理一起执行

/usr/local/zkcluster/zk01/bin/./zkServer.sh start
/usr/local/zkcluster/zk02/bin/./zkServer.sh start
/usr/local/zkcluster/zk03/bin/./zkServer.sh start

6.启动完之后,我们可以分别看每个zk节点的状态,可以看到有leader 和follower 类型

[root@centos02 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zkcluster/zk02/bin/../conf/zoo.cfg
Mode: follower

[root@centos02 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zkcluster/zk01/bin/../conf/zoo.cfg
Mode: leader

这样zookeeper集群就搭建完了

原创打造,多多指教
原文地址:https://www.cnblogs.com/iscys/p/9614763.html