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

# Relocate ZooKeeper and BookKeeper pods

Move the ZooKeeper and BookKeeper pods of a StreamNative Platform cluster deployed with the `sn-platform` or `sn-platform-slim` Helm chart to different nodes or availability zones, without downtime and without data loss. Use this to:

* Consolidate a multi-zone cluster into a single zone, for example to cut inter-zone data transfer cost in a non-production environment
* Spread a single-zone cluster across several zones
* Rebalance a cluster whose pods ended up unevenly placed after a zone outage or a capacity shortage

## How it works

Brokers, proxies, and the other stateless components can move anywhere with a restart. ZooKeeper and BookKeeper cannot, because their data lives on persistent volumes that are bound to a zone (cloud block storage such as AWS EBS, GCP Persistent Disk, or Azure Disk) or to a single node (local SSD). A pod always follows its volume, so a ZooKeeper server or bookie stays where its volume is until the volume is replaced.

Relocating a stateful pod therefore means giving it a new, empty volume in the target location and letting Pulsar refill it:

* **ZooKeeper** replicates its full state to every server. A server that starts with empty volumes downloads a snapshot from the leader. ZooKeeper state is typically well under 1 GB, so this takes seconds.
* **BookKeeper** does not. The operator decommissions the bookie first, which re-replicates every ledger it holds onto the other bookies, and only then deletes its volumes. The pod comes back as an empty bookie with the same identity.

The new volume is created where the new pod is scheduled. To make that the target location, you point ZooKeeper and BookKeeper at a StorageClass that can only provision volumes there. A replaced pod can then only be scheduled in the target location. Only Pulsar is affected: no nodes are cordoned, no other workloads are touched, and each ZooKeeper server or bookie restarts only when you replace it.

> [!WARNING]
>
> Relocate one ZooKeeper server or one bookie at a time, and complete the checks in each step before moving to the next. Replacing two members concurrently can destroy ZooKeeper quorum or leave ledgers without enough replicas to recover.

## Prerequisites

* `kubectl` access to the namespace running your Pulsar cluster, with permission to annotate and delete pods, delete PersistentVolumeClaims, and patch PersistentVolumes
* Permission to create a StorageClass
* Permission to run `helm upgrade` on your StreamNative Platform release
* No in-flight scaling operation or upgrade on the cluster

The examples below use these variables:

```bash theme={null}
NS=<k8s_namespace>
```

In `helm upgrade` commands, `<chart>` is the chart you deployed, for example `streamnative/sn-platform` or `streamnative/sn-platform-slim`. Replace `<target-zone>` in values with the zone you are moving to.

### Inspect your environment first

**1. Where pods and volumes are today.**

```bash theme={null}
kubectl get nodes -L topology.kubernetes.io/zone

kubectl -n $NS get pods -o wide

kubectl get pv -o custom-columns=PV:.metadata.name,CLAIM:.spec.claimRef.name,LOCATION:.spec.nodeAffinity.required.nodeSelectorTerms[0].matchExpressions[0].values[0]
```

The `LOCATION` column shows the zone for zonal block storage, or the node name for node-bound volumes such as local SSD.

Only pods whose volumes are outside the target location need replacing. When consolidating into one zone, choose the zone that already holds the most ZooKeeper servers and bookies.

**2. Which StorageClasses your volumes use.** You create the target StorageClass from these in Step 1:

```bash theme={null}
kubectl -n $NS get pvc -o custom-columns=NAME:.metadata.name,STORAGECLASS:.spec.storageClassName
kubectl get storageclass -o custom-columns=NAME:.metadata.name,PROVISIONER:.provisioner,BINDING:.volumeBindingMode,RECLAIM:.reclaimPolicy
```

