> ## Documentation Index
> Fetch the complete documentation index at: https://docs.controlplane.com/llms.txt
> Use this file to discover all available pages before exploring further.

# ZooKeeper

> Deploy Apache ZooKeeper 3.9 on Control Plane with SASL authentication, as an independent ensemble of 3 or 5 servers in each location or one ensemble across 3, 5 or 7 locations. Installs into an existing GVC; a dictionary secret with the client login and quorum password must exist first.

## Overview

Apache ZooKeeper is a coordination service for distributed applications: configuration storage, leader election, distributed locks, service discovery and group membership, kept consistent by a quorum of servers. This template runs either an independent ensemble of 3 or 5 servers in each location (`perLocation`) or one ensemble spread across 3, 5 or 7 locations with one server in each (`global`). Clients and servers authenticate with SASL.

> [!NOTE]
>
> This template deploys into an existing [GVC](https://docs.controlplane.com/reference/gvc.md) you already have; it does not create, provision or manage one. With `perLocation` and no `locations` list, an ensemble runs in every location of the GVC; with a list, only the listed locations run servers and any other GVC location starts nothing. With `global`, every listed location must already be a location of your GVC.

### What Gets Created

| Resource | Name | Purpose | Optional |
| - | - | - | - |
| Workload | `RELEASE_NAME-zookeeper` | The ZooKeeper servers: a stateful workload running the official `zookeeper` image; clients on port `2181`, server-to-server traffic on ports `2888` and `3888` | — |
| Volume Set | `RELEASE_NAME-zookeeper-vs` | One volume per server at `/data` holding snapshots and the transaction log, with daily volume snapshots | — |
| Identity | `RELEASE_NAME-zookeeper-identity` | Bound to the workload so it can read your credentials secret | — |
| Policy | `RELEASE_NAME-zookeeper-policy` | Grants the identity `reveal` on exactly your credentials secret | — |
| Policy | `RELEASE_NAME-zookeeper-gvc-policy` | Grants the identity `view` on the install GVC so each server can check at start that every listed location exists | `topology: global` |

Nothing is published to the internet: the servers accept connections only from inside Control Plane and make no outbound internet connections.

## Prerequisites

**1. A credentials secret**

A `dictionary` secret with three keys: `username` and `password` (the login every client uses) and `quorumPassword` (the servers authenticate to each other with it). Create it in the same org before you install, with hex passwords (no spaces, quotes or backslashes):

```bash theme={null}
cpln secret create-dictionary --name SECRET_NAME \
  --entry username=app \
  --entry password="$(openssl rand -hex 24)" \
  --entry quorumPassword="$(openssl rand -hex 24)"
```

Read it back with `cpln secret reveal SECRET_NAME -o yaml`. Set `credentialsSecretName` to this name.

**2. For global, a GVC with at least 3 locations**

Read the GVC's locations (`spec.staticPlacement.locationLinks`):

```bash theme={null}
cpln gvc get GVC_NAME -o yaml
```

List 3, 5 or 7 of them in `locations`. A server whose GVC lacks a listed location refuses to start.

> [!WARNING]
>
> If the credentials secret does not exist when you install, the deployment waits silently and `cpln logs` stays empty. `cpln workload get-deployments RELEASE_NAME-zookeeper --gvc GVC_NAME -o yaml` names the missing secret in `status.versions[].message`. After the secret exists, the deployment recovers on its own within about 10 minutes. To skip the wait, run `cpln workload force-redeployment RELEASE_NAME-zookeeper --gvc GVC_NAME`.

## Installation

Install with the default shape, an independent 3-server ensemble in every location of the GVC:

```bash theme={null}
cpln helm install RELEASE_NAME oci://ghcr.io/controlplane-com/templates/zookeeper \
  --version 1.0.0 --gvc GVC_NAME \
  --set credentialsSecretName=SECRET_NAME
```

Topology, replicas and locations cannot be changed on a running release, so choose them now; see [Ensemble](#ensemble).

To install with another method, follow the instructions for your preferred tool:

> [card] [UI](https://docs.controlplane.com/template-catalog/install-manage/ui.md)
>
> Browse, install, and manage templates visually

> [card] [CLI](https://docs.controlplane.com/template-catalog/install-manage/cli.md)
>
> Manage templates from your terminal

> [card] [Terraform](https://docs.controlplane.com/template-catalog/install-manage/terraform.md)
>
> Declare templates in your Terraform configurations

> [card] [Pulumi](https://docs.controlplane.com/template-catalog/install-manage/pulumi.md)
>
> Declare templates in your Pulumi programs

## Configuration

### Credentials

```yaml theme={null}
credentialsSecretName: my-zookeeper-credentials
```

Name of the prerequisite secret described above.

### Ensemble

```yaml theme={null}
# Choose at install: topology, replicas and locations cannot be changed on a running release.
topology: perLocation  # perLocation: an independent ensemble in each location; global: one ensemble across locations
replicas: 3            # perLocation only: servers per location, 3 (survives 1 down) or 5 (survives 2)
locations: []          # perLocation: [] runs in every GVC location, or list the ones to use
                       # global: REQUIRED, 3, 5 or 7 locations of your GVC, one server each, in a fixed order
#  - name: aws-us-east-1
multiZone: false       # spread each location's servers across its availability zones; check every location supports it first; NEVER turn it off once on
```

With `perLocation`, ensembles in different locations share nothing: a znode written in one location does not exist in another. With `global`, one server in each listed location forms one ensemble that survives the loss of a minority of locations (1 of 3, 2 of 5, 3 of 7); every write is confirmed by a majority of locations, so it takes at least one cross-region round trip.

For one ensemble across three regions:

```yaml theme={null}
topology: global
locations:
  - name: aws-us-east-1
  - name: aws-us-east-2
  - name: aws-us-west-2
```

`multiZone` spreads each location's servers and their volumes across that location's availability zones, so one zone failing takes down at most a minority of a location's ensemble. Not every Control Plane location supports it: check that every location the release runs in does before you turn it on (see [multi-zone](https://docs.controlplane.com/reference/workload/general.md#multi-zone)). You can turn it on at install or at any later upgrade, but never turn it off again; see [Important Notes](#important-notes).

### Image and resources

```yaml theme={null}
image: zookeeper:3.9.7  # official image; the start script drops to the image's zookeeper user (uid 1000)

resources:
  cpu: 500m
  memory: 1Gi  # per server, Mi or Gi, at least 512Mi; the JVM heap is half of this
```

### Storage

```yaml theme={null}
volumeset:
  capacity: 10              # GiB per server (minimum 10)
  snapshots:
    schedule: "0 3 * * *"   # daily at 3am UTC; at most hourly; "" turns them off
    retentionDuration: 7d   # how long each snapshot is kept
```

### Network access

```yaml theme={null}
internalAccess:
  type: same-gvc  # options: same-gvc, same-org, workload-list
  workloads: []   # workload-list only: //gvc/GVC_NAME/workload/NAME — the ensemble itself is added automatically
```

This limits which workloads may connect. The rule covers every port, and the servers can always reach each other.

## Connecting

ZooKeeper does not point at your applications. Each client configures the connect string and the SASL login itself, and `internalAccess` decides which workloads are allowed to reach the servers.

Clients log in with SASL DIGEST-MD5 using the secret's `username` and `password`, through a JAAS `Client` section:

```text theme={null}
Client {
  org.apache.zookeeper.server.auth.DigestLoginModule required
  username="app"
  password="PASSWORD";
};
```

Pass it to a Java client with `-Djava.security.auth.login.config=/path/to/jaas.conf -Dzookeeper.fips-mode=false` (ZooKeeper 3.9 clients disable DIGEST-MD5 unless FIPS mode is off). Unauthenticated sessions are closed. In the table, `LOCATION` is a location running servers.

| What | Connect string |
| - | - |
| `perLocation`, 3 servers | `replica-0.RELEASE_NAME-zookeeper.LOCATION.GVC_NAME.cpln.local:2181,replica-1.RELEASE_NAME-zookeeper.LOCATION.GVC_NAME.cpln.local:2181,replica-2.RELEASE_NAME-zookeeper.LOCATION.GVC_NAME.cpln.local:2181` |
| `global` | `replica-0.RELEASE_NAME-zookeeper.LOCATION_1.GVC_NAME.cpln.local:2181,…` — one entry per listed location |
| One application's subtree | append a chroot: `…:2181/myapp` |

Each server logs the exact connect string for its location at start (`clients connect with: …`). In `perLocation`, point each client at its own location's servers. Never use the workload address `RELEASE_NAME-zookeeper.GVC_NAME.cpln.local`: it can route a client to another location's separate ensemble, where its data does not exist.

To verify, open a shell inside a server; it is already logged in. `LOCATION` is the location whose ensemble you want to open:

```bash theme={null}
cpln workload exec RELEASE_NAME-zookeeper --gvc GVC_NAME --location LOCATION --container zookeeper --stdin --tty -- zkCli.sh -server 127.0.0.1:2181
```

Check a server's role with `echo srvr | nc 127.0.0.1 2181` from inside the container: `Mode: leader` or `Mode: follower`. A server that answers `This ZooKeeper instance is not currently serving requests` is not in a quorum.

## Operations

### Restoring a Backup

Restoring from a volume snapshot is manual and this template does not automate it: every server of an ensemble must be restored from snapshots taken at the same time.

```bash theme={null}
cpln volumeset snapshot get RELEASE_NAME-zookeeper-vs --gvc GVC_NAME
cpln volumeset snapshot restore RELEASE_NAME-zookeeper-vs --gvc GVC_NAME --snapshot-name SNAPSHOT_NAME --location LOCATION --volume-index 0
```

Repeat the restore for every volume index of every location in the ensemble.

### Scaling and Availability

* **Fault tolerance.** An ensemble of 3 survives 1 server down, an ensemble of 5 survives 2. A `global` ensemble survives the loss of a minority of locations.
* **Rolling changes.** In `perLocation`, servers in a location are replaced one at a time, so the ensemble keeps serving. In `global`, an upgrade that changes the workload restarts every location's server at the same time, so the ensemble is unavailable until a majority is back.
* **Ready is not in quorum.** A server reports ready as soon as it answers on port 2181. Check `srvr` as above before relying on a fresh ensemble.
* **Topology, replicas and locations are fixed at install.** To change them, install a new release and move your data.
* **Growing storage.** Changing `volumeset.capacity` does not resize an existing volume; it only sizes new ones. Grow a live volume:

```bash theme={null}
cpln volumeset expand RELEASE_NAME-zookeeper-vs --gvc GVC_NAME --new-size NEW_SIZE_GIB --location LOCATION --volume-index 0
```

### Rotating Credentials

Servers read the credentials secret only at start. To rotate `username` or `password`, change one key and keep the others, then force a redeployment:

```bash theme={null}
cpln secret reveal SECRET_NAME -o yaml > secret.yaml
# edit only the key you are rotating in secret.yaml
cpln apply -f secret.yaml
cpln secret reveal SECRET_NAME -o yaml
cpln workload force-redeployment RELEASE_NAME-zookeeper --gvc GVC_NAME
```

`quorumPassword` uses the same steps, but expect a quorum outage partway through: servers restart one at a time, and servers on the old and new passwords cannot form an ensemble together, so the ensemble stops serving until a majority runs on the new password. Writes that were acknowledged are kept. Do not use `cpln workload stop` and `start` for this: they replace the chart's per-location server counts.

### Troubleshooting

> [accordion] Nothing starts and cpln logs is empty
>
> The credentials secret does not exist. See the warning under [Prerequisites](#prerequisites).

> [accordion] Workload is misconfigured (multizone enabled in an incompatible cluster)
>
> `multiZone: true` is set and a location the release runs in does not support multi-zone. Install with `multiZone: false`, or leave that location out.

> [accordion] Servers stay initializing after multiZone was turned off
>
> Each server's volume stays in the zone it was created in, and with `multiZone: false` a server whose volume is in another zone cannot be scheduled. Upgrade again with `multiZone: true`, then run `cpln workload force-redeployment RELEASE_NAME-zookeeper --gvc GVC_NAME`.

> [accordion] FATAL: listed locations are not in GVC
>
> A `global` install lists a location your GVC does not have. Add the location to the GVC or list only GVC locations, then install a new release.

> [accordion] FATAL: /data/myid is …
>
> In `global`, the order of `locations` changed, which changes a server's id. Restore the original order; it is fixed at install.

> [accordion] This ZooKeeper instance is not currently serving requests
>
> The server is not in a quorum because too few servers of the ensemble are running. Read the logs and restore a majority:
>
> ```bash theme={null}
> cpln logs '{gvc="GVC_NAME", workload="RELEASE_NAME-zookeeper"}' --limit 50 --since 10m
> ```

> [accordion] Session closed because client failed to authenticate
>
> The client has no SASL login or the wrong password. Check the JAAS `Client` section against `cpln secret reveal SECRET_NAME -o yaml`.

## Important Notes

* **Never turn `multiZone` off once it has been on.** Each server's volume stays in the zone it was created in, so with it off a server whose volume is in another zone cannot be scheduled. It is off by default and not every location supports it; see [multi-zone](https://docs.controlplane.com/reference/workload/general.md#multi-zone). `cpln helm upgrade` does not reuse earlier values, so keep `multiZone: true` in your values file on every upgrade.
* **Create the credentials secret before you install.**
* **`topology`, `replicas` and `locations` are fixed at install.** Install a new release and move your data to change them. In `global`, never reorder `locations`.
* **SASL authenticates; it does not authorize.** Any client with the login can read and change every znode. Set ACLs on your znodes (`setAcl /path sasl:app:cdrwa`) for per-application restrictions.
* **Rotate credentials with a secret update plus `cpln workload force-redeployment`.** Never `cpln workload stop` and `start`.
* **Connect through the per-server `replica-N` addresses,** never the workload address.

## External References

* [ZooKeeper documentation](https://zookeeper.apache.org/doc/current/zookeeperOver.html)
* [Administrator's guide](https://zookeeper.apache.org/doc/current/zookeeperAdmin.html)
* [Programmer's guide (ACLs, sessions, watches)](https://zookeeper.apache.org/doc/current/zookeeperProgrammers.html)
* [Getting started and CLI basics](https://zookeeper.apache.org/doc/current/zookeeperStarted.html)
* [Official ZooKeeper image](https://hub.docker.com/_/zookeeper)
