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: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:| Feature | Description |
|---|---|
| minikube | A tool commonly used to run a Kubernetes cluster on a local workstation. |
| pod | A 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. |
| StatefulSet | A 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 volume | A 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 claim | When e pod is created, it requests a persistent volume claim to claim durable storage for its node. |
Step 1. Start Kubernetes
-
Follow the Minikube documentation to install the latest version of
minikube, a hypervisor, and thekubectlcommand-line tool. -
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.-
Install the Helm client (version 3.0 or higher) and add the
cockroachdbchart repository: -
Update your Helm chart repositories to ensure that you’re using the latest CockroachDB chart:
- 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.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.
-
Confirm that CockroachDB cluster initialization has completed successfully, with the pods for CockroachDB showing
1/1underREADYand the pod for initialization showingCOMPLETEDunderSTATUS: -
Confirm that the persistent volumes and corresponding claims were created successfully for all three pods:
Step 3. Use the built-in SQL client
-
Launch a temporary interactive pod and start the inside it:
-
Run some basic :
-
Exit the SQL shell and delete the temporary pod:
Step 4. Access the DB Console
To access the cluster’s :-
In a new terminal window, port-forward from your local machine to the
cockroachdb-publicservice:$ 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.- Go to http://localhost:8080.
- 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
bankis listed.
Step 5. Simulate node failure
Based on thereplicas: 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:
-
Terminate one of the CockroachDB nodes:
$ kubectl delete pod cockroachdb-2 ``pod "cockroachdb-2" deleted - 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.
-
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
-
Use the
kubectl scalecommand to add a pod for another CockroachDB node: -
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 thespec.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 .
-
Launch a temporary interactive pod and use the
cockroach node statuscommand to get the internal IDs of nodes: -
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.
stderr as it changes:
-
Once the node has been decommissioned, remove a pod from your StatefulSet:
-
From your local workstation, use our file to create the StatefulSet that automatically creates 3 pods, each with a CockroachDB node running inside it:
-
Confirm that three pods are
Runningsuccessfully. Note that they will not be consideredReadyuntil after the cluster has been initialized: -
Confirm that the persistent volumes and corresponding claims were created successfully for all three pods:
-
Use our
cluster-init.yamlfile to perform a one-time initialization that joins the CockroachDB nodes into a single cluster: -
Confirm that cluster initialization has completed successfully. The job should be considered successful and the Kubernetes pods should soon be considered
Ready:
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
-
Launch a temporary interactive pod and start the inside it:
-
Run some basic :
-
Exit the SQL shell and delete the temporary pod:
Step 4. Access the DB Console
To access the cluster’s :-
In a new terminal window, port-forward from your local machine to the
cockroachdb-publicservice:$ kubectl port-forward service/cockroachdb-public 8080Theport-forwardcommand 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 configuringkubectllocally and running the aboveport-forwardcommand on your local machine. - Go to http://localhost:8080.
-
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
bankis listed.
Step 5. Simulate node failure
Based on thereplicas: 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:
-
Terminate one of the CockroachDB nodes:
$ kubectl delete pod cockroachdb-2 ``pod "cockroachdb-2" deleted - 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.
-
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
-
Use the
kubectl scalecommand to add a pod for another CockroachDB node: -
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 thespec.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 .
-
Launch a temporary interactive pod and use the
cockroach node statuscommand to get the internal IDs of nodes: -
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 tostderras it changes:Once the node has been fully decommissioned, you’ll see a confirmation: -
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 stopcommand. This shuts down the minikube virtual machine but preserves all the resources you created:You can restore the cluster to its previous state withminikube start. -
If you do not plan to restart the cluster, use the
minikube deletecommand. This shuts down and deletes the minikube virtual machine and all the resources you created, including persistent volumes:

