How to configure ZooKeeper cluster

Install and configure a basic ZooKeeper cluster to play with it.

Update package index.

$ sudo apt update

Install GnuPG.

$ sudo apt -y install gpg

Install Java.

$ sudo apt -y install openjdk-11-jre-headless

Create a dedicated zookeeper user.

$ sudo adduser --system --home /opt/zookeeper --shell /bin/bash --uid 810  --group --disabled-login zookeeper

Create a directory for the software archive.

$ sudo -u zookeeper mkdir /opt/zookeeper/software

Download the ZooKeeper archive and its ASCII-armored signature.

$ sudo -u zookeeper wget  --quiet --directory-prefix /opt/zookeeper/software
$ sudo -u zookeeper wget  --quiet --directory-prefix /opt/zookeeper/software

Download the public key used to sign this archive.

$ sudo -u zookeeper gpg --keyserver --recv-key AF3D175EC05DB249738D01AC8D8C3C3ED0B02E66
gpg: directory '/opt/zookeeper/.gnupg' created
gpg: keybox '/opt/zookeeper/.gnupg/pubring.kbx' created
gpg: /opt/zookeeper/.gnupg/trustdb.gpg: trustdb created
gpg: key 8D8C3C3ED0B02E66: public key "Damien Diederen (CODE SIGNING KEY) " imported
gpg: Total number processed: 1
gpg:               imported: 1

Verify signature.

