28

A ZooKeeper Quorum consisting of three ZooKeeper servers has been created.

The zoo.cfg located on all three ZooKeeper servers looks as follows:

maxClientCnxns=50
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
dataDir=/var/lib/zookeeper
# the port at which the clients will connect
clientPort=2181

server.1=<ip-address-1>:2888:3888
server.2=<ip-address-2>:2888:3888
server.3=<ip-address-3>:2888:3888

Analysis

It is clear that one of the three ZooKeeper servers will become the Leader and the others Followers. If the Leader ZooKeeper server has been shutdown, the Leader election will start again. The aim is to check if another ZooKeeper server will become the Leader if the Leader server has been shut down.

030
  • 5,731
  • 12
  • 61
  • 107

3 Answers3

57

It is possible to check whether a ZooKeeper server is a leader or follower using the nc command that is included in the netcat package:

echo stat | nc localhost 2181 | grep Mode
echo srvr | nc localhost 2181 | grep Mode #(From 3.3.0 onwards)

If the ZooKeeper server is a leader then the command will return: Mode: leader and otherwise: Mode: follower

030
  • 5,731
  • 12
  • 61
  • 107
bsd
  • 686
  • 6
  • 4
6

Alternatively the following could be used:

bin/zkServer.sh status

It will print the mode in the output:

ZooKeeper JMX enabled by default
Using config: /home/kafka/zookeeper/bin/../conf/zoo.cfg
Mode: follower
Caner
  • 269
  • 2
  • 6
0
  1. echo "srvr" | nc localhost 2181 | grep "Mode"

    root@zoo2:/apache-zookeeper-3.8.0-bin# echo "srvr" | nc localhost 2181 | grep "Mode"
    Mode: follower
    
  2. using bin/zkServer.sh status:

    root@zoo2:/apache-zookeeper-3.8.0-bin# bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /conf/zoo.cfg
    Client port found: 2181. Client address: localhost. Client SSL: false.
    Mode: follower
    
  3. If you set ZOO_4LW_COMMANDS_WHITELIST: "*", you can use echo "stat" | nc localhost 2181

    root@zoo2:/apache-zookeeper-3.8.0-bin# echo "stat" | nc localhost 2181
    Zookeeper version: 3.8.0-5a02a05eddb59aee6ac762f7ea82e92a68eb9c0f, built on 2022-02-25 08:49 UTC
    Clients:
     /127.0.0.1:47200[0](queued=0,recved=1,sent=0)
    
    Latency min/avg/max: 0/0.0/0
    Received: 5
    Sent: 4
    Connections: 1
    Outstanding: 0
    Zxid: 0x0
    Mode: follower
    Node count: 5
    

    see The Four Letter Words

  4. using http://localhost:8080/commands/srvr

    root@zoo2:/apache-zookeeper-3.8.0-bin# wget --quiet --output-document=/dev/stdout http://localhost:8080/commands/srvr | grep "server_state"
        "server_state" : "follower",
    

    or

    root@zoo2:/apache-zookeeper-3.8.0-bin# curl --silent http://localhost:8080/commands/stat | grep "server_state"
    

    New in 3.5.0: The AdminServer is an embedded Jetty server that provides an HTTP interface to the four letter word commands. By default, the server is started on port 8080, and commands are issued by going to the URL "/commands/[command name]", e.g., http://localhost:8080/commands/stat. The command response is returned as JSON. Unlike the original protocol, commands are not restricted to four-letter names, and commands can have multiple names; for instance, "stmk" can also be referred to as "set_trace_mask". To view a list of all available commands, point a browser to the URL /commands (e.g., http://localhost:8080/commands). See the AdminServer configuration options for how to change the port and URLs.

    see The AdminServer

  5. using http://localhost:8080/commands/leader

    root@zoo2:/apache-zookeeper-3.8.0-bin# curl --silent http://localhost:8080/commands/leader
    {
      "is_leader" : false,
      "leader_id" : 3,
      "leader_ip" : "zoo3",
      "command" : "leader",
      "error" : null
    }
    
mforsetti
  • 2,488
  • 2
  • 14
  • 20
Nick Dong
  • 101
  • 2