Skip to main content
On top of CockroachDB’s built-in automation, you can use a third-party system to simplify and automate even more of your operations, from deployment to scaling to overall cluster management. This page demonstrates a basic integration with the open-source Kubernetes orchestration system. Using either the CockroachDB Helm chart or a few configuration files, you’ll quickly create a 3-node local cluster. You’ll run some SQL commands against the cluster and then simulate node failure, watching how Kubernetes auto-restarts without the need for any manual intervention. You’ll then scale the cluster with a single command before shutting the cluster down, again with a single command.
To orchestrate a physically distributed cluster in production, see . To deploy a 30-day free CockroachDB Dedicated cluster instead of running CockroachDB yourself, see the .

Limitations

Kubernetes version

To deploy CockroachDB v23.2, Kubernetes 1.18 or higher is required. Cockroach Labs strongly recommends that you use a Kubernetes version that is eligible for patch support by the Kubernetes project.

Kubernetes Operator

  • The CockroachDB Kubernetes Operator currently deploys clusters in a single region. For multi-region deployments using manual configs, see .
  • Using the Operator, you can give a new cluster an arbitrary number of labels. However, a cluster’s labels cannot be modified after it is deployed. To track the status of this limitation, refer to #993 in the Operator project’s issue tracker.

Helm version

The CockroachDB Helm chart requires Helm 3.0 or higher. If you attempt to use an incompatible Helm version, an error like the following occurs:
The CockroachDB Helm chart is currently not under active development, and no new features are planned. However, Cockroach Labs remains committed to fully supporting the Helm chart by addressing defects, providing security patches, and addressing breaking changes due to deprecations in Kubernetes APIs. A deprecation notice for the Helm chart will be provided to customers a minimum of 6 months in advance of actual deprecation.

Network

Service Name Indication (SNI) is an extension to the TLS protocol which allows a client to indicate which hostname it is attempting to connect to at the start of the TCP handshake process. The server can present multiple certificates on the same IP address and TCP port number, and one server can serve multiple secure websites or API services even if they use different certificates. Due to its order of operations, the PostgreSQL wire protocol’s implementation of TLS is not compatible with SNI-based routing in the Kubernetes ingress controller. Instead, use a TCP load balancer for CockroachDB that is not shared with other services.

Resources

When starting Kubernetes, select machines with at least 4 vCPUs and 16 GiB of memory, and provision at least 2 vCPUs and 8 Gi of memory to CockroachDB per pod. These minimum settings are used by default in this deployment guide, and are appropriate for testing purposes only. On a production deployment, you should adjust the resource settings for your workload. For details, see .

Storage

Kubernetes deployments use external persistent volumes that are often replicated by the provider. CockroachDB replicates data automatically, and this redundant layer of replication can impact performance. Using local volumes may improve performance.

Before you begin

Before getting started, it’s helpful to review some Kubernetes-specific terminology:
FeatureDescription
minikubeA tool commonly used to run a Kubernetes cluster on a local workstation.
podA pod is a group of one of more containers managed by Kubernetes. In this tutorial, all pods run on your local workstation. Each pod contains a single container that runs a single-node CockroachDB cluster. You’ll start with 3 pods and grow to 4.
StatefulSetA StatefulSet is a group of pods treated as stateful units, where each pod has distinguishable network identity and always binds back to the same persistent storage on restart.
persistent volumeA persistent volume is storage mounted in a pod and available to its containers. The lifetime of a persistent volume is decoupled from the lifetime of the pod that’s using it, ensuring that each CockroachDB node binds back to the same storage on restart. When using minikube, persistent volumes are external temporary directories that endure until they are manually deleted or until the entire Kubernetes cluster is deleted.
persistent volume claimWhen e pod is created, it requests a persistent volume claim to claim durable storage for its node.

Step 1. Start Kubernetes

  1. Follow the Minikube documentation to install the latest version of minikube, a hypervisor, and the kubectl command-line tool.
  2. Start a local Kubernetes cluster:

Step 2. Start CockroachDB

To start your CockroachDB cluster, you can either use our StatefulSet configuration and related files directly, or you can use the Helm package manager for Kubernetes to simplify the process.
  1. Install the Helm client (version 3.0 or higher) and add the cockroachdb chart repository:
  2. Update your Helm chart repositories to ensure that you’re using the latest CockroachDB chart:
  3. Install the CockroachDB Helm chart. Provide a “release” name to identify and track this particular deployment of the chart.
This tutorial uses my-release as the release name. If you use a different value, be sure to adjust the release name in subsequent commands.
Behind the scenes, this command uses our cockroachdb-statefulset.yaml file to create the StatefulSet that automatically creates 3 pods, each with a CockroachDB node running inside it, where each pod has distinguishable network identity and always binds back to the same persistent storage on restart.
  1. Confirm that CockroachDB cluster initialization has completed successfully, with the pods for CockroachDB showing 1/1 under READY and the pod for initialization showing COMPLETED under STATUS:
  2. Confirm that the persistent volumes and corresponding claims were created successfully for all three pods:
The StatefulSet configuration sets all CockroachDB nodes to log to stderr, so if you ever need access to a pod/node’s logs to troubleshoot, use kubectl logs <podname> rather than checking the log on the persistent volume.

Step 3. Use the built-in SQL client

  1. Launch a temporary interactive pod and start the inside it:
  2. Run some basic :
  3. Exit the SQL shell and delete the temporary pod:

Step 4. Access the DB Console

To access the cluster’s :
  1. In a new terminal window, port-forward from your local machine to the cockroachdb-public service: $ kubectl port-forward service/cockroachdb-public 8080
