95

I exploring a few options to setup kafka and I knew that the Zookeeper has to be up and running to initiate a kafka.

I would like to know how can I find the below.

1) hostname and port for my zookeeper instance---I checked the zoo.cfg and I could only find the ClientPort not the hostname, will hostname be the hostname of my box??

2) To check if ZooKeeper is up and running---I tried to do a ps -ef | grep "zoo" I could not find anything. May be I am using a wrong key word to search??

Any help would be really appreciated?

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
Suren Baskaran
  • 1,228
  • 1
  • 10
  • 17

12 Answers12

103

To check if Zookeeper is accessible. One method is to simply telnet to the proper port and execute the stats command.

root@host:~# telnet localhost 2181
Trying 127.0.0.1...
Connected to myhost.
Escape character is '^]'.
stats
Zookeeper version: 3.4.3-cdh4.0.1--1, built on 06/28/2012 23:59 GMT
Clients:

Latency min/avg/max: 0/0/677
Received: 4684478
Sent: 4687034
Outstanding: 0
Zxid: 0xb00187dd0
Mode: leader
Node count: 127182
Connection closed by foreign host.
Haoyuan Ge
  • 3,379
  • 3
  • 24
  • 40
  • 8
    telnet is often not available/installed on the servers – Vijay Kumar Aug 09 '19 at 16:15
  • 4
    'stats' is not working on my Windows. Try 'srvr' instead. – Xieyi Feb 13 '20 at 03:29
  • 2
    The list of available "Four Letter Words" are listed [here](https://zookeeper.apache.org/doc/r3.4.8/zookeeperAdmin.html#sc_zkCommands). According to this `stat` is the correct word. But - I think - it has to be whitelisted. In my case only `srvr` was whitelisted by default (windows). – TrueY Jan 20 '21 at 10:48
97

One other way would be to use 4 letter commands to validate if zookeeper service is healthy or not

echo stat | nc <zookeeper ip> 2181
echo mntr | nc <zookeeper ip> 2181
echo isro  | nc <zookeeper ip> 2181

More details on the documentation link below https://zookeeper.apache.org/doc/r3.1.2/zookeeperAdmin.html#sc_zkCommands

Mehul
  • 1,106
  • 8
  • 12
  • 12
    `echo ruok | nc 2181` just returns "imok" if it is OK, so that is probably the easiest to parse. – Mitchell Tracy Apr 19 '19 at 15:15
  • `nc` might not be available in all environments. – Jesse Chisholm Feb 06 '20 at 00:28
  • 3
    Don't forget to allow `ZOO_4LW_COMMANDS_WHITELIST="*"` or lesser e.g. `"stat"`, if you're running Zk in a Container. Otherwise you'll get a `stat is not executed because it is not in the whitelist.` [Reference](https://registry.hub.docker.com/_/zookeeper) – Semo May 04 '21 at 06:34
  • @JesseChisholm That goes without saying. There's a lot of popular commands that "might not be available in all environments," `wget` probably being the most common one despite often being used as *the* example by a huge amount of people to do web requests. Run a stripped down Docker image and even something as basic as the `ip` command might not be available. I've seen Ubuntu-based containers where `sudo` does not exist. It's really a moot point. – aggregate1166877 Sep 14 '22 at 00:36
60

Go to bin directory of Zookeeper and type

./zkServer.sh status

For More info go through below link:

http://www.ibm.com/developerworks/library/bd-zookeeper/

Hope this could help you.

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
Gaurav Mishra
  • 1,009
  • 6
  • 11
28
echo stat | nc localhost 2181 | grep Mode
echo srvr | nc localhost 2181 | grep Mode #(From 3.3.0 onwards)

Above will work in whichever modes Zookeeper is running (standalone or embedded).

Another way

If zookeeper is running in standalone mode, its a JVM process. so -

jps | grep Quorum

will display list of jvm processes; something like this for zookeeper with process ID

HQuorumPeer
Kaidul
  • 15,409
  • 15
  • 81
  • 150
16

I did some test:

When it's running:

$ /usr/lib/zookeeper/bin/zkServer.sh status
JMX enabled by default
Using config: /usr/lib/zookeeper/bin/../conf/zoo.cfg
Mode: follower

When it's stopped:

$ zkServer status                                                                                                                                
JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Error contacting service. It is probably not running.

I'm not running on the same machine, but you get the idea.

laike9m
  • 18,344
  • 20
  • 107
  • 140
10

enter the below command to verify if zookeeper is running :

echo "ruok" | nc localhost 2181 ; echo 

expected response: imok

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
G.V. Sridhar
  • 129
  • 1
  • 5
8

Zookeeper is just a Java process and when you start a Zookeeper instance it runs a org.apache.zookeeper.server.quorum.QuorumPeerMain class. So you can check for a running Zookeeper like this:

jps -l | grep zookeeper

or even like this:

jps | grep Quorum

upd:

regarding this: will hostname be the hostname of my box?? - the answer is yes.

serejja
  • 22,901
  • 6
  • 64
  • 72
4

From a Windows 10

  • Open Command Promt then type telnet localhost 2181and then you type srvr OR
  • From inside bin folder, open a PowerShell window and type zkServer.sh status
Oana
  • 537
  • 5
  • 11
3

I use:

  jps

Depending on your installation a running Zookeeper would look like

  HQuorumPeer

or sth. with zookeeper in it's name.

Christian Wirth
  • 123
  • 1
  • 9
1

For people who uses the official docker image:

https://hub.docker.com/_/zookeeper

in the whitelist is only enabled srvr

See below the example:

telnet hostname 2181                                                                                                                                
Trying ::1...
Connected to localhost.
Escape character is '^]'.
srvr
Zookeeper version: 3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC
Latency min/avg/max: 0/0.0/0
Received: 3
Sent: 2
Connections: 1
Outstanding: 0
Zxid: 0x0
Mode: standalone
Node count: 5
Connection closed by foreign host.

Doc:

ZOO_4LW_COMMANDS_WHITELIST

Defaults to srvr. Zookeeper's 4lw.commands.whitelist

A list of comma separated Four Letter Words commands that user wants to use. A valid Four Letter Words command must be put in this list else ZooKeeper server will not enable the command. By default the whitelist only contains "srvr" command which zkServer.sh uses. The rest of four letter word commands are disabled by default.

Wakerboy135
  • 91
  • 1
  • 7
0

For Window, users Install mobaxterm https://mobaxterm.mobatek.net/download.html

The mobaxterm terminal allows you to run all Linux commands

enter image description here

Karan
  • 31
  • 3
0

For those of you who used homebrew, use this command - /opt/homebrew/Cellar/zookeeper/3.8.1/bin/zkServer status

adit negi
  • 39
  • 2