Skip to main content
This article assumes you have already .
This page explains how to add and remove CockroachDB nodes on Kubernetes.
All kubectl steps should be performed in the . By default, this is cockroach-operator-system.
If you , substitute kubectl with oc in the following commands.

Add nodes

Before scaling up CockroachDB, note the following :
  • Each CockroachDB node (running in its own pod) should run on a separate Kubernetes worker node.
  • Each availability zone should have the same number of CockroachDB nodes.
If your cluster has 3 CockroachDB nodes distributed across 3 availability zones (as in our ), we recommend scaling up by a multiple of 3 to retain an even distribution of nodes. You should therefore scale up to a minimum of 6 CockroachDB nodes, with 2 nodes in each zone.
  1. Run kubectl get nodes to list the worker nodes in your Kubernetes cluster. There should be at least as many worker nodes as pods you plan to add. This ensures that no more than one pod will be placed on each worker node.
  2. If you need to add worker nodes, resize your GKE cluster by specifying the desired number of worker nodes in each zone:
    This example distributes 2 worker nodes across the default 3 zones, raising the total to 6 worker nodes.
  3. If you are adding nodes after previously scaling down, and have not enabled automatic PVC pruning, you must first manually delete any persistent volumes that were orphaned by node removal.
Due to a known issue, automatic pruning of PVCs is currently disabled by default. This means that after decommissioning and removing a node, the Operator will not remove the persistent volume that was mounted to its pod.
View the PVCs on the cluster:
  1. The PVC names correspond to the pods they are bound to. For example, if the pods cockroachdb-3, cockroachdb-4, and cockroachdb-5 had been removed by scaling the cluster down from 6 to 3 nodes, datadir-cockroachdb-3, datadir-cockroachdb-4, and datadir-cockroachdb-5 would be the PVCs for the orphaned persistent volumes. To verify that a PVC is not currently bound to a pod:
    The output will include the following line:
    If the PVC is bound to a pod, it will specify the pod name.
  2. Remove the orphaned persistent volumes by deleting their PVCs:
Before deleting any persistent volumes, be sure you have a backup copy of your data. Data cannot be recovered once the persistent volumes are deleted. For more information, see the Kubernetes documentation.
  1. Update nodes in the Operator’s custom resource, which you downloaded when , with the target size of the CockroachDB cluster. This value refers to the number of CockroachDB nodes, each running in one pod:
Note that you must scale by updating the nodes value in the custom resource. Using kubectl scale statefulset <cluster-name> --replicas=4 will result in new pods immediately being terminated.
  1. Apply the new settings to the cluster:
  2. Verify that the new pods were successfully started:
    Each pod should be running in one of the 6 worker nodes.
Before scaling up CockroachDB, note the following :
  • Each CockroachDB node (running in its own pod) should run on a separate Kubernetes worker node.
  • Each availability zone should have the same number of CockroachDB nodes.
If your cluster has 3 CockroachDB nodes distributed across 3 availability zones (as in our deployment example), we recommend scaling up by a multiple of 3 to retain an even distribution of nodes. You should therefore scale up to a minimum of 6 CockroachDB nodes, with 2 nodes in each zone.
  1. Run kubectl get nodes to list the worker nodes in your Kubernetes cluster. There should be at least as many worker nodes as pods you plan to add. This ensures that no more than one pod will be placed on each worker node.
  2. Add worker nodes if necessary:
  3. Edit your StatefulSet configuration to add pods for each new CockroachDB node:
  4. Verify that the new pod started successfully:
  5. You can also open the in the DB Console to ensure that the fourth node successfully joined the cluster.
Before scaling CockroachDB, ensure that your Kubernetes cluster has enough worker nodes to host the number of pods you want to add. This is to ensure that two pods are not placed on the same worker node, as recommended in our . For example, if you want to scale from 3 CockroachDB nodes to 4, your Kubernetes cluster should have at least 4 worker nodes. You can verify the size of your Kubernetes cluster by running kubectl get nodes.
  1. Edit your StatefulSet configuration to add another pod for the new CockroachDB node:
  2. Get the name of the Pending CSR for the new pod:
    If you do not see a Pending CSR, wait a minute and try again.
  3. Examine the CSR for the new pod:
  4. If everything looks correct, approve the CSR for the new pod:
  5. Verify that the new pod started successfully:
  6. You can also open the in the DB Console to ensure that the fourth node successfully joined the cluster.

Remove nodes