$ sudo -u zookeeper gpg --verify /opt/zookeeper/software/{apache-zookeeper-3.7.0-bin.tar.gz.asc,apache-zookeeper-3.7.0-bin.tar.gz}
gpg: Signature made Wed Mar 17 09:47:05 2021 UTC
gpg:                using RSA key AF3D175EC05DB249738D01AC8D8C3C3ED0B02E66
gpg: Good signature from "Damien Diederen (CODE SIGNING KEY) " [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: AF3D 175E C05D B249 738D  01AC 8D8C 3C3E D0B0 2E66

Create an application directory.

$ sudo -u zookeeper mkdir /opt/zookeeper/zookeeper

Extract downloaded archive.

$ sudo -u zookeeper tar --directory /opt/zookeeper/zookeeper --strip-components 1 --extract --file /opt/zookeeper/software/apache-zookeeper-3.7.0-bin.tar.gz

Create a data directory.

$ sudo -u zookeeper mkdir /opt/zookeeper/zookeeper/data

Create a sample configuration.

$ sudo -u zookeeper cat <<EOF | tee /opt/zookeeper/zookeeper/conf/zoo.cfg

Update myid file that contains the machine id.

$ hostname -s | sed "s/.*\([^0-9]\)//g" | sudo -u zookeeper tee /opt/zookeeper/zookeeper/data/myid

Inspect myid file on

$ cat /opt/zookeeper/zookeeper/data/myid

Create systemd service file.

$ cat <<EOF | sudo tee /etc/systemd/system/zookeeper.service
Description=ZooKeeper Service



ExecStart=/opt/zookeeper/zookeeper/bin/ --config /opt/zookeeper/zookeeper/conf start
ExecStop=/opt/zookeeper/zookeeper/bin/ --config /opt/zookeeper/zookeeper/conf stop
ExecReload=/opt/zookeeper/zookeeper/bin/ --config /opt/zookeeper/zookeeper/conf restart


Reload systemd daemon.

$ sudo systemctl daemon-reload

Start service and enable it at boot.

$ sudo systemctl enable --now zookeeper.service 

Check mode on each server.

zookeeper1:~$ sudo -u zookeeper /opt/zookeeper/zookeeper/bin/ status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
zookeeper2:~$ sudo -u zookeeper /opt/zookeeper/zookeeper/bin/ status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
zookeeper3:~$ sudo -u zookeeper /opt/zookeeper/zookeeper/bin/ status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower

Execute cli.

$ sudo -u zookeeper /opt/zookeeper/zookeeper/bin/
/usr/bin/java                                                                                                            [10/47]
Connecting to localhost:2181                                                                                                    
2021-06-11 14:27:27,565 [myid:] - INFO  [main:Environment@98] - Client environment:zookeeper.version=3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC                                                                              2021-06-11 14:27:27,569 [myid:] - INFO  [main:Environment@98] - Client
2021-06-11 14:27:27,570 [myid:] - INFO  [main:Environment@98] - Client environment:java.version=11.0.11                         2021-06-11 14:27:27,572 [myid:] - INFO  [main:Environment@98] - Client environment:java.vendor=Debian
2021-06-11 14:27:27,573 [myid:] - INFO  [main:Environment@98] - Client environment:java.home=/usr/lib/jvm/java-11-openjdk-amd64
2021-06-11 14:27:27,574 [myid:] - INFO  [main:Environment@98] - Client environment:java.class.path=/opt/zookeeper/zookeeper/bin/../zookeeper-server/target/classes:/opt/zookeeper/zookeeper/bin/../build/classes:/opt/zookeeper/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/opt/zookeeper/zookeeper/bin/../build/lib/*.jar:/opt/zookeeper/zookeeper/bin/../lib/zookeeper-prometheus-metrics-3.7.0.jar:/opt/zookeeper/zookeeper/bin/../lib/zookeeper-jute-3.7.0.jar:/opt/zookeeper/zookeeper/bin/../lib/zookeeper-3.7.0.jar:/opt/zookeeper/zookeeper/bin/../lib/snappy-java-*.jar:/opt/zookeeper/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/opt/zookeeper/zookeeper/bin/../conf:
2021-06-11 14:27:27,574 [myid:] - INFO  [main:Environment@98] - Client environment:java.library.path=/usr/java/packages/lib:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib
2021-06-11 14:27:27,574 [myid:] - INFO  [main:Environment@98] - Client
2021-06-11 14:27:27,575 [myid:] - INFO  [main:Environment@98] - Client environment:java.compiler=
2021-06-11 14:27:27,575 [myid:] - INFO  [main:Environment@98] - Client
2021-06-11 14:27:27,576 [myid:] - INFO  [main:Environment@98] - Client environment:os.arch=amd64
2021-06-11 14:27:27,576 [myid:] - INFO  [main:Environment@98] - Client environment:os.version=5.10.0-3-amd64
2021-06-11 14:27:27,576 [myid:] - INFO  [main:Environment@98] - Client
2021-06-11 14:27:27,577 [myid:] - INFO  [main:Environment@98] - Client environment:user.home=/opt/zookeeper
2021-06-11 14:27:27,577 [myid:] - INFO  [main:Environment@98] - Client environment:user.dir=/home/vagrant
2021-06-11 14:27:27,577 [myid:] - INFO  [main:Environment@98] - Client
2021-06-11 14:27:27,580 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.max=247MB
2021-06-11 14:27:27,580 [myid:] - INFO  [main:Environment@98] - Client
2021-06-11 14:27:27,584 [myid:] - INFO  [main:ZooKeeper@637] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@68c4039c
2021-06-11 14:27:27,588 [myid:] - INFO  [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-06-11 14:27:27,596 [myid:] - INFO  [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2021-06-11 14:27:27,612 [myid:] - INFO  [main:ClientCnxn@1726] - zookeeper.request.timeout value is 0. feature enabled=false
Welcome to ZooKeeper!
2021-06-11 14:27:27,657 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1171] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181.
2021-06-11 14:27:27,657 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1173] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2021-06-11 14:27:27,693 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1005] - Socket connection established, initiating session, client: /0:0:0:0:0:0:0:1:45036, server: localhost/0:0:0:0:0:0:0:1:2181
2021-06-11 14:27:27,732 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1438] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, session id = 0x2000000eb000000, negotiated timeout = 30000


WatchedEvent state:SyncConnected type:None path:null

Test some commands.

[zk: localhost:2181(CONNECTED) 0] version
ZooKeeper CLI version: 3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC
[zk: localhost:2181(CONNECTED) 1] get /zookeeper/config