全新集群选举
zookeeper 全新集群选举机制网上资料很多说法很模糊,仔细思考了一下,应该是这样
得到票数最多的机器>机器总数半数
具体启动过程中的哪个节点成为 leader 与 zoo.cfg 中配置的节点数有关,下面以3个举例
选举过程如下
- server1 启动,给自己投票,然后群发投票信息,由于其他 server 没有启动,所有收不到任何反馈信息,状态处于 LOOKING
- server2 启动,给自己投票,由于myid文件内容比server1的大,自己得票两票,机器总数半数是1.5,符合上面的规则所以胜出,server2 成为 leader,server1 成为 follower
- 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为节点数
为什么是奇数?之前写过一篇文章
redis集群最少使用三个主节点和使用16384个槽以及主节点数量不超过1000的原因_zlpzlpzyd的博客-CSDN博客
很多说法是按照5个节点来讲的,按照公式来讲三个应该可以。
下面就来试一下
在自己的机器上安装 zookeeper
三个节点
分别为 master、node1、node2
zoo.cfg
sudo tee conf/zoo.cfg <<-'EOF'
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/opt/zookeeper
clientPort=2181
server.1=master:2888:3888
server.2=node1:2888:3888
server.3=node2:2888:3888
EOF
各个节点的 zoo.cfg 文件内容一致。各个节点 dataDir 中文件 myid 内容如下。
master
echo 1 > /opt/zookeeper/myid
node1
echo 2 > /opt/zookeeper/myid
node2
echo 3 > /opt/zookeeper/myid
启动 zookeeper
先启动两个节点,看看情况
[root@master apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@node1 apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
节点状态
[root@master apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
[root@node1 apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
配置了三个节点,只启动了两个节点的情况下,正常选举,符合想法。说明3个节点中有两个节点启动就可以完成选举流程。
下面再启动 node2
[root@node2 apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@node2 apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
符合上面的推测
突发奇想,两个节点会是什么情况?
个人猜测,跟上面两个节点启动一致。
两个节点
分别为 master、node1
zoo.cfg
sudo tee conf/zoo.cfg <<-'EOF'
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/opt/zookeeper
clientPort=2181
server.1=master:2888:3888
server.2=node1:2888:3888
server.3=node2:2888:3888
EOF
各个节点的 zoo.cfg 文件内容一致。
启动 zookeeper
[root@master apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@node1 apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
节点状态
[root@master apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
[root@node1 apache-zookeeper-3.9.0-bin]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.9.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
最终结果如上面猜测。
为什么最少三个节点?
去看一下官方文档
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,当其中一个节点有故障,没有足够的机器进行优先级处理。
符合上面的最少三个节点的理论。