- Manual StatefulSet configuration
- Helm package manager for Kubernetes
The
--insecure flag used in this tutorial is intended for non-production testing only. To run CockroachDB in production, use a secure cluster instead.Before you begin
Before getting started, it’s helpful to review some Kubernetes-specific terminology and current limitations.Kubernetes terminology
| Feature | Description |
|---|---|
| node | A physical or virtual machine. In this tutorial, you’ll create GCE or AWS instances and join them as worker nodes into a single Kubernetes cluster from your local workstation. |
| pod | A pod is a group of one or more Docker containers. In this tutorial, each pod will run on a separate Kubernetes node and include one Docker container running a single CockroachDB node. 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. StatefulSets are considered stable as of Kubernetes version 1.9 after reaching beta in version 1.5. |
| persistent volumes | A persistent volume is a piece of networked storage (Persistent Disk on GCE, Elastic Block Store on AWS) mounted into a pod. 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. This tutorial assumes that dynamic volume provisioning is available. When that is not the case, persistent volume claims need to be created manually. |
Best practices
Kubernetes version
To deploy CockroachDB v25.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.Public operator
- The Public operator deploys clusters in a single region. For multi-region deployments using manual configs, Cockroach Labs recommends using the which is designed to support multi-region deployments. For guidance on how to force multi-region support with the Public operator, see .
- Using the Public 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 Public 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.Step 1. Start Kubernetes
You can use the hosted Google Kubernetes Engine (GKE) service or the hosted Amazon Elastic Kubernetes Service (EKS) to quickly start Kubernetes.GKE or EKS are not required to run CockroachDB on Kubernetes. A manual GCE or AWS cluster with the minimum recommended Kubernetes version and at least 3 pods, each presenting sufficient resources to start a CockroachDB node, can also be used.
Hosted GKE
-
Complete the Before You Begin steps described in the Google Kubernetes Engine Quickstart documentation.
This includes installing
gcloud, which is used to create and delete Kubernetes Engine clusters, andkubectl, which is the command-line tool used to manage Kubernetes from your workstation.
- From your local workstation, start the Kubernetes cluster, specifying one of the available regions (e.g.,
us-east1):
cockroachdb. The --region flag specifies a regional three-zone cluster, and --num-nodes specifies one Kubernetes worker node in each zone.
The --machine-type flag tells the node pool to use the n2-standard-4 machine type (4 vCPUs, 16 GB memory), which meets our .
The process can take a few minutes, so do not move on to the next step until you see a Creating cluster cockroachdb...done message and details about your cluster.
-
Get the email address associated with your Google Cloud account:
This command returns your email address in all lowercase. However, in the next step, you must enter the address using the accurate capitalization. For example, if your address is YourName@example.com, you must use YourName@example.com and not yourname@example.com.
-
Create the RBAC roles CockroachDB needs for running on GKE, using the address from the previous step:
Hosted EKS
-
Complete the steps described in the EKS Getting Started documentation.
This includes installing and configuring the AWS CLI and
eksctl, which is the command-line tool used to create and delete Kubernetes clusters on EKS, andkubectl, which is the command-line tool used to manage Kubernetes from your workstation.
If you are running EKS-Anywhere, CockroachDB requires that you configure your default storage class to auto-provision persistent volumes. Alternatively, you can define a custom storage configuration as required by your install pattern.
- From your local workstation, start the Kubernetes cluster:
cockroachdb. The --node-type flag tells the node pool to use the m5.xlarge instance type (4 vCPUs, 16 GB memory), which meets our .
Cluster provisioning usually takes between 10 and 15 minutes. Do not move on to the next step until you see a message like [✔] EKS cluster "cockroachdb" in "us-east-1" region is ready and details about your cluster.
- Open the AWS CloudFormation console to verify that the stacks
eksctl-cockroachdb-clusterandeksctl-cockroachdb-nodegroup-standard-workerswere successfully created. Be sure that your region is selected in the console.
Step 2. Start CockroachDB
Choose how you want to deploy and maintain the CockroachDB cluster.-
From your local workstation, use our file to create the StatefulSet that automatically creates 3 pods, each with a CockroachDB node running inside it.
Download :
By default, this manifest specifies CPU and memory resources that are appropriate for the virtual machines used in this deployment example. On a production cluster, you should substitute values that are appropriate for your machines and workload. For details on configuring your deployment, see .
-
Download our :
-
Modify the file wherever there is a
TODOcomment. -
Use the file to create the StatefulSet and start the cluster:
-
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:
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. Stop the cluster
This procedure shuts down the CockroachDB cluster and deletes the resources you just created, including the logs. This command intentionally does not delete the persistent volumes that were attached to the pods or any secrets you may have created. Before deleting a cluster’s persistent volumes, be sure to back them up. For more information, refer to Delete a Cluster’s Persistent Volumes and Managing Secrets Usingkubectl in the Kubernetes project’s documentation.
Do not use the
--all flag to kubectl delete, to avoid the risk of unintentional data loss.-
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:
-
Modify our Helm chart’s
values.yamlparameters for your deployment scenario. Create amy-values.yamlfile to override the defaults invalues.yaml, substituting your own values in this example based on the guidelines below.- To avoid running out of memory when CockroachDB is not the only pod on a Kubernetes node, you must set memory limits explicitly. This is because CockroachDB does not detect the amount of memory allocated to its pod when run in Kubernetes. We recommend setting
conf.cacheandconf.max-sql-memoryeach to 1/4 of thememoryallocation specified instatefulset.resources.requestsandstatefulset.resources.limits.
- To avoid running out of memory when CockroachDB is not the only pod on a Kubernetes node, you must set memory limits explicitly. This is because CockroachDB does not detect the amount of memory allocated to its pod when run in Kubernetes. We recommend setting
-
For an insecure deployment, set
tls.enabledtofalse. For clarity, this example includes the example configuration from the previous steps.- You may want to modify
storage.persistentVolume.sizeandstorage.persistentVolume.storageClassfor your use case. This chart defaults to 100Gi of disk space per pod. For more details on customizing disks for performance, see .
- You may want to modify
If necessary, you can after the cluster is live.
-
Install the CockroachDB Helm chart.
Provide a “release” name to identify and track this particular deployment of the chart, and override the default values with those in
my-values.yaml.
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:
stderr, so if you ever need access to logs for a pod, 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. Stop the cluster
This procedure shuts down the CockroachDB cluster and deletes the resources you just created, including the logs. This command intentionally does not delete the persistent volumes that were attached to the pods or any secrets you may have created. Before deleting a cluster’s persistent volumes, be sure to back them up. For more information, refer to Delete a Cluster’s Persistent Volumes and Managing Secrets Usingkubectl in the Kubernetes project’s documentation.
Delete a cluster’s persistent volumes
If you need to free up the storage used by CockroachDB, you can optionally delete the persistent volumes that were attached to the pods, after first backing up your data.Before you delete a cluster’s 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.
Stop Kubernetes
To delete the Kubernetes cluster:-
Hosted GKE:
-
Hosted EKS:
-
Manual GCE:
-
Manual AWS:

