1
votes

I used two machines to build ZooKeeper cluster. Then I have following questions:

(1) After configuration, and start Zookeeper separately on each machine, they work as follows: machine A is showed as follower, machine B is leader. But I have set machine A as server.1 and machine B as server.2. I wonder how ZooKeeper chose the leader and follower

(2) Then I run "zkServer.sh stop" on Machine B ,then run "zkServer.sh status" on Machine A, and found en error "Error contacting service. It is probably not running." The expected behaviour should be that machine A will change from follower to leader, right? But why it is failed. And after I start again on machine B, then running "zkServer.sh status" on Machine A will get the message that machine A is follower

So what's wrong? Can two machines not be used for ZooKeeper cluster, and at lease 3 machines? Did I made a wrong cofiguration?

1
What is your zoo.cfg? – Jacky1205

1 Answers

2
votes

Answer 1

Leader election does not rely on the id in server.xx. In short, Zookeeper's leader election is implemented with Zab (Atomic broadcast). You could see more details in Andr'e Medeiros's lecture: ZooKeeper’s atomic broadcast protocol: Theory and practice

Answer 2

A cluster with 2 nodes could work, but not recommended in practice; It is because ZK requires a quorum (majority) of nodes alive (2 / 2) + 1 = 2. That means the cluster requires all 2 nodes alive. If one of nodes fails, the entire cluster could not work.

That's why ZK asks at least 3 nodes, to tolerant 1 node failure; and the number of nodes should be odd.