
ClickHouse is an open-source, column-oriented database management system designed for Online Analytical Processing (OLAP) workloads. It runs complex analytical queries across large datasets with sub-second response times. The official ClickHouse Kubernetes Operator extends the Kubernetes API to manage ClickHouse clusters using Custom Resource Definitions (CRDs), automating setup, scaling, and upgrades.
This article outlines the deployment of a ClickHouse cluster on Kubernetes using the official ClickHouse Operator. It covers the installation of the operator and its dependencies, deployment of a multi-replica cluster with ClickHouse Keeper for coordination, internal and external connectivity, and scaling considerations.
Before you begin, you need to:
The deployment consists of three components managed by the ClickHouse Operator:
The ClickHouse Operator watches for Custom Resource Definitions and keeps the desired state by creating StatefulSets, Services, and storage as needed.
The ClickHouse Operator manages the creation, updates, and removal of ClickHouse and Keeper clusters. It needs cert-manager to issue TLS certificates for its webhooks before the operator can start.
Install cert-manager using the Helm chart. cert-manager issues TLS certificates for the operator's webhooks, keeping communication between the operator and the Kubernetes API server secure.
Verify that the cert-manager pods enter the Running state.
Install the ClickHouse Operator using the official Helm chart. The operator watches for ClickHouse Custom Resources and keeps the desired state by creating Kubernetes objects such as StatefulSets, Services, and ConfigMaps.
Verify that the operator deployment is available.
ClickHouse uses a two-layer setup on Kubernetes. ClickHouse Keeper handles coordination for data replication and cluster management, and must be deployed before the ClickHouse cluster. The ClickHouse Operator manages both as separate Custom Resources, so each can be scaled and managed on its own.
Create a dedicated namespace for the ClickHouse deployment.
ClickHouse Keeper runs as a group of three nodes. Each node saves coordination data to block storage. The podAntiAffinity rule spreads the nodes across different machines so the cluster keeps working if one node fails.
Create the Keeper cluster manifest file.
Add the following configuration. Adjust the storage value to meet your platform's minimum volume size requirements.
This configuration uses your cluster's default StorageClass. If your cluster does not have a default, specify a storage class explicitly under dataVolumeClaimSpec.
The podAntiAffinity rule stops Kubernetes from placing multiple Keeper nodes on the same machine, so the cluster keeps working if a node fails.
Save and close the file.
Apply the Keeper cluster manifest.
Wait for the Keeper pods to become ready.
The ClickHouseCluster Custom Resource defines the desired state of the database cluster, including the number of replicas, storage settings, and the Keeper reference. The operator creates a StatefulSet for each replica and sets up macros such as {shard} and {replica} on each pod. These macros are required for tables that replicate data across pods.
Generate a secure password for the default ClickHouse user and store it in a Kubernetes Secret.
This command generates a random password and stores it directly in the secret without showing it in the terminal output or shell history.
Create the TLS certificate manifest file. cert-manager generates the certificate and stores it in a Kubernetes Secret.
Add the following configuration. The Issuer creates a self-signed certificate authority. The Certificate defines the service names that the TLS certificate covers.
Save and close the file.
Apply the TLS certificate manifest.
Wait for the certificate to be issued.
Create the ClickHouse cluster manifest file.
Add the following configuration. Adjust the storage value to meet your platform's minimum volume size requirements.
The podAntiAffinity rule works the same way as the Keeper setup, so ClickHouse keeps handling queries if a node fails.
Save and close the file.
Apply the ClickHouse cluster manifest.
Wait for the ClickHouse pods to become ready.
After deploying both clusters, check that all components are running and that data replication works correctly across pods.
Verify the Keeper pods are running.
The output displays three pods, each in the Running state.
Verify the ClickHouse pods are running.
The output displays two pods with names following the pattern {cluster}-clickhouse-{shard}-{replica}-0, each in the Running state.
Verify the cluster status.
The output displays True in the READY column when all replicas are running.
Connect to the first ClickHouse pod using the built-in client. The following queries run within this session.
Check that the operator set up the replication macros correctly.
The output displays the shard and replica values set by the operator for this pod.
Create a replicated table to test data replication across pods.
The {shard} and {replica} macros are filled in automatically based on each pod's identity. ClickHouse Keeper stores the replication data at the specified path.
Insert sample data into the table.
Type exit to close the client session.
Verify that the data was copied to the second pod.
The output displays 3, confirming the data was copied successfully to the other pod.
The ClickHouse Operator creates a headless service named clickhouse-clickhouse-headless that exposes port 9000 for native TCP connections and port 9440 for TLS-encrypted connections. Pods within the cluster reach this service using the DNS. For access outside the cluster, a load balancer service exposes port 9440.
Pods within the cluster connect to ClickHouse using the headless service name. This is the same way that application pods connect in production.
Create a temporary pod using the ClickHouse client image and connect to the database.
The pod starts, connects to ClickHouse through the headless service, and opens an interactive session. The pod is removed automatically after the session ends.
Run a test query to verify the connection.
The output displays the ClickHouse server version, confirming a successful internal connection.
Type exit to close the session and remove the temporary pod.
The ClickHouse Operator supports scaling the cluster by changing the replica count in the ClickHouseCluster Custom Resource. The operator handles adding or removing pods and moves data as needed.
replicas field in the ClickHouseCluster manifest and apply the change. Refer to the official ClickHouse Operator documentation for scaling steps and best practices.You have deployed a ClickHouse cluster on Kubernetes using the official ClickHouse Operator. The cluster runs with two replicas managed by a three-node ClickHouse Keeper group. TLS is enabled for secure external connectivity on port 9440. For more information, refer to the official ClickHouse Operator documentation.
0 Comments
Be the first to comment and share your perspective with the community.