ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,它是集群的管理者,监视着集群中各个节点的状态根据节点提交的反馈进行下一步合理操作。最终,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
集群模式有两种形式:
使用一台机器,在该台机器上运行多个ZooKeeper Server进程。
在生产环境中,一般使用第一种形式,在练习环境中,一般使用第二种形式。
1.依赖环境:
Linux:Centos 7.5.1804
Java:JDK 1.8.0_65
Zookeeper:3.4.12
2.安装java环境,下载jdk1.8.0_65包解压到/apps目录下
vi /etc/profile
export JAVA_HOME=/apps/jdk1.8.0_65
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
source /etc/profile
3.本机安装多个zookeeper实例:1
2
3
4
5
6
7
8
9
10
11
12[root@localhost ~]# cd /software/
[root@localhost software]# tar zxf /root/zookeeper-3.4.12.tar.gz
[root@localhost software]# mv zookeeper-3.4.12/ zookeeper1
[root@localhost software]# tar zxf /root/zookeeper-3.4.12.tar.gz
[root@localhost software]# mv zookeeper-3.4.12/ zookeeper2
[root@localhost software]# tar zxf /root/zookeeper-3.4.12.tar.gz
[root@localhost software]# mv zookeeper-3.4.12/ zookeeper3
[root@localhost software]# ll
total 12
drwxr-xr-x 10 centos centos 4096 Mar 27 12:36 zookeeper1
drwxr-xr-x 10 centos centos 4096 Mar 27 12:36 zookeeper2
drwxr-xr-x 10 centos centos 4096 Mar 27 12:36 zookeeper3
4.修改三个zookeeper的配置文件:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38Server1:
[root@localhost software]# cp zookeeper1/conf/zoo_sample.cfg zookeeper1/conf/zoo.cfg
[root@localhost software]# vim zookeeper1/conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/software/data/zookeeper1
dataLogDir=/software/logs/zookeeper1
clientPort=2181
server.1=127.0.0.1:2200:2201
server.2=127.0.0.1:2210:2211
server.3=127.0.0.1:2220:2221
Server2:
[root@localhost software]# cp zookeeper2/conf/zoo_sample.cfg zookeeper1/conf/zoo.cfg
[root@localhost software]# vim zookeeper2/conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/software/data/zookeeper2
dataLogDir=/software/logs/zookeeper2
clientPort=2182
server.1=127.0.0.1:2200:2201
server.2=127.0.0.1:2210:2211
server.3=127.0.0.1:2220:2221
Server3:
[root@localhost software]# cp zookeeper3/conf/zoo_sample.cfg zookeeper1/conf/zoo.cfg
[root@localhost software]# vim zookeeper3/conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/software/data/zookeeper3
dataLogDir=/software/logs/zookeeper3
clientPort=2183
server.1=127.0.0.1:2200:2201
server.2=127.0.0.1:2210:2211
server.3=127.0.0.1:2220:2221
4.添加myid文件:1
2
3
4
5
6
7data目录下添加myid文件,用于存储一个数值,用来作为该ZooKeeper Server进程的标识。即上面配置中的NUM。
[root@localhost software]# cat data/zookeeper1/myid
1
[root@localhost software]# cat data/zookeeper2/myid
2
[root@localhost software]# cat data/zookeeper3/myid
3
5.启动zookeeper集群:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25[root@localhost software]# zookeeper1/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /software/zookeeper1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost software]# zookeeper2/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /software/zookeeper2/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost software]# zookeeper3/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /software/zookeeper3/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost software]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 127.0.0.1:2210 *:*
LISTEN 0 50 *:33730 *:*
LISTEN 0 50 127.0.0.1:2211 *:*
LISTEN 0 50 *:2181 *:*
LISTEN 0 50 *:2182 *:*
LISTEN 0 50 *:2183 *:*
LISTEN 0 50 127.0.0.1:2221 *:*
LISTEN 0 50 *:45907 *:*
LISTEN 0 50 *:45108 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 50 127.0.0.1:2201 *:*
6.查看zookeeper集群的状态:1
2
3
4
5
6
7
8
9
10
11
12[root@localhost software]# zookeeper1/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/zookeeper1/bin/../conf/zoo.cfg
Mode: follower
[root@localhost software]# zookeeper2/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/zookeeper2/bin/../conf/zoo.cfg
Mode: leader
[root@localhost software]# zookeeper3/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/zookeeper3/bin/../conf/zoo.cfg
Mode: follower
7.故障转移1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28假设zookeeper2节点(leader)故障,查看Leader是否会转移:
(1)停止zookeeper2节点:
[root@localhost software]# zookeeper2/bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /software/zookeeper2/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@localhost software]# zookeeper2/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/zookeeper2/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
(2)查看其它zookeeper节点的状态,发现Leader已经转移到zookeeper3节点
[root@localhost software]# zookeeper1/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/zookeeper1/bin/../conf/zoo.cfg
Mode: follower
[root@localhost software]# zookeeper3/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/zookeeper3/bin/../conf/zoo.cfg
Mode: leader
(3)使zookeeper2节点手动上线:
[root@localhost software]# zookeeper2/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /software/zookeeper2/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost software]# zookeeper2/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/zookeeper2/bin/../conf/zoo.cfg
Mode: follower