Clickhouse
title: ClickHouse production deployment description: Single-node ClickHouse on k3s for analytics: manifests, storage, users, connection settings and troubleshooting. audience: internal type: reference owner: platform-team last_reviewed: 2026-11-19
Analytics Stack — Single Node (k3s)¶
Architecture Overview¶
Your App / Services
│
│ native TCP :9000 (Go/Python/Node SDKs)
│ HTTP :8123 (Grafana, REST clients)
▼
┌─────────────────────────────────┐
│ Service: clickhouse-svc │ ← Use this in your app
│ (ClusterIP) │
└────────────────┬────────────────┘
│
┌────────────────▼────────────────┐
│ Service: clickhouse (headless) │ ← StatefulSet DNS only
└────────────────┬────────────────┘
│
┌────────────────▼────────────────┐
│ Pod: clickhouse-0 │
│ ┌───────────────────────────┐ │
│ │ init-sysctl │ │ kernel tuning (vm.max_map_count)
│ │ init-permissions │ │ chown PVC to uid 101
│ ├───────────────────────────┤ │
│ │ clickhouse-server:24.3 │ │
│ │ ports: 8123, 9000, 9009 │ │
│ │ probes: startup+ready+live│ │
│ └───────────────────────────┘ │
└────────────────┬────────────────┘
│
┌────────▼────────┐
│ PVC: 80Gi │ ← local-path-retain
│ /var/lib/ │ survives pod deletion
│ clickhouse │ node: ticket-node-3
└─────────────────┘
Files Reference¶
| File | Purpose | Status |
|---|---|---|
01-namespace.yaml |
Creates analytic namespace |
Applied |
02-secret.yaml |
Credentials for 3 users | Applied |
03-configmap.yaml |
Server tuning + user profiles | Applied |
04-statefulset.yaml |
ClickHouse pod | Applied |
05-service.yaml |
Headless + ClusterIP services | Applied |
06-migration.yaml |
Schema — access_logs + events tables | Applied |
07-backup.yaml |
Daily backup cronjob | Not applied yet |
Users & Access¶
| User | Profile | Can Do | Used By |
|---|---|---|---|
ch_admin |
default | Full access | kubectl, migrations only |
ch_app |
app | SELECT + INSERT, no DDL | Your application |
ch_grafana |
readonly | SELECT only | Grafana dashboards |
How users are created¶
ch_admin— created by the Docker entrypoint viaCLICKHOUSE_USERenv varch_app+ch_grafana— created by migration job viaCREATE USERSQL
Profiles (defined in 03-configmap.yaml)¶
| Profile | Max Memory | Max Query Time | Readonly |
|---|---|---|---|
default |
3GB | 1 hour | No |
app |
2GB | 5 min | No (DDL blocked) |
readonly |
1GB | 2 min | Yes |
Schema¶
analytics.access_logs¶
API gateway request logs (Kong/APISIX).
CREATE TABLE analytics.access_logs
(
timestamp DateTime,
route_id LowCardinality(String),
route_name LowCardinality(String),
service_id LowCardinality(String),
service_name LowCardinality(String),
consumer_name String,
status UInt16,
latency_ms Float32,
client_ip String,
method LowCardinality(String),
uri String,
host String,
user_agent String,
request_size UInt32,
response_size UInt32,
upstream_addr String,
upstream_time Float32,
request_id String
)
ENGINE = MergeTree()
PARTITION BY toYYYYMMDD(timestamp)
ORDER BY (timestamp, route_id)
TTL timestamp + INTERVAL 90 DAY
SETTINGS index_granularity = 8192;
Design decisions:
- Daily partitions (toYYYYMMDD) — fast date range queries
- TTL 90 days — auto-deletes old logs, storage stays bounded
- LowCardinality on repeated fields — 3-5x compression
analytics.events¶
Product analytics event stream.
CREATE TABLE analytics.events
(
timestamp DateTime,
event String,
user_id String,
session_id String,
device_type String,
properties String -- JSON payload
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(timestamp)
ORDER BY (event, timestamp);
Design decisions:
- Monthly partitions (toYYYYMM) — good for DAU/MAU cohort queries
- No TTL — product events kept long-term for retention analysis
- properties stores JSON — query with JSONExtractString(properties, 'key')
Configuration¶
What each configmap controls¶
clickhouse-config → mounted to config.d/
- performance.xml — connection limits, background merge threads, listen host
- telemetry.xml — crash reporting disabled
clickhouse-users-config → mounted to users.d/ via subPath
- profiles.xml — memory limits, timeouts, readonly settings per profile + quotas
Why subPath? Mounting the whole directory makes it read-only. The Docker entrypoint needs to write
default-user.xmlintousers.d/on startup. SubPath mounts individual files as read-only while the directory itself stays writable.
Key settings explained¶
<!-- listen_host — required for Kubernetes probes to reach the pod IP -->
<!-- Without this ClickHouse only listens on 127.0.0.1 -->
<listen_host>0.0.0.0</listen_host>
<listen_try>1</listen_try> <!-- don't crash if IPv6 unavailable -->
<!-- background_pool_size * ratio must be > 20 (default mutation threshold) -->
<!-- 16 * 2 = 32 > 20 — passes ClickHouse sanity check -->
<background_pool_size>16</background_pool_size>
<background_merges_mutations_concurrency_ratio>2</background_merges_mutations_concurrency_ratio>
Storage¶
| Item | Value |
|---|---|
| Storage class | local-path-retain |
| PVC size | 80Gi |
| Node | ticket-node-3 |
| Data directory | /var/lib/clickhouse |
| Retention policy | PVC survives StatefulSet deletion |
Useful storage queries:
-- Check table sizes on disk
SELECT
table,
formatReadableSize(sum(bytes_on_disk)) AS size_on_disk,
formatReadableSize(sum(data_uncompressed_bytes)) AS uncompressed,
sum(rows) AS total_rows
FROM system.parts
WHERE database = 'analytics' AND active = 1
GROUP BY table
ORDER BY sum(bytes_on_disk) DESC;
Expand storage when needed:
kubectl patch pvc data-clickhouse-0 -n analytic \
-p '{"spec":{"resources":{"requests":{"storage":"200Gi"}}}}'
Connecting from Your App¶
Secret keys reference¶
# From secret: ch-credentials
admin_username # ch_admin
admin_password # admin password
app_username # ch_app
app_password # app password
app_dsn # full native TCP DSN for ch_app
app_http_dsn # full HTTP DSN for ch_app
grafana_username # ch_grafana
grafana_password # grafana password
grafana_http_dsn # full HTTP DSN for ch_grafana
Inject into your app deployment¶
env:
- name: CLICKHOUSE_HOST
value: "clickhouse-svc.analytic.svc.cluster.local"
- name: CLICKHOUSE_PORT
value: "9000"
- name: CLICKHOUSE_HTTP_PORT
value: "8123"
- name: CLICKHOUSE_DB
value: "analytics"
- name: CLICKHOUSE_USER
valueFrom:
secretKeyRef:
name: ch-credentials
key: app_username
- name: CLICKHOUSE_PASSWORD
valueFrom:
secretKeyRef:
name: ch-credentials
key: app_password
Connection strings by language¶
# Go (clickhouse-go)
clickhouse://ch_app:<PASSWORD>@clickhouse-svc.analytic.svc.cluster.local:9000/analytics
# Python (clickhouse-connect)
client = clickhouse_connect.get_client(
host='clickhouse-svc.analytic.svc.cluster.local',
port=8123, username='ch_app', password='<PASSWORD>', database='analytics'
)
# Node.js (@clickhouse/client)
const client = createClient({
host: 'http://clickhouse-svc.analytic.svc.cluster.local:8123',
username: 'ch_app', password: '<PASSWORD>', database: 'analytics'
})
# Grafana datasource URL
http://clickhouse-svc.analytic.svc.cluster.local:8123
# User: ch_grafana
Deploy Order (fresh install)¶
kubectl apply -f 01-namespace.yaml
kubectl apply -f 02-secret.yaml
kubectl apply -f 03-configmap.yaml
kubectl apply -f 05-service.yaml # services before statefulset
kubectl apply -f 04-statefulset.yaml
# Wait for ready
kubectl rollout status statefulset/clickhouse -n analytic --timeout=300s
# Run migrations
kubectl apply -f 06-migration.yaml
kubectl logs -n analytic -l app=clickhouse-migration -f
Day-2 Operations¶
Verify ClickHouse is healthy¶
kubectl get pods -n analytic
kubectl exec -it clickhouse-0 -n analytic -- wget -qO- http://localhost:8123/ping
# Expected: Ok.
Connect via CLI¶
kubectl exec -it clickhouse-0 -n analytic -- \
clickhouse-client --user ch_admin --password <CH_ADMIN_PASSWORD>
Check users¶
Check running queries¶
Kill a slow query¶
Add a new migration¶
# 1. Add new SQL to ConfigMap in 06-migration.yaml
# 2. Rename Job to clickhouse-schema-v2
# 3. kubectl apply -f 06-migration.yaml
Troubleshooting¶
| Symptom | Command |
|---|---|
| Pod stuck in Init | kubectl logs clickhouse-0 -n analytic -c init-sysctl |
| Pod crashing | kubectl logs clickhouse-0 -n analytic --previous |
| Auth failed | Check password matches in 02-secret.yaml and 03-configmap.yaml |
| Probe failing (connection refused) | Verify listen_host in 03-configmap.yaml |
| Migration failed | kubectl logs -n analytic -l app=clickhouse-migration |
| OOMKilled | Increase memory limit in 04-statefulset.yaml |
| Disk full | kubectl exec clickhouse-0 -n analytic -- df -h /var/lib/clickhouse |
| Old PVC data on node | sudo find /var/lib/rancher/k3s/storage/ -name "*data-clickhouse*" -type d |
Known Issues & Lessons Learned¶
| Issue | Root Cause | Fix |
|---|---|---|
runAsNonRoot blocks init containers |
Pod-level setting applies to all containers | Set runAsNonRoot: false per init container |
Entrypoint can't write default-user.xml |
Mounting whole users.d/ makes it read-only |
Use subPath to mount individual files |
max_execution_time rejected in config.d/ |
It's a user-level setting, not server-level | Move to profiles.xml in users.d/ |
storage_configuration rejected for default disk |
Only for multi-disk setups | Use <path> at top level |
background_pool_size=4 rejected |
4*2=8 < 20 default mutation threshold |
Increase to 16 (16*2=32 > 20) |
| Probe fails (connection refused) | ClickHouse defaults to 127.0.0.1 only |
Add <listen_host>0.0.0.0</listen_host> |
| Auth failed on startup | Entrypoint hardcodes <quota>default</quota> |
Add default quota to profiles.xml |
clickhouse-client:24.3 image not found |
Separate client image doesn't exist on Docker Hub | Use clickhouse-server:24.3 (includes client binary) |
Future Work¶
- Apply
07-backup.yaml— daily SYSTEM FREEZE backup - Migrate to ClickHouse Operator when scaling is needed
- Add NetworkPolicy when CNI supports it (Calico/Cilium)
- Hash admin password using
CLICKHOUSE_PASSWORD_SHA256_HEX - Seal secrets with Sealed Secrets or External Secrets Operator