The CockroachDB Kubernetes Operator is also available on platforms such as and IBM Cloud Pak for Data.
- Manual StatefulSet configuration
- Helm package manager for Kubernetes
Limitations
Kubernetes version
To deploy CockroachDB v23.1, 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 kubernetes Operator
- The Public 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.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.The CockroachDB Kubernetes Operator eases CockroachDB cluster creation and management on a single Kubernetes cluster.The Operator does not provision or apply an Enterprise license key. To use CockroachDB with the Operator, in the SQL shell.
Install the Operator
-
Apply the custom resource definition (CRD) for the Operator:
-
By default, the Operator is configured to install in the
cockroach-operator-systemnamespace and to manage CockroachDB instances for all namespaces on the cluster.
- To use these defaults, apply the Operator manifest without modifying it:
- To change these defaults:
- Download the Operator manifest:
- To use a custom namespace, edit all instances of
namespace: cockroach-operator-systemwith your desired namespace. - To limit the namespaces that will be monitored, set the
WATCH\_NAMESPACEenvironment variable in theDeploymentpod spec. This can be set to a single namespace or a comma-delimited set of namespaces. When set, only thoseCrdbClusterresources in the supplied namespace(s) will be reconciled. - Apply your local version of the Operator manifest to the cluster:
- Download the Operator manifest:
-
Set your current namespace to the one used by the Operator. For example, to use the Operator’s default namespace:
-
Validate that the Operator is running:
Initialize the cluster
After a cluster managed by the Kubernetes operator is initialized, its Kubernetes labels cannot be modified. For more details, refer to Limitations.
-
Download
example.yaml, a custom resource that tells the Operator how to configure the Kubernetes cluster.By default, this custom resource 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 .
By default, the Operator will generate and sign 1 client and 1 node certificate to secure the cluster. This means that if you do not provide a CA, a
cockroach-generated CA is used. If you want to authenticate using your own CA, before proceeding to the next step.-
Apply
example.yaml:The Operator will create a StatefulSet and initialize the nodes as a cluster. -
Check that the pods were created:
Each pod should have
READYstatus soon after being created.
Configure the cluster
-
Download and modify our :
-
Update
secretNamewith the name of the corresponding node secret. The secret names depend on your method for generating secrets. For example, if you follow the below steps usingcockroach cert, use this secret name: -
The StatefulSet configuration deploys CockroachDB into the
defaultnamespace. To use a different namespace, search forkind: RoleBindingand change itssubjects.namespaceproperty to the name of the namespace. Otherwise, afailed to read secretserror occurs when you attempt to follow the steps in Initialize the cluster.
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 Configure the Cluster.
Create certificates
The below steps use to quickly generate and sign the CockroachDB node and client certificates. Read our docs to learn about other methods of signing certificates.-
Create two directories:
Directory Description certsYou’ll generate your CA certificate and all node and client certificates and keys in this directory. my-safe-directoryYou’ll generate your CA key in this directory and then reference the key when generating node and client certificates. -
Create the CA certificate and key pair:
-
Create a client certificate and key pair for the root user:
-
Upload the client certificate and key to the Kubernetes cluster as a secret:
-
Create the certificate and key pair for your CockroachDB nodes:
-
Upload the node certificate and key to the Kubernetes cluster as a secret:
-
Check that the secrets were created on the cluster:
Initialize the cluster
-
Use the config file you downloaded to create the StatefulSet that automatically creates 3 pods, each running a CockroachDB node:
-
Initialize the CockroachDB 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:
-
Run
cockroach initon one of the pods to complete the node startup process and have them join together as a cluster: -
Confirm that cluster initialization has completed successfully. The job should be considered successful and the Kubernetes pods should soon be considered
Ready:
-
Confirm that three pods are
If you are running a secure Helm deployment on Kubernetes 1.22 and later, you must migrate away from using the Kubernetes CA for cluster authentication. The recommended approach is to use
cert-manager for certificate management. For details, refer to Deploy cert-manager for mTLS.-
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:
- The cluster configuration is set in the Helm chart’s values file.
By default, the Helm chart 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 Configure the Cluster.
-
Create a local YAML file (e.g.,
my-values.yaml) to specify your custom values. These will be used to override the defaults invalues.yaml. -
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.
tls.enabled to true.
By default, the Helm chart will generate and sign 1 client and 1 node certificate to secure the cluster. To authenticate using your own CA, see .
values.yaml template.
-
Install the CockroachDB Helm chart, specifying your custom values file.
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.To allow the CockroachDB pods to successfully deploy, do not set the
--wait flag when using Helm 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
-
To use the CockroachDB SQL client, first launch a secure pod running the
cockroachbinary. -
Get a shell into the pod and start the CockroachDB :
-
Run some basic :
-
:
You will need this username and password to access the DB Console later.
-
Exit the SQL shell and pod:
-
To use the CockroachDB SQL client, first launch a secure pod running the
cockroachbinary. -
Get a shell into the pod and start the CockroachDB :
-
Run some basic :
-
:
You will need this username and password to access the DB Console later.
-
Exit the SQL shell and pod:
client-secure.yaml file to launch a pod and keep it running indefinitely.
-
Download the file:
-
In the file, set the following values:
spec.serviceAccountName: my-release-cockroachdbspec.image: cockroachdb/cockroach: {your CockroachDB version}spec.volumes[0].project.sources[0].secret.name: my-release-cockroachdb-client-secret
-
Use the file to launch a pod and keep it running indefinitely:
-
Get a shell into the pod and start the CockroachDB :
This pod will continue running indefinitely, so any time you need to reopen the built-in SQL client or run any other (e.g.,
cockroach node), repeat step 2 using the appropriatecockroachcommand. If you’d prefer to delete the pod and recreate it when needed, runkubectl delete pod cockroachdb-client-secure. -
Run some basic :
-
:
You will need this username and password to access the DB Console later.
-
Exit the SQL shell and pod:
Step 4. Access the DB Console
To access the cluster’s :-
On secure clusters, can only be accessed by
adminusers. Get a shell into the pod and start the CockroachDB :$ kubectl exec -it cockroachdb-client-secure
— ./cockroach sql
—certs-dir=/cockroach-certs
—host=my-release-cockroachdb-public -
Assign
roachto theadminrole (you only need to do this once): -
Exit the SQL shell and pod:
-
In a new terminal window, port-forward from your local machine to the
cockroachdb-publicservice: - Go to https://localhost:8080 and log in with the username and password you created earlier.
localhost because its certificate has been revoked, go to chrome://flags/#allow-insecure-localhost, enable “Allow invalid certificates for resources loaded from localhost”, and then restart the browser. Enabling this Chrome feature degrades security for all sites running on localhost, not just CockroachDB’s DB Console, so be sure to enable the feature only temporarily.
- 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. Stop the cluster
If you want to continue using this cluster, refer the documentation on , , , and the cluster.
-
Delete the previously created custom resource:
-
Remove the Operator:
This will delete the CockroachDB cluster being run by the Operator. It intentionally does not delete:
- The persistent volumes that were attached to the pods, to avoid the risk of data loss. Before deleting a cluster’s persistent volumes, be sure to back them up. For more information, refer to Delete a Cluster’s Persistent Volumes in the Kubernetes project’s documentation.
- Any secrets you may have created. For more information on managing secrets, refer to Managing Secrets Using
kubectlin the Kubernetes project’s documentation.
- The persistent volumes that were attached to the pods, to avoid the risk of data loss. Before deleting a cluster’s persistent volumes, be sure to back them up. For more information, refer to Delete a Cluster’s Persistent Volumes in the Kubernetes project’s documentation.
- Any secrets you may have created. For more information on managing secrets, refer to Managing Secrets Using
kubectlin the Kubernetes project’s documentation.
Do not use the
--all flag to kubectl delete, to avoid the risk of data loss.-
Delete the resources associated with the
cockroachdblabel, including the logs and Prometheus and Alertmanager resources. This command is very long; you may need to scroll your browser to read all of it. -
Delete the pod created for
cockroachclient commands, if you didn’t do so earlier: - Delete the cluster’s cryptographic resources.
- If your cluster’s certificates are managed using
cert-manager(recommended but not default), get the names of the cluster’sissuersand delete them: - If your cluster uses self-signed certificates (the default), get the names of any CSRs for the cluster, then delete them:
-
Uninstall the release:
-
Delete the pod created for
cockroachclient commands, if you didn’t do so earlier: - Delete the cluster’s cryptographic resources.
- If your cluster’s certificates are managed using
cert-manager(recommended but not default), get the names of the cluster’sissuersand delete them: - If your cluster uses self-signed certificates (the default), get the names of any CSRs for the cluster, then delete them:
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:
If you stop Kubernetes without first deleting the persistent volumes, they will still exist in your cloud project.

