• zookeeper选举机制


    目录

    全新集群选举

    选举过程如下

    三个节点

    启动 zookeeper

    节点状态

    两个节点

    启动 zookeeper

    节点状态

    为什么最少三个节点?

    非全新集群选举


    全新集群选举

    zookeeper 全新集群选举机制网上资料很多说法很模糊,仔细思考了一下,应该是这样

    得到票数最多的机器>机器总数半数

    具体启动过程中的哪个节点成为 leader 与 zoo.cfg 中配置的节点数有关,下面以3个举例

    选举过程如下

    1. server1 启动,给自己投票,然后群发投票信息,由于其他 server 没有启动,所有收不到任何反馈信息,状态处于 LOOKING
    2. server2 启动,给自己投票,由于myid文件内容比server1的大,server1 给自己投票,加上给自己的投票,自己得票共两票,机器总数半数是1.5,符合上面的规则所以胜出,server2 成为 leader,server1 成为 follower
    3. server3 启动,给自己投票,由于myid文件内容比server2的大,自己得票三票,机器总数半数是1.5,符合上面的规则,但是 leader 已经有了,所以只能成为 follower(有点委屈,有点类似于国内结婚每个男人只有一个老婆,哈哈哈)

    机器总数对应在 zoo.cfg 中配置如下形式配置节点信息

    server.X=A:B:C

    X 代表当前机器在整个架构中的序号,是整数

    A 代表第X个节点的ip地址

    B 代表第X个节点与 leader 节点交换信息的端口

    C 表示万一 leader 节点宕机,需要C这个端口在多个 follower 之间进行选举,选出一个新的 leader,用来进行选举时节点之间相互通信的端口

    集群节点数量计算公式

    y=2x+1,x为正整数,y为节点数

    为什么是奇数?之前写过一篇文章

    https://blog.csdn.net/zlpzlpzyd/article/details/132716450

    很多说法是按照5个节点来讲的,按照公式来讲三个应该可以。

    下面就来试一下

    在自己的机器上安装 zookeeper

    三个节点

    分别为 master、node1、node2

    zoo.cfg

    1. sudo tee conf/zoo.cfg <<-'EOF'
    2. tickTime=2000
    3. initLimit=10
    4. syncLimit=5
    5. dataDir=/opt/zookeeper
    6. clientPort=2181
    7. server.1=master:2888:3888
    8. server.2=node1:2888:3888
    9. server.3=node2:2888:3888
    10. EOF

    各个节点的 zoo.cfg 文件内容一致。各个节点 dataDir 中文件 myid 内容如下。

    master

    echo 1 > /opt/zookeeper/myid

    node1

    echo 2 > /opt/zookeeper/myid

    node2

    echo 3 > /opt/zookeeper/myid

    启动 zookeeper

    先启动两个节点,看看情况

    1. [root@master apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh start
    2. ZooKeeper JMX enabled by default
    3. Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
    4. Starting zookeeper ... STARTED
    1. [root@node1 apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh start
    2. ZooKeeper JMX enabled by default
    3. Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
    4. Starting zookeeper ... STARTED

    节点状态

    1. [root@master apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh status
    2. ZooKeeper JMX enabled by default
    3. Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
    4. Client port found: 2181. Client address: localhost. Client SSL: false.
    5. Mode: follower
    1. [root@node1 apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh status
    2. ZooKeeper JMX enabled by default
    3. Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
    4. Client port found: 2181. Client address: localhost. Client SSL: false.
    5. Mode: leader

    配置了三个节点,只启动了两个节点的情况下,正常选举,符合想法。说明3个节点中有两个节点启动就可以完成选举流程。

    下面再启动 node2

    1. [root@node2 apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh start
    2. ZooKeeper JMX enabled by default
    3. Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
    4. Starting zookeeper ... STARTED
    5. [root@node2 apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh status
    6. ZooKeeper JMX enabled by default
    7. Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
    8. Client port found: 2181. Client address: localhost. Client SSL: false.
    9. Mode: follower

    符合上面的推测

    突发奇想,两个节点会是什么情况?

    个人猜测,跟上面两个节点启动一致。

    两个节点

    分别为 master、node1

    zoo.cfg

    1. sudo tee conf/zoo.cfg <<-'EOF'
    2. tickTime=2000
    3. initLimit=10
    4. syncLimit=5
    5. dataDir=/opt/zookeeper
    6. clientPort=2181
    7. server.1=master:2888:3888
    8. server.2=node1:2888:3888
    9. EOF

    各个节点的 zoo.cfg 文件内容一致。

    启动 zookeeper

    1. [root@master apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh start
    2. ZooKeeper JMX enabled by default
    3. Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
    4. Starting zookeeper ... STARTED
    1. [root@node1 apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh start
    2. ZooKeeper JMX enabled by default
    3. Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
    4. Starting zookeeper ... STARTED

    节点状态

    1. [root@master apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh status
    2. ZooKeeper JMX enabled by default
    3. Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
    4. Client port found: 2181. Client address: localhost. Client SSL: false.
    5. Mode: follower
    1. [root@node1 apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh status
    2. ZooKeeper JMX enabled by default
    3. Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
    4. Client port found: 2181. Client address: localhost. Client SSL: false.
    5. Mode: leader

    最终结果如上面猜测。

    有一个问题比较奇怪,启动后只有 leader 节点绑定了 2888 端口,其他 follower 节点没绑定。

    1. [root@master zookeeper]# netstat -tunlp | grep 6244
    2. tcp6 0 0 :::2181 :::* LISTEN 6244/java
    3. tcp6 0 0 :::42279 :::* LISTEN 6244/java
    4. tcp6 0 0 192.168.0.70:3888 :::* LISTEN 6244/java
    5. tcp6 0 0 :::8080 :::* LISTEN 6244/java
    1. [root@node1 zookeeper]# netstat -tunlp | grep 9409
    2. tcp6 0 0 192.168.0.73:2888 :::* LISTEN 9409/java
    3. tcp6 0 0 192.168.0.73:3888 :::* LISTEN 9409/java
    4. tcp6 0 0 :::8080 :::* LISTEN 9409/java
    5. tcp6 0 0 :::2181 :::* LISTEN 9409/java
    6. tcp6 0 0 :::37957 :::* LISTEN 9409/java
    1. [root@node2 zookeeper]# netstat -tunlp | grep 17645
    2. tcp6 0 0 192.168.0.79:3888 :::* LISTEN 17645/java
    3. tcp6 0 0 :::8080 :::* LISTEN 17645/java
    4. tcp6 0 0 :::2181 :::* LISTEN 17645/java
    5. tcp6 0 0 :::36683 :::* LISTEN 17645/java

    为什么最少三个节点?

    去看一下官方文档

    ZooKeeper: Because Coordinating Distributed Systems is a Zoo

    For replicated mode, a minimum of three servers are required, and it is strongly recommended that you have an odd number of servers. If you only have two servers, then you are in a situation where if one of them fails, there are not enough machines to form a majority quorum. Two servers are inherently less stable than a single server, because there are two single points of failure.

    对于集群节点,最少的 server 数量是三个, 强烈建议是奇数。如果是两个 server,当其中一个节点有故障,没有足够的机器进行优先级处理。

    符合上面的最少三个节点的理论。

    非全新集群选举

  • 相关阅读:
    回顾软件编程的四种命名方法
    Oracle、MySQL等数据库故障处理优质文章分享 | 10月文章汇总
    油猴脚本:bing 搜索结果居中
    Docker操作总结
    SystemVerilog Assertions应用指南 第一章(1.28章节 内建的系统函数)
    遥感云大数据在灾害、水体与湿地领域及GPT模型应用
    Java 中的 IO 和 NIO
    2311ddip1000不能从函数返回域引用
    新手炒外汇,如何防止炒外汇被坑?
    Cannot resolve dependency to assembly Microsoft Windows.Design.Extensibility
  • 原文地址:https://blog.csdn.net/zlpzlpzyd/article/details/133564065