美文网首页
ZooKeeper从入门到精通10:演示选举leader

ZooKeeper从入门到精通10:演示选举leader

作者: 金字塔下的小蜗牛 | 来源:发表于2020-04-07 13:19 被阅读0次

    ZooKeeper集群环境介绍:

    bigdata131 192.168.126.131
    bigdata132 192.168.126.132
    bigdata133 192.168.126.133

    1.启动ZooKeeper集群并查看状态

    [root@bigdata131 ~]# zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@bigdata131 ~]# jps
    1186 QuorumPeerMain
    1213 Jps
    [root@bigdata131 ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Mode: follower

    [root@bigdata132 ~]# zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@bigdata132 ~]# jps
    1187 QuorumPeerMain
    1220 Jps
    [root@bigdata132 ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Mode: leader

    [root@bigdata133 ~]# zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@bigdata133 ~]# jps
    1184 QuorumPeerMain
    1212 Jps
    [root@bigdata133 ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Mode: follower

    可以看到:初始化时bigdata132上启动的是leader,bigdata131和bigdata133上启动的是follower。

    2.杀死leader进程后再查看状态

    [root@bigdata132 ~]# kill -9 1187
    [root@bigdata132 ~]# jps
    1263 Jps
    [root@bigdata132 ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Error contacting service. It is probably not running.

    [root@bigdata131 ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Mode: follower

    [root@bigdata133 ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Mode: leader

    可以看到:杀死bigdata132上的leader后,ZooKeeper通过内部的选举算法将bigdata133上的follower选举成为新的leader。

    3.再次杀死leader进程后再查看状态

    [root@bigdata133 ~]# kill -9 1184
    [root@bigdata133 ~]# jps
    1292 Jps
    [root@bigdata133 ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Error contacting service. It is probably not running.

    [root@bigdata131 ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Error contacting service. It is probably not running.
    [root@bigdata131 ~]# jps
    1186 QuorumPeerMain
    1322 Jps

    可以看到:杀死bigdata133上的leader后,bigdata131上的follower并没有变成leader,而是出错了,但是进程还在。

    4.重启一台ZooKeeper服务器

    [root@bigdata132 ~]# zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@bigdata132 ~]# jps
    1317 QuorumPeerMain
    1342 Jps
    [root@bigdata132 ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Mode: follower

    [root@bigdata131 ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Mode: leader

    可以看到:重新启动bigdata132上的ZooKeeper后,bigdata131上的ZooKeeper又可以用了,变成了新的leader,bigdata132上的成了follower。

    5.再重启一台ZooKeeper服务器

    [root@bigdata133 ~]# zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@bigdata133 ~]# jps
    1386 Jps
    1359 QuorumPeerMain
    [root@bigdata133 ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Mode: follower

    [root@bigdata131 ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Mode: leader

    [root@bigdata132 ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
    Mode: follower

    可以看到:bigdata133上新启动的的ZooKeeper直接作为follower存在,bigdata131还是leader,bigdata132还是follower。

    总结:

    • ZooKeeper集群的节点个数不一定非得是2n+1,但是最好是2n+1,好处是可以有效减少选举的投票次数;
    • ZooKeeper集群能够工作的节点数不能少于2,否则将出现没有leader的情况,比如step3的情形。注意和ZooKeeper StandAlone模式的区别:StandAlone能够工作的节点数是1。
    • ZooKeeper集群恢复的节点直接作为follower存在,不能影响恢复前的集群状态,因为恢复前的leader可能正在对外提供服务。
    • ZooKeeper集群内部选举leader使用端口3888,ZooKeeper集群内部各个节点间的数据同步使用端口2888,ZooKeeper对外提供服务使用端口2181。

    相关文章

      网友评论

          本文标题:ZooKeeper从入门到精通10:演示选举leader

          本文链接:https://www.haomeiwen.com/subject/zpmkdhtx.html