Wednesday, August 6, 2025

Deploy ClickHouse on Kubernetes without Operator


 


Clickhouse is a fast SQL based database. To deploy it we can use the clickhouse operator. This however is an overkill for a small deployment such as a deployment for local testing and debug.

This post describes deployment of a single pod clickhouse without the need of an operator.


We start by creating a role and a service account.


---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: my-clickhouse-role
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: my-clickhouse-service-account
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: my-clickhouse-role-binding
subjects:
- kind: ServiceAccount
name: my-clickhouse-service-account
namespace: default
roleRef:
kind: ClusterRole
name: my-clickhouse-role
apiGroup: rbac.authorization.k8s.io



Next we add the a config map that includes the required configuration files.


apiVersion: v1
kind: ConfigMap
metadata:
name: my-clickhouse-config


data:
custom.xml: |-
<clickhouse>
<logger>
<level>information</level>
<console>true</console>
</logger>
</clickhouse>
users.xml: |-
<clickhouse>
<users>
<default>
<password></password>
<networks>
<ip>::/0</ip>
</networks>
</default>
</users>
</clickhouse>


To make the tables persistent we add a persistent volume to hostpath.


apiVersion: v1
kind: PersistentVolume
metadata:
name: my-clickhouse-pv-0
labels:
type: local
spec:
storageClassName: hostpath-my-clickhouse
capacity:
storage: 100Mi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/hostpath/my-clickhouse"


We expose the clickhouse port as well as the HTTP based port in a service.

apiVersion: v1
kind: Service
metadata:
name: my-clickhouse-service
spec:
selector:
configid: my-clickhouse-container
type: ClusterIP
ports:
- name: native
port: 9000
targetPort: 9000
- name: http
port: 8123
targetPort: 8123


And finally we add the statefulset to run the clickhouse.


apiVersion: apps/v1
kind: StatefulSet
metadata:
name: my-clickhouse-statefulset
spec:
serviceName: my-clickhouse-service
replicas: 1
selector:
matchLabels:
configid: my-clickhouse-container
template:
metadata:
labels:
configid: my-clickhouse-container


spec:
serviceAccountName: my-clickhouse-service-account
containers:
- name: my-clickhouse
image: clickhouse/clickhouse-server:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9000
name: native
- containerPort: 8123
name: http
volumeMounts:
- name: pvc
mountPath: /var/lib/clickhouse
- name: my-clickhouse-config
mountPath: /etc/clickhouse-server/config.d/custom.xml
subPath: custom.xml
- name: my-clickhouse-config
mountPath: /etc/clickhouse-server/users.d/users.xml
subPath: users.xml
volumes:
- name: my-clickhouse-config
configMap:
name: my-clickhouse-config
volumeClaimTemplates:
- metadata:
name: pvc
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "hostpath-my-clickhouse"
resources:
requests:
storage: 100Mi


No comments:

Post a Comment