Do not scale down to fewer than 3 nodes. This is considered an anti-pattern on CockroachDB and will cause errors.
Due to a known issue, automatic pruning of PVCs is currently disabled by default. This means that after decommissioning and removing a node, the Operator will not remove the persistent volume that was mounted to its pod. If you plan to eventually scale up the cluster after scaling down, you will need to manually delete any PVCs that were orphaned by node removal before scaling up. For more information, see Add nodes.
If you want to enable the Operator to automatically prune PVCs when scaling down, see Automatic PVC pruning. However, note that this workflow is currently unsupported.
Before scaling down CockroachDB, note the following :
  • Each availability zone should have the same number of CockroachDB nodes.
If your nodes are distributed across 3 availability zones (as in our ), we recommend scaling down by a multiple of 3 to retain an even distribution. If your cluster has 6 CockroachDB nodes, you should therefore scale down to 3, with 1 node in each zone.
  1. Update nodes in the custom resource, which you downloaded when , with the target size of the CockroachDB cluster. For instance, to scale down to 3 nodes:
Before removing a node, the Operator first decommissions the node. This lets a node finish in-flight requests, rejects any new requests, and transfers all range replicas and range leases off the node.
  1. Apply the new settings to the cluster:
    The Operator will remove nodes from the cluster one at a time, starting from the pod with the highest number in its address.
  2. Verify that the pods were successfully removed:

Automatic PVC pruning

To enable the Operator to automatically remove persistent volumes when scaling down a cluster, turn on automatic PVC pruning through a feature gate.
This workflow is unsupported and should be enabled at your own risk.
  1. Download the Operator manifest:
  2. Uncomment the following lines in the Operator manifest:
  3. Reapply the Operator manifest:
  4. Validate that the Operator is running:
Before removing a node from your cluster, you must first decommission the node. This lets a node finish in-flight requests, rejects any new requests, and transfers all range replicas and range leases off the node.
If you remove nodes without first telling CockroachDB to decommission them, you may cause data or even cluster unavailability. For more details about how this works and what to consider before removing nodes, see Prepare for graceful shutdown.
  1. Use the command to get the internal IDs of nodes. For example, if you followed the steps in to launch a secure client pod, get a shell into the cockroachdb-client-secure pod:
    The pod uses the root client certificate created earlier to initialize the cluster, so there’s no CSR approval required.
  2. Use the command to decommission the node with the highest number in its address, specifying its ID (in this example, node ID 4 because its address is cockroachdb-3):
You must decommission the node with the highest number in its address. Kubernetes will remove the pod for the node with the highest number in its address when you reduce the replica count.
You’ll then see the decommissioning status print to stderr as it changes:
Once the node has been fully decommissioned, you’ll see a confirmation:
  1. Once the node has been decommissioned, scale down your StatefulSet:
  2. Verify that the pod was successfully removed:
  3. You should also remove the persistent volume that was mounted to the pod. Get the persistent volume claims for the volumes:
  4. Verify that the PVC with the highest number in its name is no longer mounted to a pod:
  5. Remove the persistent volume by deleting the PVC:
Before removing a node from your cluster, you must first decommission the node. This lets a node finish in-flight requests, rejects any new requests, and transfers all range replicas and range leases off the node. If you remove nodes without first telling CockroachDB to decommission them, you may cause data or even cluster unavailability. For more details about how this works and what to consider before removing nodes, see Prepare for graceful shutdown.
  1. Use the command to get the internal IDs of nodes. For example, if you followed the steps in to launch a secure client pod, get a shell into the cockroachdb-client-secure pod:
    The pod uses the root client certificate created earlier to initialize the cluster, so there’s no CSR approval required.
  2. Use the command to decommission the node with the highest number in its address, specifying its ID (in this example, node ID 4 because its address is my-release-cockroachdb-3): You must decommission the node with the highest number in its address. Kubernetes will remove the pod for the node with the highest number in its address when you reduce the replica count.
    You’ll then see the decommissioning status print to stderr as it changes:
    Once the node has been fully decommissioned, you’ll see a confirmation:
  3. Once the node has been decommissioned, scale down your StatefulSet:
  4. Verify that the pod was successfully removed:
  5. You should also remove the persistent volume that was mounted to the pod. Get the persistent volume claims for the volumes:
  6. Verify that the PVC with the highest number in its name is no longer mounted to a pod:
  7. Remove the persistent volume by deleting the PVC: