ZooKeeper cluster with Docker Compose
Tags: clickhouse, zookeeper, docker
Published: 2021-01-26 13:56:59

In [this post](https://dev.to/jv/zookeeper-cluster-in-vagrant-49e9) I talked about setting up a ZooKeeper cluster using Vagrant. In this post I will talk about doing it without having to launch 3 different virtual servers in our development environment thanks to [Docker Compose](https://docs.docker.com/compose/). We will need Docker and Docker Compose installed, of course.

The `docker-compose.yml` will look like this:

```yaml
version: "3.9"
services:
  zk1:
    container_name: zk1
    hostname: zk1
    image: bitnami/zookeeper:3.6.2
    ports:
      - 21811:2181
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
      - ZOO_SERVER_ID=1
      - ZOO_SERVERS=0.0.0.0:2888:3888,zk2:2888:3888,zk3:2888:3888
  zk2:
    container_name: zk2
    hostname: zk2
    image: bitnami/zookeeper:3.6.2
    ports:
      - 21812:2181
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
      - ZOO_SERVER_ID=2
      - ZOO_SERVERS=zk1:2888:3888,0.0.0.0:2888:3888,zk3:2888:3888
  zk3:
    container_name: zk3
    hostname: zk3
    image: bitnami/zookeeper:3.6.2
    ports:
      - 21813:2181
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
      - ZOO_SERVER_ID=3
      - ZOO_SERVERS=zk1:2888:3888,zk2:2888:3888,0.0.0.0:2888:3888
  zoonavigator:
    container_name: zoonavigator
    image: elkozmon/zoonavigator
    ports:
      - 9000:9000
```

It will set up a 3 node cluster, `zk1`, `zk2` and `zk3`. All we have to do is:

```bash
$ docker-compose up -d zk1 zk2 zk3 zoonavigator
Pulling zk1 (bitnami/zookeeper:3.6.2)...
3.6.2: Pulling from bitnami/zookeeper
ff7c165d667c: Pull complete
68ff1df62e09: Pull complete
b2c65a5b1bba: Pull complete
299e03a29f26: Pull complete
ac359c31a869: Pull complete
73ff1a3aae4d: Pull complete
2ff27ed904ba: Pull complete
671b552f88a4: Pull complete
8a18f1c76d65: Pull complete
144c6a5e1dd4: Pull complete
6c8379a42a77: Pull complete
2c88ec00718c: Pull complete
ce2390504de6: Pull complete
Digest: sha256:4bfdd618105aad42160263e5586b9295aa949a932f9bf7736fe148328adc6e3a
Status: Downloaded newer image for bitnami/zookeeper:3.6.2
Pulling zoonavigator (elkozmon/zoonavigator:)...
latest: Pulling from elkozmon/zoonavigator
bb79b6b2107f: Pull complete
00028440d132: Pull complete
ebd07266fb43: Pull complete
33e3eef28e5d: Pull complete
d356e0493ed9: Pull complete
d20cf66b6cb3: Pull complete
0f036ba81390: Pull complete
4245cea4b993: Pull complete
2b8eb69a8e7b: Pull complete
Digest: sha256:a1089a133f116bd760d361d6b0c0b4cbe2fd28d9a81db13cb53547669e70e933
Status: Downloaded newer image for elkozmon/zoonavigator:latest
Creating zk3          ... done
Creating zoonavigator ... done
Creating zk1          ... done
Creating zk2          ... done
```

We could even access the ZooKeeper servers from the host server thanks to port forwarding: `zk1` will be available through port 21811, `zk2` through port 21812, and `zk3` through port 21813. 

Finally, we can also set up a container that will run [zoonavigator](https://github.com/elkozmon/zoonavigator), a web-based ZooKeeper UI and editor/browser with many features.

We can access zoonavigator from a browser in the host server just using the URL http://localhost:9000/, and connect to `zk1` just using `zk1:8121` as the connection string.

![zoonavigator](https://dev-to-uploads.s3.amazonaws.com/i/9q69j5gtutlzzu7skae9.png)