在一台机上搭建zookeeper集群(3节点)
主要步骤
- 复制出三份zookeeper
- cp zoo_sample.cfg zoo.cfg
- 修改zoo.cfg
zookeeper1/conf/zoo.cfg
dataDir=/usr/local/soft/zookeeper1/zkdata
dataLogDir=/usr/local/soft/zookeeper1/zklog
clientPort=2181
server.1=node1:2777:3777
server.2=node1:2888:3888
server.3=node1:2999:3999
zookeeper2/conf/zoo.cfg
dataDir=/usr/local/soft/zookeeper2/zkdata
dataLogDir=/usr/local/soft/zookeeper2/zklog
clientPort=2182
server.1=node1:2777:3777
server.2=node1:2888:3888
server.3=node1:2999:3999
zookeeper3/conf/zoo.cfg
dataDir=/usr/local/soft/zookeeper3/zkdata
dataLogDir=/usr/local/soft/zookeeper3/zklog
clientPort=2183
server.1=node1:2777:3777
server.2=node1:2888:3888
server.3=node1:2999:3999
- 创建配置的/usr/local/soft/zookeeper
1
/zkdata目录 - 在zkdata目录下创建myid文件,内容是
1
,跟zoo.cfg配置中server.1
的1一致 - 创建配置的/usr/local/soft/zookeeper
1
/zklog目录 - 启动
- /usr/local/soft/zookeeper1/bin/zkServer.sh start
- /usr/local/soft/zookeeper2/bin/zkServer.sh start
- /usr/local/soft/zookeeper3/bin/zkServer.sh start
- 查询节点zookeeper的状态角色
- /usr/local/soft/zookeeper1/bin/zkServer.sh status
- 注意,如果第一次只启动一个时候,整个集群还没全部启动成功时,获取状态会如下结果
[root@node1 conf]# /usr/local/soft/zookeeper1/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/soft/zookeeper1/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Error contacting service. It is probably not running.
- 当全部zookeeper启动之后再获取状态,可以看到角色
[root@node1 conf]# /usr/local/soft/zookeeper1/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/soft/zookeeper1/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
[root@node1 conf]# /usr/local/soft/zookeeper2/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/soft/zookeeper2/bin/../conf/zoo.cfg
Client port found: 2182. Client address: localhost. Client SSL: false.
Mode: leader
- 测试停掉leader,看时候重新选举
#把zookeeper2停掉
[root@node1 conf]# /usr/local/soft/zookeeper2/bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /usr/local/soft/zookeeper2/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
#zookeeper2状态是not running
[root@node1 conf]# /usr/local/soft/zookeeper2/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/soft/zookeeper2/bin/../conf/zoo.cfg
Client port found: 2182. Client address: localhost. Client SSL: false.
Error contacting service. It is probably not running.
#查看zookeeper1状态,还不是leader
[root@node1 conf]# /usr/local/soft/zookeeper1/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/soft/zookeeper1/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
#查看zookeeper3状态,变成是leader
[root@node1 conf]# /usr/local/soft/zookeeper3/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/soft/zookeeper3/bin/../conf/zoo.cfg
Client port found: 2183. Client address: localhost. Client SSL: false.
Mode: leader