The port-forward command must be run on the same machine as the web browser in which you want to view the DB Console. If you have been running these commands from a cloud instance or other non-local shell, you will not be able to view the UI without configuring kubectl locally and running the above port-forward command on your local machine.
  1. Go to http://localhost:8080.
  2. In the UI, verify that the cluster is running as expected:
    • View the to ensure that all nodes successfully joined the cluster.
    • Click the Databases tab on the left to verify that bank is listed.

Step 5. Simulate node failure

Based on the replicas: 3 line in the StatefulSet configuration, Kubernetes ensures that three pods/nodes are running at all times. When a pod/node fails, Kubernetes automatically creates another pod/node with the same network identity and persistent storage. To see this in action:
  1. Terminate one of the CockroachDB nodes: $ kubectl delete pod cockroachdb-2 ``pod "cockroachdb-2" deleted
  2. In the DB Console, the Cluster Overview will soon show one node as Suspect. As Kubernetes auto-restarts the node, watch how the node once again becomes healthy.
  3. Back in the terminal, verify that the pod was automatically restarted: $ kubectl get pod cockroachdb-2 ``NAME READY STATUS RESTARTS AGE cockroachdb-2 1/1 Running 0 12s

Step 6. Add nodes

  1. Use the kubectl scale command to add a pod for another CockroachDB node:
  2. Verify that the pod for a fourth node, cockroachdb-3, was added successfully:

Step 7. Remove nodes

To safely remove a node from your cluster, you must first decommission the node and only then adjust the spec.replicas value of your StatefulSet configuration to permanently remove it. This sequence is important because the decommissioning process 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 .
  1. Launch a temporary interactive pod and use the cockroach node status command to get the internal IDs of nodes:
  2. Note the ID of the node with the highest number in its address (in this case, the address including cockroachdb-3) and use the command to decommission it:
It’s important to decommission the node with the highest number in its address because, when you reduce the replica count, Kubernetes will remove the pod for that node.
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, remove a pod from your StatefulSet:
  2. From your local workstation, use our file to create the StatefulSet that automatically creates 3 pods, each with a CockroachDB node running inside it:
  3. Confirm that three pods are Running successfully. Note that they will not be considered Ready until after the cluster has been initialized:
  4. Confirm that the persistent volumes and corresponding claims were created successfully for all three pods:
  5. Use our cluster-init.yaml file to perform a one-time initialization that joins the CockroachDB nodes into a single cluster:
  6. Confirm that cluster initialization has completed successfully. The job should be considered successful and the Kubernetes pods should soon be considered Ready:
The StatefulSet configuration sets all CockroachDB nodes to log to stderr, so if you ever need access to a pod/node’s logs to troubleshoot, use kubectl logs <podname> rather than checking the log on the persistent volume.

Step 3. Use the built-in SQL client

  1. Launch a temporary interactive pod and start the inside it:
  2. Run some basic :
  3. Exit the SQL shell and delete the temporary pod:

Step 4. Access the DB Console

To access the cluster’s :
  1. In a new terminal window, port-forward from your local machine to the cockroachdb-public service: $ kubectl port-forward service/cockroachdb-public 8080
    The port-forward command must be run on the same machine as the web browser in which you want to view the DB Console. If you have been running these commands from a cloud instance or other non-local shell, you will not be able to view the UI without configuring kubectl locally and running the above port-forward command on your local machine.
  2. Go to http://localhost:8080.
  3. In the UI, verify that the cluster is running as expected:
    • View the to ensure that all nodes successfully joined the cluster.
    • Click the Databases tab on the left to verify that bank is listed.

Step 5. Simulate node failure

Based on the replicas: 3 line in the StatefulSet configuration, Kubernetes ensures that three pods/nodes are running at all times. When a pod/node fails, Kubernetes automatically creates another pod/node with the same network identity and persistent storage. To see this in action:
  1. Terminate one of the CockroachDB nodes: $ kubectl delete pod cockroachdb-2 ``pod "cockroachdb-2" deleted
  2. In the DB Console, the Cluster Overview will soon show one node as Suspect. As Kubernetes auto-restarts the node, watch how the node once again becomes healthy.
  3. Back in the terminal, verify that the pod was automatically restarted: $ kubectl get pod cockroachdb-2 ``NAME READY STATUS RESTARTS AGE cockroachdb-2 1/1 Running 0 12s

Step 6. Add nodes

  1. Use the kubectl scale command to add a pod for another CockroachDB node:
  2. Verify that the pod for a fourth node, cockroachdb-3, was added successfully:

Step 7. Remove nodes

To safely remove a node from your cluster, you must first decommission the node and only then adjust the spec.replicas value of your StatefulSet configuration to permanently remove it. This sequence is important because the decommissioning process 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 .
  1. Launch a temporary interactive pod and use the cockroach node status command to get the internal IDs of nodes:
  2. Note the ID of the node with the highest number in its address (in this case, the address including cockroachdb-3) and use the command to decommission it: It’s important to decommission the node with the highest number in its address because, when you reduce the replica count, Kubernetes will remove the pod for that node.
    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, remove a pod from your StatefulSet:

Step 8. Stop the cluster

  • If you plan to restart the cluster, use the minikube stop command. This shuts down the minikube virtual machine but preserves all the resources you created:
    You can restore the cluster to its previous state with minikube start.
  • If you do not plan to restart the cluster, use the minikube delete command. This shuts down and deletes the minikube virtual machine and all the resources you created, including persistent volumes:
To retain logs, copy them from each pod’s stderr before deleting the cluster and all its resources. To access a pod’s standard error stream, run kubectl logs <podname>.

See also

Explore other CockroachDB benefits and features:
You might also want to learn how to .