The StorageClass reclaim policy decides what happens to each replaced volume. With `Delete`, the old volume and its disk are removed when the claim is deleted. With `Retain`, they stay behind and you must [clean them up](#clean-up-retained-volumes) yourself.

> [!NOTE]
>
> The chart values `zookeeper.volumes.reclaimPolicy` and `bookkeeper.volumes.reclaimPolicy` do not change this. They only control whether claims are cleaned up when the cluster itself is deleted.

**3. That the target location has enough nodes.** If a component sets `affinity.type: requiredDuringSchedulingIgnoredDuringExecution` with `anti_affinity: true`, each node runs at most one pod of that component. The target location then needs at least as many schedulable nodes as the largest of the ZooKeeper replica count, the bookie replica count, and the broker replica count (`broker.autoScaling.maxReplicas`, if autoscaling is enabled). Each node also needs enough allocatable CPU and memory for one pod of each. If you rely on a node autoscaler, make sure it can add nodes in the target location.

**4. That BookKeeper does not require multiple racks.** If you set `bookkeeper.rackAwareTopologyLabels` with the zone label and `bookkeeperClientEnforceMinNumRacksPerWriteQuorum: "true"` in `broker.configData`, the brokers refuse to create ledgers once fewer racks are available than `bookkeeperClientMinNumRacksPerWriteQuorum`. Before consolidating zones, set `bookkeeperClientEnforceMinNumRacksPerWriteQuorum` to `"false"`, or remove the zone label from `rackAwareTopologyLabels`. Rack-aware placement without enforcement falls back to a single rack on its own and needs no change.

**5. That the cluster is healthy.** All pods are Running and Ready. Exactly one ZooKeeper server reports `"server_state" : "leader"`, and it reports `synced_followers` equal to the number of other servers. BookKeeper reports `Under replicated ledger count: 0`, and every bookie is listed as read-write.

```bash theme={null}
for p in $(kubectl -n $NS get pods -l cloud.streamnative.io/component=zookeeper -o jsonpath='{.items[*].metadata.name}'); do
  echo "== $p"
  kubectl -n $NS exec $p -- curl -s localhost:9990/commands/mntr | grep -E '"server_state"|synced_followers'
done

BK0=$(kubectl -n $NS get pods -l cloud.streamnative.io/component=bookie -o jsonpath='{.items[0].metadata.name}')
kubectl -n $NS exec $BK0 -- bin/bookkeeper shell listunderreplicated
kubectl -n $NS exec $BK0 -- bin/bookkeeper shell listbookies -rw -h
```

> [!NOTE]
>
> The ZooKeeper checks use the ZooKeeper AdminServer on port 9990, which the chart enables by default. The four-letter-word commands `stat` and `mntr` on port 2181 are not on the default ZooKeeper allow list.

**6. That the remaining bookies can hold one bookie's data.** While a bookie is decommissioned, its ledgers move onto the others.

```bash theme={null}
kubectl -n $NS exec $BK0 -- bin/bookkeeper shell bookieinfo
```

After absorbing the data of the bookie being replaced, the remaining bookies must stay well below `diskUsageWarnThreshold` (default 90%). The bookie count minus one must also be at least the largest ensemble size in use (`managedLedgerDefaultEnsembleSize` and any namespace-level persistence policies), or the decommission cannot complete.

## Step 1: Create a StorageClass for the target location

Create a StorageClass that provisions volumes only in the target location. Start from the class your ZooKeeper and BookKeeper volumes use today, keep its provisioner and parameters, give it a new name, and add `allowedTopologies` and `volumeBindingMode: WaitForFirstConsumer`:

```bash theme={null}
kubectl get storageclass <current_storageclass> -o yaml
```

For example, for AWS EBS:

```yaml theme={null}
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: <current_storageclass>-<target-zone>
provisioner: ebs.csi.aws.com
parameters:
  type: gp3
reclaimPolicy: Delete
allowVolumeExpansion: true
volumeBindingMode: WaitForFirstConsumer
allowedTopologies:
- matchLabelExpressions:
  - key: topology.kubernetes.io/zone
    values:
    - <target-zone>
```

```bash theme={null}
kubectl apply -f target-storageclass.yaml
```

If ZooKeeper, the journal, and the ledgers use different StorageClasses, create one target class for each.

> [!WARNING]
>
> `volumeBindingMode: WaitForFirstConsumer` is required. With `Immediate`, a new volume is provisioned before the pod is scheduled, and `allowedTopologies` alone does not keep the pod in the target location.

## Step 2: Point ZooKeeper and BookKeeper at the new StorageClass

Set the new StorageClass for every ZooKeeper and BookKeeper volume:

```yaml theme={null}
zookeeper:
  volumes:
    data:
      storageClassName: <target_storageclass>
bookkeeper:
  volumes:
    journal:
      storageClassName: <target_storageclass>
    ledgers:
      storageClassName: <target_storageclass>
```

If you enabled a separate ZooKeeper transaction log volume (`zookeeper.volumes.useSeparateDiskForTxlog`), also set `zookeeper.volumes.dataLog.storageClassName`.

> [!NOTE]
>
> If you deploy `sn-platform-slim` as a subchart of your own chart, nest these keys under the subchart name, for example `sn-platform-slim.bookkeeper.volumes.ledgers.storageClassName`.

```bash theme={null}
helm upgrade -f /path/to/values.yaml <release_name> <chart> -n $NS
```

The operator replaces the ZooKeeper and BookKeeper StatefulSets so that they carry the new volume claim templates. Running pods and their existing volumes are not affected, and no pod restarts. Only volumes created from now on use the new StorageClass. AutoRecovery can scale down for a few seconds during the replacement, then returns on its own.

Verify that the volume claim templates use the new class before continuing:

```bash theme={null}
kubectl -n $NS get sts -o custom-columns=NAME:.metadata.name,STORAGECLASSES:.spec.volumeClaimTemplates[*].spec.storageClassName
```

> [!WARNING]
>
> Do not change the bookie pod template during this procedure, for example by adding `bookkeeper.nodeSelector`. A bookie pod-template change makes the operator turn AutoRecovery off until every bookie runs the new template. A bookie that cannot start in its old location then blocks its own decommission, which needs AutoRecovery.

## Step 3: Move the stateless components

Add a `nodeSelector` for the target location to the stateless components, and turn off zone anti-affinity for the broker. The scheduler cannot satisfy a zone spread in a single zone.

```yaml theme={null}
broker:
  nodeSelector:
    topology.kubernetes.io/zone: <target-zone>
  affinity:
    zone_anti_affinity: false
proxy:
  nodeSelector:
    topology.kubernetes.io/zone: <target-zone>
toolset:
  nodeSelector:
    topology.kubernetes.io/zone: <target-zone>
pulsar_detector:
  nodeSelector:
    topology.kubernetes.io/zone: <target-zone>
streamnative_console:
  nodeSelector:
    topology.kubernetes.io/zone: <target-zone>
autorecovery:
  nodeSelector:
    topology.kubernetes.io/zone: <target-zone>
```

```bash theme={null}
helm upgrade -f /path/to/values.yaml <release_name> <chart> -n $NS
```

Brokers restart one at a time and come back in the target location. The AutoRecovery pod can take a few minutes to move. Changing `autorecovery.nodeSelector` does not restart bookies.

Do not set `zookeeper.nodeSelector` or `bookkeeper.nodeSelector`. The StorageClass from Step 1 already keeps relocated ZooKeeper servers and bookies in the target location.

The StreamNative Console stores its metadata on its own volume, mounted at `/data`. If that volume is outside the target location, the console pod stays Pending after this upgrade. To move it:

1. Back up `/data` with `kubectl cp` before the upgrade.
2. Delete the console PVC and pod.
3. Run `helm upgrade` again. This recreates the PVC.
4. Restore `/data` and restart the pod.

Verify before continuing. All brokers are Ready, on nodes in the target location, and pass the health check:

```bash theme={null}
kubectl -n $NS get pods -l cloud.streamnative.io/component=broker -o wide
kubectl -n $NS exec <toolset_pod_name> -- bin/pulsar-admin brokers healthcheck
```

## Step 4: Relocate ZooKeeper servers

Repeat this step for each ZooKeeper server whose volumes are outside the target location. Process followers first and the leader last. Replacing the leader triggers an election, and there is no reason to incur one before the final server. Servers that are already in the target location need no action.

### Back up the server's data

```bash theme={null}
ZK=<zookeeper_pod_name>
kubectl -n $NS exec $ZK -- bash -c 'tar czf - /pulsar/data $([ -d /pulsar/data-log ] && echo /pulsar/data-log)' \
  > zk-backup-$ZK-$(date +%Y%m%d%H%M%S).tar.gz
```

### Delete the volume claims, then the pod

Delete all of the server's claims together. By default, each server has one `data` volume. If you enabled a separate transaction log volume (`zookeeper.volumes.useSeparateDiskForTxlog`), it also has a `data-log` volume. Replacing only one of the two leaves stale transaction logs behind, and ZooKeeper then fails to start or drops znodes. The claims remain `Terminating` until the pod using them is gone.

To keep the server's old volumes available for [rollback](#re-bind-a-zookeeper-server-to-its-old-volume) even if the StorageClass reclaim policy is `Delete`, set their PersistentVolumes to `Retain` first and note their names:

```bash theme={null}
PVCS=$(kubectl -n $NS get pod $ZK -o jsonpath='{.spec.volumes[*].persistentVolumeClaim.claimName}')
echo $PVCS
for c in $PVCS; do
  PV=$(kubectl -n $NS get pvc $c -o jsonpath='{.spec.volumeName}')
  echo "$c -> $PV"
  kubectl patch pv $PV -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
done
```

Then delete the claims and the pod:

```bash theme={null}
kubectl -n $NS delete pvc --wait=false $PVCS
kubectl -n $NS delete pod $ZK
kubectl -n $NS wait --for=condition=Ready pod/$ZK --timeout=15m
```

The StatefulSet recreates the pod and its claims. Because the new claims use the StorageClass from Step 1, the pod is scheduled in the target location and its new volumes are provisioned there. No other ZooKeeper server restarts.

> [!NOTE]
>
> If the new pod stays Pending with `persistentvolumeclaim not found`, delete the pod once more. This is a known Kubernetes StatefulSet behavior when claims are deleted and recreated.

### Confirm the server rejoined and fully synced

```bash theme={null}
kubectl -n $NS get pod $ZK -o wide

for p in $(kubectl -n $NS get pods -l cloud.streamnative.io/component=zookeeper -o jsonpath='{.items[*].metadata.name}'); do
  echo "== $p"
  kubectl -n $NS exec $p -- curl -s localhost:9990/commands/mntr | grep -E '"server_state"|synced_followers'
done
```

The pod runs on a node in the target location. Every server reports `follower` or `leader`, and the leader reports `synced_followers` equal to the number of other servers (`2.0` in a three-server ensemble).

> [!WARNING]
>
> Do not replace the next server until the leader reports all followers synced.

## Step 5: Relocate bookies

Repeat this step for each bookie whose volumes are outside the target location, one bookie at a time, from the highest ordinal down. Bookies that are already in the target location need no action.

### Mark the bookie for decommission and delete the pod

```bash theme={null}
BK=<bookie_pod_name>
kubectl -n $NS annotate pod $BK bookkeeper.streamnative.io/decommission-ctrl="true"
kubectl -n $NS delete pod $BK --wait=false
```

The operator holds the pod with a finalizer and runs a decommission job. The job re-replicates the bookie's ledgers onto the remaining bookies and removes the bookie's cookie. Only then does the operator delete the pod's volume claims and let the StatefulSet recreate it. The new pod is scheduled in the target location with new, empty volumes and registers as the same bookie.

> [!NOTE]
>
> You do not delete bookie volume claims yourself. The operator deletes them after it has confirmed the data is safe on the other bookies.

### Monitor decommission progress

The decommission job is named after the bookie pod. Start following its logs right after deleting the pod, because the operator removes the job as soon as it completes:

```bash theme={null}
kubectl -n $NS get job $BK-decommission
kubectl -n $NS logs -f job/$BK-decommission
```

While ledgers are still being re-replicated, the job keeps waiting and logging progress. On a bookie holding hundreds of gigabytes, this can take hours. The job is done when it logs that the bookie's ledgers `are properly replicated`, followed by `Cookie of the decommissioned bookie ... is deleted successfully`. The pod is then recreated in the target location.

### Confirm the bookie returned to service

```bash theme={null}
kubectl -n $NS wait --for=condition=Ready pod/$BK --timeout=60m
kubectl -n $NS get pod $BK -o wide
kubectl -n $NS exec $BK -- curl -s http://localhost:8000/api/v1/bookie/state/readonly
kubectl -n $NS exec $BK -- bin/bookkeeper shell listbookies -rw -h
kubectl -n $NS exec $BK -- bin/bookkeeper shell listunderreplicated
```

The pod runs on a node in the target location. The bookie reports `"readOnly" : false`, every bookie is listed as read-write, and `listunderreplicated` reports `Under replicated ledger count: 0`.

> [!WARNING]
>
> Do not decommission the next bookie until `listunderreplicated` reports nothing and the replacement pod is Ready and writable. Proceeding while ledgers are still under-replicated reduces the number of intact copies and risks data loss.

## Finish

When every ZooKeeper server and bookie is in the target location:

1. Run the health checks from [Inspect your environment first](#inspect-your-environment-first) again.
2. Retire the nodes in the old location if nothing else needs them.

No final restart is needed. Every ZooKeeper and BookKeeper volume is now in the target location, so those pods can only run there, and any new volume comes from the StorageClass from Step 1.

Pulsar Functions run by Function Mesh are scheduled from their own pod specs. Set the matching `nodeSelector` there if they should follow.

## Impact on clients

| Step | Impact |
| - | - |
| Brokers restart (Step 3) | Each broker's topics move to other brokers. Producers and consumers reconnect automatically after a few seconds of added latency. Transactions open on a restarting broker abort, and clients retry them. |
| ZooKeeper replacement (Step 4) | None while a quorum is up. Replacing the leader triggers an election, and topic lookups and ownership changes pause for a few seconds. |
| Bookie replacement (Step 5) | None for correctness. Writes continue on the remaining bookies. Re-replication adds disk and network load, which can raise publish latency until the decommission completes. |

## Roll back

| Point of failure | Recovery |
| - | - |
| Steps 1 and 2 | Remove the `storageClassName` overrides and run `helm upgrade`. The operator replaces the StatefulSets again without restarting pods. |
| Step 3 | Remove the `nodeSelector` overrides and run `helm upgrade`. Brokers roll one at a time. |
| A replaced ZooKeeper server does not rejoin (Step 4) | The remaining servers still hold quorum. Do not replace another server. Check `kubectl describe pod` and the server logs. If the old volume still exists, [re-bind the server to it](#re-bind-a-zookeeper-server-to-its-old-volume). |
| A relocated bookie stays Pending (Step 5) | The decommission completed before the claims were deleted, so its data is safe on the other bookies. Its new volumes can only be created in the target location, so add capacity there, and check `kubectl describe pod` for the scheduling reason. |
| Revert the whole relocation | Create a StorageClass restricted to the original location, point ZooKeeper and BookKeeper at it as in Step 2, then relocate the servers and bookies back one at a time with Steps 4 and 5. Undo Step 3 last. |

### Re-bind a ZooKeeper server to its old volume

Use this when a relocated ZooKeeper server fails to rejoin and its old PersistentVolume still exists, either because the StorageClass reclaim policy is `Retain` or because you set the volume to `Retain` in Step 4. The server comes back in its original location with its original data, then catches up from the leader.

Do not delete the new claim and recreate it by hand. As soon as the pod is deleted, the StatefulSet recreates the claim from its template, and that claim would get a new volume. Instead, reserve the old volume for the claim name, so the claim that the StatefulSet creates binds to it.

1. Reserve the old volume for the server's claim. This clears the reference to the deleted claim and leaves the volume `Available` for a claim with this name only:

   ```bash theme={null}
   ZK=<zookeeper_pod_name>
   OLD_PV=<old_persistentvolume_name>
   CLAIM=data-$ZK
   kubectl patch pv $OLD_PV --type=merge \
     -p "{\"spec\":{\"claimRef\":{\"namespace\":\"$NS\",\"name\":\"$CLAIM\",\"uid\":null,\"resourceVersion\":null}}}"
   kubectl get pv $OLD_PV -o custom-columns=STATUS:.status.phase,CLAIM:.spec.claimRef.name
   ```

   The volume shows `Available` with your claim name. If the server also has a `data-log` volume, repeat this step for it with `CLAIM=data-log-$ZK`.

   The recreated claim binds to the reserved volume even though the claim template now names the target StorageClass. If the claim stays `Pending` instead, set `storageClassName` back to the original class as in Step 2, then repeat steps 3 and 4.

2. Make sure the old volume's location has a schedulable node with room for the pod.

3. Delete the current claims and the pod:

   ```bash theme={null}
   kubectl -n $NS delete pvc --wait=false $(kubectl -n $NS get pod $ZK -o jsonpath='{.spec.volumes[*].persistentVolumeClaim.claimName}')
   kubectl -n $NS delete pod $ZK
   ```

4. Confirm that the recreated claim is bound to the old volume and the pod runs in the old location:

   ```bash theme={null}
   kubectl -n $NS get pvc $CLAIM -o custom-columns=NAME:.metadata.name,STATUS:.status.phase,VOLUME:.spec.volumeName
   kubectl -n $NS wait --for=condition=Ready pod/$ZK --timeout=15m
   kubectl -n $NS get pod $ZK -o wide
   ```

5. Wait until the leader reports all followers synced, as in [Confirm the server rejoined and fully synced](#confirm-the-server-rejoined-and-fully-synced). The pod can report Ready several seconds before the server has rejoined the quorum.

## Clean up retained volumes

If your StorageClasses use reclaim policy `Retain`, every replaced ZooKeeper server and bookie leaves its old volumes behind in the `Released` phase. They keep incurring cost until you delete them.

```bash theme={null}
kubectl get pv --sort-by=.metadata.creationTimestamp \
  -o custom-columns=NAME:.metadata.name,STATUS:.status.phase,CLAIM:.spec.claimRef.name,SC:.spec.storageClassName
```

Once the cluster has run cleanly for a while, confirm that each `Released` volume belongs to a pod you replaced. Then delete the PersistentVolume and the cloud disk that backs it.

## Related documentation

* [Configure Pod Scheduling](https://docs.streamnative.io/private-cloud/v1/operating-streamnative-platform/advanced/pod-scheduling.md)
* [Scale Pulsar clusters](https://docs.streamnative.io/private-cloud/v1/operating-streamnative-platform/maintenance/scale-cluster.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.