> ## 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 a running Pulsar cluster's ZooKeeper and BookKeeper pods 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

This page describes the parts that are specific to relocation: steering where new volumes are created, and the order of operations. The per-pod replacement itself follows [Replace ZooKeeper storage](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-zookeeper-storage.md) and [Replace BookKeeper storage](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-bookkeeper-storage.md).

> [!NOTE]
>
> This page applies to clusters you manage through the `ZooKeeperCluster`, `BookKeeperCluster`, `PulsarBroker`, and `PulsarProxy` resources. If you deploy with the `sn-platform` or `sn-platform-slim` Helm chart, follow [Relocate ZooKeeper and BookKeeper pods](https://docs.streamnative.io/private-cloud/v1/operating-streamnative-platform/maintenance/relocate-zookeeper-bookkeeper.md) for Private Cloud v1 instead.

## 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.
* **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

* Everything listed in the prerequisites of [Replace ZooKeeper storage](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-zookeeper-storage.md#prerequisites) and [Replace BookKeeper storage](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-bookkeeper-storage.md#prerequisites)
* Permission to create a StorageClass, and to edit the `ZooKeeperCluster`, `BookKeeperCluster`, `PulsarBroker`, and `PulsarProxy` resources

### Inspect your environment first

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

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

kubectl -n <namespace> get pods -l cloud.streamnative.io/cluster=<cluster> -o wide

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

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 <namespace> get pvc -l cloud.streamnative.io/cluster=<cluster> \
  -o custom-columns=NAME:.metadata.name,STORAGECLASS:.spec.storageClassName
kubectl get storageclass -o custom-columns=NAME:.metadata.name,PROVISIONER:.provisioner,BINDING:.volumeBindingMode,RECLAIM:.reclaimPolicy
```

**3. That the target location has enough nodes.** If your components use required pod anti-affinity on `kubernetes.io/hostname` (`requiredDuringSchedulingIgnoredDuringExecution`), each node runs at most one ZooKeeper server, one bookie, and one broker. The target location needs at least as many schedulable nodes as the largest of the ZooKeeper replica count, the bookie replica count, and the broker replica count (the broker HPA maximum, 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 configured `spec.config.rackAwareTopologyLabels` on the `BookKeeperCluster` with the zone label and set `bookkeeperClientEnforceMinNumRacksPerWriteQuorum: "true"` on the brokers, the brokers refuse to create ledgers once fewer racks are available than `bookkeeperClientMinNumRacksPerWriteQuorum`. Before consolidating zones, set `bookkeeperClientEnforceMinNumRacksPerWriteQuorum` to `"false"` on the `PulsarBroker`, 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 must be Running and Ready, ZooKeeper must have a leader, and BookKeeper must report no under-replicated ledgers, as described in the "Inspect your environment first" sections of the two storage pages.

## 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>
```

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 on the `ZooKeeperCluster` and the `BookKeeperCluster`, as described in Option A of [Replace ZooKeeper storage](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-zookeeper-storage.md#option-a-change-the-storageclass-on-the-zookeepercluster) and [Replace BookKeeper storage](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-bookkeeper-storage.md#option-a-change-the-storageclass-on-the-bookkeepercluster):

```yaml theme={null}
# ZooKeeperCluster
spec:
  persistence:
    data:
      storageClassName: <target_storageclass>
    dataLog:  # only if the cluster has a dedicated transaction log volume
      storageClassName: <target_storageclass>
```

```yaml theme={null}
# BookKeeperCluster
spec:
  storage:
    journal:
      volumeClaimTemplate:
        storageClassName: <target_storageclass>
    ledger:
      volumeClaimTemplate:
        storageClassName: <target_storageclass>
```

The operator replaces the 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 <namespace> 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 `spec.pod.nodeSelector` to the `BookKeeperCluster`. 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. Each change rolls the component's pods, and they come back in the target location.

```yaml theme={null}
# PulsarBroker and PulsarProxy
spec:
  pod:
    nodeSelector:
      topology.kubernetes.io/zone: <target-zone>
```

```yaml theme={null}
# BookKeeperCluster: AutoRecovery only, this does not restart bookies
spec:
  autoRecovery:
    pod:
      nodeSelector:
        topology.kubernetes.io/zone: <target-zone>
```

If the broker and proxy pods carry a preferred pod anti-affinity term on `topology.kubernetes.io/zone`, remove it from `spec.pod.affinity` at the same time. The scheduler cannot satisfy it in a single zone.

Do not add a `nodeSelector` to the `ZooKeeperCluster` or to the bookies. The StorageClass from Step 1 already keeps relocated ZooKeeper servers and bookies in the target location.

Verify before continuing:

```bash theme={null}
kubectl -n <namespace> get pods -l cloud.streamnative.io/cluster=<cluster>,cloud.streamnative.io/component=broker -o wide
```

All brokers are Ready and running on nodes in the target location.

## Step 4: Relocate ZooKeeper servers

For each ZooKeeper server whose volumes are outside the target location, follow [Step 2 of Replace ZooKeeper storage](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-zookeeper-storage.md#step-2-replace-each-servers-volumes). Skip Step 1 of that page; Step 2 on this page already did it.

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

* Process followers first and the leader last.
* Delete the `data` and `data-log` claims of a server together.
* Do not move to the next server until the leader reports all followers synced.

Servers that are already in the target location need no action.

To keep each 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` before deleting the claims, and note their names:

```bash theme={null}
NS=<namespace>
ZK=<zookeeper_pod_name>
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
```

## Step 5: Relocate bookies

For each bookie whose volumes are outside the target location, follow [Step 2 of Replace BookKeeper storage](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-bookkeeper-storage.md#step-2-decommission-each-bookie-in-turn). Skip Step 1 of that page; Step 2 on this page already did it.

The operator re-replicates the bookie's ledgers to the remaining bookies, deletes the old claims, and recreates the pod in the target location with new volumes.

* Process one bookie at a time, from the highest ordinal down.
* Do not move to the next bookie until the replacement is Ready and writable and `listunderreplicated` reports nothing.

Bookies that are already in the target location need no action.

## 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 | Set the original `storageClassName` back on the `ZooKeeperCluster` and `BookKeeperCluster`. The operator replaces the StatefulSets again without restarting pods. |
| Step 3 | Revert the `nodeSelector` changes. 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. Follow [the ZooKeeper troubleshooting section](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-zookeeper-storage.md#troubleshooting). 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, then follow [the BookKeeper troubleshooting section](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-bookkeeper-storage.md#a-replacement-pod-stays-pending). |
| 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. Follow the cleanup sections of [Replace ZooKeeper storage](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-zookeeper-storage.md#clean-up-retained-volumes) and [Replace BookKeeper storage](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-bookkeeper-storage.md#clean-up-retained-volumes) once the cluster has run cleanly for a while.

## Related documentation

* [Replace ZooKeeper storage](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-zookeeper-storage.md)
* [Replace BookKeeper storage](https://docs.streamnative.io/private-cloud/v2/operate-private-cloud/storage/replace-bookkeeper-storage.md)
* [Configure storage](https://docs.streamnative.io/private-cloud/v2/configure-private-cloud/storage/private-cloud-storage.md)


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