From 7f03e22c7bacf13024a36d0500aad8a4d1f32526 Mon Sep 17 00:00:00 2001 From: sk-portkey Date: Mon, 2 Mar 2026 18:31:14 +0530 Subject: [PATCH 1/3] feat: clickhouse replicas --- charts/portkey-app/templates/_helpers.tpl | 28 ++++ .../clickhouse-keeper/config-map.yaml | 43 ++++++ .../clickhouse-keeper/headless-service.yaml | 32 ++++ .../clickhouse-keeper/service-account.yaml | 16 ++ .../clickhouse-keeper/stateful-set.yaml | 140 ++++++++++++++++++ .../templates/clickhouse/config-map.yaml | 43 ++++++ .../clickhouse/headless-service.yaml | 36 +++++ .../templates/clickhouse/service.yaml | 6 + .../templates/clickhouse/stateful-set.yaml | 54 ++++++- charts/portkey-app/values.yaml | 34 +++++ 10 files changed, 427 insertions(+), 5 deletions(-) create mode 100644 charts/portkey-app/templates/clickhouse-keeper/config-map.yaml create mode 100644 charts/portkey-app/templates/clickhouse-keeper/headless-service.yaml create mode 100644 charts/portkey-app/templates/clickhouse-keeper/service-account.yaml create mode 100644 charts/portkey-app/templates/clickhouse-keeper/stateful-set.yaml create mode 100644 charts/portkey-app/templates/clickhouse/headless-service.yaml diff --git a/charts/portkey-app/templates/_helpers.tpl b/charts/portkey-app/templates/_helpers.tpl index 941d7c4..14467ee 100644 --- a/charts/portkey-app/templates/_helpers.tpl +++ b/charts/portkey-app/templates/_helpers.tpl @@ -552,6 +552,34 @@ Template containing common environment variables that are used by several servic {{- end -}} {{- end -}} +{{- define "clickhouse.replicationEnabled" -}} +{{- if and (not .Values.clickhouse.external.enabled) (gt (int .Values.clickhouse.statefulSet.replicas) 1) -}} +true +{{- end -}} +{{- end -}} + +{{- define "clickhouse.headlessServiceName" -}} +{{- printf "%s-%s-headless" (include "portkey.fullname" .) .Values.clickhouse.name -}} +{{- end -}} + +{{- define "clickhouseKeeper.deployEnabled" -}} +{{- if and (include "clickhouse.replicationEnabled" .) (not .Values.clickhouseKeeper.external.enabled) -}} +true +{{- end -}} +{{- end -}} + +{{- define "clickhouseKeeper.headlessServiceName" -}} +{{- printf "%s-%s-headless" (include "portkey.fullname" .) .Values.clickhouseKeeper.name -}} +{{- end -}} + +{{- define "clickhouseKeeper.serviceAccountName" -}} +{{- if .Values.clickhouseKeeper.serviceAccount.create -}} + {{ default (printf "%s-%s" (include "portkey.fullname" .) .Values.clickhouseKeeper.name) .Values.clickhouseKeeper.serviceAccount.name | trunc 63 | trimSuffix "-" }} +{{- else -}} + {{ default "default" .Values.clickhouseKeeper.serviceAccount.name }} +{{- end -}} +{{- end -}} + {{- define "mysql.serviceAccountName" -}} {{- if .Values.mysql.serviceAccount.create -}} {{ default (printf "%s-%s" (include "portkey.fullname" .) .Values.mysql.name) .Values.mysql.serviceAccount.name | trunc 63 | trimSuffix "-" }} diff --git a/charts/portkey-app/templates/clickhouse-keeper/config-map.yaml b/charts/portkey-app/templates/clickhouse-keeper/config-map.yaml new file mode 100644 index 0000000..8debbd7 --- /dev/null +++ b/charts/portkey-app/templates/clickhouse-keeper/config-map.yaml @@ -0,0 +1,43 @@ +{{- if include "clickhouseKeeper.deployEnabled" . }} +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "portkey.fullname" . }}-{{ .Values.clickhouseKeeper.name }} + labels: + {{- include "portkey.labels" . | nindent 4 }} + annotations: + {{- include "portkey.annotations" . | nindent 4 }} +data: + keeper_config.xml: | + + + information + 1 + + + {{ .Values.clickhouseKeeper.containerPort }} + ${SERVER_ID} + /var/lib/clickhouse-keeper/log + /var/lib/clickhouse-keeper/snapshots + + 10000 + 30000 + warning + + + {{- $keeperReplicas := int .Values.clickhouseKeeper.replicas }} + {{- $fullname := include "portkey.fullname" . }} + {{- $keeperName := .Values.clickhouseKeeper.name }} + {{- $headlessSvc := include "clickhouseKeeper.headlessServiceName" . }} + {{- $raftPort := .Values.clickhouseKeeper.containerRaftPort }} + {{- range $i := until $keeperReplicas }} + + {{ add $i 1 }} + {{ $fullname }}-{{ $keeperName }}-{{ $i }}.{{ $headlessSvc }} + {{ $raftPort }} + + {{- end }} + + + +{{- end }} diff --git a/charts/portkey-app/templates/clickhouse-keeper/headless-service.yaml b/charts/portkey-app/templates/clickhouse-keeper/headless-service.yaml new file mode 100644 index 0000000..12ca86e --- /dev/null +++ b/charts/portkey-app/templates/clickhouse-keeper/headless-service.yaml @@ -0,0 +1,32 @@ +{{- if include "clickhouseKeeper.deployEnabled" . }} +apiVersion: v1 +kind: Service +metadata: + name: {{ include "clickhouseKeeper.headlessServiceName" . }} + labels: + {{- include "portkey.labels" . | nindent 4 }} + {{- with .Values.clickhouseKeeper.service.labels }} + {{- toYaml . | nindent 4 }} + {{- end }} + annotations: + {{- include "portkey.annotations" . | nindent 4 }} + {{- with .Values.clickhouseKeeper.service.annotations }} + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + type: ClusterIP + clusterIP: None + publishNotReadyAddresses: true + ports: + - name: keeper-client + port: {{ .Values.clickhouseKeeper.containerPort }} + targetPort: keeper-client + protocol: TCP + - name: keeper-raft + port: {{ .Values.clickhouseKeeper.containerRaftPort }} + targetPort: keeper-raft + protocol: TCP + selector: + {{- include "portkey.selectorLabels" . | nindent 4 }} + app.kubernetes.io/component: {{ include "portkey.fullname" . }}-{{ .Values.clickhouseKeeper.name }} +{{- end }} diff --git a/charts/portkey-app/templates/clickhouse-keeper/service-account.yaml b/charts/portkey-app/templates/clickhouse-keeper/service-account.yaml new file mode 100644 index 0000000..3b79829 --- /dev/null +++ b/charts/portkey-app/templates/clickhouse-keeper/service-account.yaml @@ -0,0 +1,16 @@ +{{- if and (include "clickhouseKeeper.deployEnabled" .) .Values.clickhouseKeeper.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "clickhouseKeeper.serviceAccountName" . }} + labels: + {{- include "portkey.labels" . | nindent 4 }} + {{- with .Values.clickhouseKeeper.serviceAccount.labels }} + {{- toYaml . | nindent 4 }} + {{- end }} + annotations: + {{- include "portkey.annotations" . | nindent 4 }} + {{- with .Values.clickhouseKeeper.serviceAccount.annotations }} + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/charts/portkey-app/templates/clickhouse-keeper/stateful-set.yaml b/charts/portkey-app/templates/clickhouse-keeper/stateful-set.yaml new file mode 100644 index 0000000..edf1ab2 --- /dev/null +++ b/charts/portkey-app/templates/clickhouse-keeper/stateful-set.yaml @@ -0,0 +1,140 @@ +{{- if include "clickhouseKeeper.deployEnabled" . }} +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: {{ include "portkey.fullname" . }}-{{ .Values.clickhouseKeeper.name }} + labels: + {{- include "portkey.labels" . | nindent 4 }} + {{- with .Values.clickhouseKeeper.statefulSet.labels }} + {{- toYaml . | nindent 4 }} + {{- end }} + annotations: + {{- include "portkey.annotations" . | nindent 4 }} + {{- with .Values.clickhouseKeeper.statefulSet.annotations }} + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + serviceName: {{ include "clickhouseKeeper.headlessServiceName" . }} + replicas: {{ .Values.clickhouseKeeper.replicas }} + selector: + matchLabels: + {{- include "portkey.selectorLabels" . | nindent 6 }} + app.kubernetes.io/component: {{ include "portkey.fullname" . }}-{{ .Values.clickhouseKeeper.name }} + template: + metadata: + {{- with .Values.clickhouseKeeper.statefulSet.annotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- with .Values.clickhouseKeeper.statefulSet.labels }} + {{- toYaml . | nindent 8 }} + {{- end }} + {{- include "portkey.labels" . | nindent 8 }} + app.kubernetes.io/component: {{ include "portkey.fullname" . }}-{{ .Values.clickhouseKeeper.name }} + spec: + imagePullSecrets: + {{- range .Values.imageCredentials }} + - name: {{ .name }} + {{- end }} + securityContext: + {{- toYaml .Values.clickhouseKeeper.statefulSet.podSecurityContext | nindent 8 }} + serviceAccountName: {{ include "clickhouseKeeper.serviceAccountName" . }} + initContainers: + - name: generate-keeper-config + image: busybox:1.36 + command: + - sh + - -c + - | + HOSTNAME=$(hostname) + SERVER_ID=$((${HOSTNAME##*-} + 1)) + sed "s/\${SERVER_ID}/${SERVER_ID}/g" /etc/clickhouse-keeper-template/keeper_config.xml > /etc/clickhouse-keeper/keeper_config.xml + volumeMounts: + - name: keeper-config-template + mountPath: /etc/clickhouse-keeper-template + - name: keeper-config + mountPath: /etc/clickhouse-keeper + containers: + - name: {{ .Values.clickhouseKeeper.name }} + image: "{{ .Values.images.clickhouseImage.repository }}:{{ .Values.images.clickhouseImage.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.images.clickhouseImage.pullPolicy }} + command: + - clickhouse-keeper + - --config-file=/etc/clickhouse-keeper/keeper_config.xml + ports: + - name: keeper-client + containerPort: {{ .Values.clickhouseKeeper.containerPort }} + protocol: TCP + - name: keeper-raft + containerPort: {{ .Values.clickhouseKeeper.containerRaftPort }} + protocol: TCP + readinessProbe: + exec: + command: + - bash + - -c + - echo ruok | nc localhost {{ .Values.clickhouseKeeper.containerPort }} | grep -q imok + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + livenessProbe: + exec: + command: + - bash + - -c + - echo ruok | nc localhost {{ .Values.clickhouseKeeper.containerPort }} | grep -q imok + initialDelaySeconds: 15 + periodSeconds: 20 + timeoutSeconds: 5 + resources: + {{- toYaml .Values.clickhouseKeeper.statefulSet.resources | nindent 12 }} + securityContext: + {{- toYaml .Values.clickhouseKeeper.statefulSet.securityContext | nindent 12 }} + volumeMounts: + - name: keeper-config + mountPath: /etc/clickhouse-keeper + - name: keeper-data + mountPath: /var/lib/clickhouse-keeper + {{- with .Values.clickhouseKeeper.statefulSet.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.clickhouseKeeper.statefulSet.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.clickhouseKeeper.statefulSet.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} + volumes: + - name: keeper-config-template + configMap: + name: {{ include "portkey.fullname" . }}-{{ .Values.clickhouseKeeper.name }} + - name: keeper-config + emptyDir: {} + {{- if not .Values.clickhouseKeeper.statefulSet.persistence.enabled }} + - name: keeper-data + emptyDir: {} + {{- end }} + {{- if .Values.clickhouseKeeper.statefulSet.persistence.enabled }} + volumeClaimTemplates: + - apiVersion: v1 + kind: PersistentVolumeClaim + metadata: + name: keeper-data + labels: + {{- include "portkey.selectorLabels" . | nindent 10 }} + app.kubernetes.io/component: {{ include "portkey.fullname" . }}-{{ .Values.clickhouseKeeper.name }} + spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: {{ .Values.clickhouseKeeper.statefulSet.persistence.size }} + {{- if .Values.clickhouseKeeper.statefulSet.persistence.storageClassName }} + storageClassName: {{ .Values.clickhouseKeeper.statefulSet.persistence.storageClassName }} + {{- end }} + {{- end }} +{{- end }} diff --git a/charts/portkey-app/templates/clickhouse/config-map.yaml b/charts/portkey-app/templates/clickhouse/config-map.yaml index 15384ac..3f45c92 100644 --- a/charts/portkey-app/templates/clickhouse/config-map.yaml +++ b/charts/portkey-app/templates/clickhouse/config-map.yaml @@ -34,6 +34,49 @@ data: {{ .Values.clickhouse.retention.ttlOnlyDropParts }} {{- end }} + {{- if include "clickhouse.replicationEnabled" . }} + {{ .Values.clickhouse.containerInterserverPort }} + + <{{ .Values.clickhouse.clusterName }}> + + true + {{- $replicas := int .Values.clickhouse.statefulSet.replicas }} + {{- $headlessSvc := include "clickhouse.headlessServiceName" . }} + {{- $nativePort := .Values.clickhouse.containerNativePort }} + {{- $fullname := include "portkey.fullname" . }} + {{- $chName := .Values.clickhouse.name }} + {{- range $i := until $replicas }} + + {{ $fullname }}-{{ $chName }}-{{ $i }}.{{ $headlessSvc }} + {{ $nativePort }} + + {{- end }} + + + + + {{- if .Values.clickhouseKeeper.external.enabled }} + {{- range .Values.clickhouseKeeper.external.hosts }} + + {{ .host }} + {{ .port }} + + {{- end }} + {{- else }} + {{- $keeperReplicas := int .Values.clickhouseKeeper.replicas }} + {{- $keeperHeadlessSvc := include "clickhouseKeeper.headlessServiceName" . }} + {{- $keeperFullname := include "portkey.fullname" . }} + {{- $keeperName := .Values.clickhouseKeeper.name }} + {{- $keeperPort := .Values.clickhouseKeeper.containerPort }} + {{- range $i := until $keeperReplicas }} + + {{ $keeperFullname }}-{{ $keeperName }}-{{ $i }}.{{ $keeperHeadlessSvc }} + {{ $keeperPort }} + + {{- end }} + {{- end }} + + {{- end }} {{- if not .Values.clickhouse.external.existingSecretName }} users.xml: | diff --git a/charts/portkey-app/templates/clickhouse/headless-service.yaml b/charts/portkey-app/templates/clickhouse/headless-service.yaml new file mode 100644 index 0000000..4ddb057 --- /dev/null +++ b/charts/portkey-app/templates/clickhouse/headless-service.yaml @@ -0,0 +1,36 @@ +{{- if include "clickhouse.replicationEnabled" . }} +apiVersion: v1 +kind: Service +metadata: + name: {{ include "clickhouse.headlessServiceName" . }} + labels: + {{- include "portkey.labels" . | nindent 4 }} + {{- with .Values.clickhouse.service.labels }} + {{- toYaml . | nindent 4 }} + {{- end }} + annotations: + {{- include "portkey.annotations" . | nindent 4 }} + {{- with .Values.clickhouse.service.annotations }} + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + type: ClusterIP + clusterIP: None + publishNotReadyAddresses: true + ports: + - name: ch + port: {{ .Values.clickhouse.service.httpPort }} + targetPort: ch + protocol: TCP + - name: ch-native + port: {{ .Values.clickhouse.service.nativePort }} + targetPort: ch-native + protocol: TCP + - name: ch-interserver + port: {{ .Values.clickhouse.containerInterserverPort }} + targetPort: ch-interserver + protocol: TCP + selector: + {{- include "portkey.selectorLabels" . | nindent 4 }} + app.kubernetes.io/component: {{ include "portkey.fullname" . }}-{{ .Values.clickhouse.name }} +{{- end }} diff --git a/charts/portkey-app/templates/clickhouse/service.yaml b/charts/portkey-app/templates/clickhouse/service.yaml index 89cc3e3..d288764 100644 --- a/charts/portkey-app/templates/clickhouse/service.yaml +++ b/charts/portkey-app/templates/clickhouse/service.yaml @@ -29,6 +29,12 @@ spec: port: {{ .Values.clickhouse.service.nativePort }} targetPort: ch-native protocol: TCP + {{- if include "clickhouse.replicationEnabled" . }} + - name: ch-interserver + port: {{ .Values.clickhouse.containerInterserverPort }} + targetPort: ch-interserver + protocol: TCP + {{- end }} selector: {{- include "portkey.selectorLabels" . | nindent 4 }} app.kubernetes.io/component: {{ include "portkey.fullname" . }}-{{ .Values.clickhouse.name }} diff --git a/charts/portkey-app/templates/clickhouse/stateful-set.yaml b/charts/portkey-app/templates/clickhouse/stateful-set.yaml index d9b5644..c2fb000 100644 --- a/charts/portkey-app/templates/clickhouse/stateful-set.yaml +++ b/charts/portkey-app/templates/clickhouse/stateful-set.yaml @@ -14,7 +14,7 @@ metadata: {{- toYaml . | nindent 4 }} {{- end }} spec: - serviceName: {{ include "portkey.fullname" . }}-{{ .Values.clickhouse.name }} + serviceName: {{ include "portkey.fullname" . }}-{{ .Values.clickhouse.name }}{{- if include "clickhouse.replicationEnabled" . }}-headless{{- end }} replicas: {{ .Values.clickhouse.statefulSet.replicas | default 1 }} selector: matchLabels: @@ -40,8 +40,9 @@ spec: securityContext: {{- toYaml .Values.clickhouse.statefulSet.podSecurityContext | nindent 8 }} serviceAccountName: {{ include "clickhouse.serviceAccountName" . }} - {{- if .Values.clickhouse.external.existingSecretName }} + {{- if or .Values.clickhouse.external.existingSecretName (include "clickhouse.replicationEnabled" .) }} initContainers: + {{- if .Values.clickhouse.external.existingSecretName }} - name: generate-users-config image: busybox:1.36 command: @@ -84,6 +85,35 @@ spec: resources: {{- toYaml . | nindent 12 }} {{- end }} + {{- end }} + {{- if include "clickhouse.replicationEnabled" . }} + - name: generate-macros + image: busybox:1.36 + command: + - sh + - -c + - | + REPLICA=$(hostname) + cat > /tmp/macros/macros.xml << EOF + + + ${CLUSTER_NAME} + 01 + ${REPLICA} + + + EOF + env: + - name: CLUSTER_NAME + value: {{ .Values.clickhouse.clusterName | quote }} + volumeMounts: + - name: clickhouse-macros + mountPath: /tmp/macros + {{- with .Values.clickhouse.initContainer.resources }} + resources: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- end }} {{- end }} containers: - name: {{ .Values.clickhouse.name }} @@ -122,6 +152,11 @@ spec: - name: ch-native containerPort: {{ .Values.clickhouse.containerNativePort }} protocol: TCP + {{- if include "clickhouse.replicationEnabled" . }} + - name: ch-interserver + containerPort: {{ .Values.clickhouse.containerInterserverPort }} + protocol: TCP + {{- end }} {{- with .Values.clickhouse.statefulSet.startupProbe }} startupProbe: {{- toYaml . | nindent 12 }} @@ -152,11 +187,16 @@ spec: name: clickhouse-conf subPath: users.xml {{- end }} - {{- if .Values.clickhouse.customConfig.enabled }} + {{- if or .Values.clickhouse.customConfig.enabled (include "clickhouse.replicationEnabled" .) }} - mountPath: /etc/clickhouse-server/config.d/config.xml name: clickhouse-conf subPath: config.xml {{- end }} + {{- if include "clickhouse.replicationEnabled" . }} + - mountPath: /etc/clickhouse-server/config.d/macros.xml + name: clickhouse-macros + subPath: macros.xml + {{- end }} {{- with .Values.clickhouse.statefulSet.extraContainerConfig }} {{- toYaml . | nindent 10 }} {{- end }} @@ -183,7 +223,7 @@ spec: {{- if .Values.clickhouse.external.existingSecretName }} - name: clickhouse-users-config emptyDir: {} - {{- if .Values.clickhouse.customConfig.enabled }} + {{- if or .Values.clickhouse.customConfig.enabled (include "clickhouse.replicationEnabled" .) }} - name: clickhouse-conf configMap: name: {{ include "portkey.fullname" . }}-{{ .Values.clickhouse.name }} @@ -198,11 +238,15 @@ spec: items: - key: users.xml path: users.xml - {{- if .Values.clickhouse.customConfig.enabled }} + {{- if or .Values.clickhouse.customConfig.enabled (include "clickhouse.replicationEnabled" .) }} - key: config.xml path: config.xml {{- end }} {{- end }} + {{- if include "clickhouse.replicationEnabled" . }} + - name: clickhouse-macros + emptyDir: {} + {{- end }} {{- with .Values.clickhouse.statefulSet.volumes }} {{- toYaml . | nindent 8 }} {{- end }} diff --git a/charts/portkey-app/values.yaml b/charts/portkey-app/values.yaml index f0806bd..b9a584b 100644 --- a/charts/portkey-app/values.yaml +++ b/charts/portkey-app/values.yaml @@ -684,6 +684,8 @@ clickhouse: verificationMode: "strict" containerHttpPort: 8123 containerNativePort: 9000 + containerInterserverPort: 9009 + clusterName: "portkey_cluster" initContainer: resources: {} # resources: @@ -760,6 +762,38 @@ clickhouse: labels: {} annotations: {} +clickhouseKeeper: + name: "clickhouse-keeper" + replicas: 3 + containerPort: 2181 + containerRaftPort: 9234 + external: + enabled: false + hosts: [] + # - host: keeper-1.example.com + # port: 2181 + statefulSet: + labels: {} + annotations: {} + podSecurityContext: {} + securityContext: {} + resources: {} + nodeSelector: {} + tolerations: [] + affinity: {} + persistence: + enabled: false + size: 5Gi + storageClassName: "" + service: + labels: {} + annotations: {} + serviceAccount: + create: true + name: "" + labels: {} + annotations: {} + apm: grafana: enabled: false From e56659039916026202cbf86d8144da8d6bbb6cfe Mon Sep 17 00:00:00 2001 From: sk-portkey Date: Wed, 4 Mar 2026 10:47:57 +0530 Subject: [PATCH 2/3] fix: sharding --- charts/portkey-app/templates/_helpers.tpl | 10 +++++-- .../templates/backend/deployment.yaml | 10 +++++++ .../templates/clickhouse/config-map.yaml | 27 ++++++++++++++----- .../templates/clickhouse/stateful-set.yaml | 21 ++++++++++++--- charts/portkey-app/values.yaml | 5 ++++ 5 files changed, 61 insertions(+), 12 deletions(-) diff --git a/charts/portkey-app/templates/_helpers.tpl b/charts/portkey-app/templates/_helpers.tpl index 14467ee..ce51f9c 100644 --- a/charts/portkey-app/templates/_helpers.tpl +++ b/charts/portkey-app/templates/_helpers.tpl @@ -553,7 +553,13 @@ Template containing common environment variables that are used by several servic {{- end -}} {{- define "clickhouse.replicationEnabled" -}} -{{- if and (not .Values.clickhouse.external.enabled) (gt (int .Values.clickhouse.statefulSet.replicas) 1) -}} +{{- if and (not .Values.clickhouse.external.enabled) (or .Values.clickhouseKeeper.enabled .Values.clickhouseKeeper.external.enabled) -}} +true +{{- end -}} +{{- end -}} + +{{- define "clickhouse.shardingEnabled" -}} +{{- if and (include "clickhouse.replicationEnabled" .) .Values.clickhouse.sharding.enabled -}} true {{- end -}} {{- end -}} @@ -563,7 +569,7 @@ true {{- end -}} {{- define "clickhouseKeeper.deployEnabled" -}} -{{- if and (include "clickhouse.replicationEnabled" .) (not .Values.clickhouseKeeper.external.enabled) -}} +{{- if and .Values.clickhouseKeeper.enabled (not .Values.clickhouseKeeper.external.enabled) -}} true {{- end -}} {{- end -}} diff --git a/charts/portkey-app/templates/backend/deployment.yaml b/charts/portkey-app/templates/backend/deployment.yaml index 53473c4..8741a4f 100644 --- a/charts/portkey-app/templates/backend/deployment.yaml +++ b/charts/portkey-app/templates/backend/deployment.yaml @@ -100,6 +100,16 @@ spec: - name: MCP_GATEWAY_URL value: {{ .Values.mcp.externalUrl | quote }} {{- end }} + {{- if include "clickhouse.replicationEnabled" . }} + - name: CLICKHOUSE_REPLICATED + value: "true" + - name: CLICKHOUSE_CLUSTER_NAME + value: {{ .Values.clickhouse.clusterName | quote }} + {{- end }} + {{- if include "clickhouse.shardingEnabled" . }} + - name: CLICKHOUSE_SHARDING_ENABLED + value: "true" + {{- end }} image: "{{ .Values.images.backendImage.repository }}:{{ .Values.images.backendImage.tag | default .Chart.AppVersion }}" imagePullPolicy: {{ .Values.images.backendImage.pullPolicy }} command: ["/bin/sh", "-c"] diff --git a/charts/portkey-app/templates/clickhouse/config-map.yaml b/charts/portkey-app/templates/clickhouse/config-map.yaml index 3f45c92..653c314 100644 --- a/charts/portkey-app/templates/clickhouse/config-map.yaml +++ b/charts/portkey-app/templates/clickhouse/config-map.yaml @@ -38,20 +38,33 @@ data: {{ .Values.clickhouse.containerInterserverPort }} <{{ .Values.clickhouse.clusterName }}> + {{- if include "clickhouse.shardingEnabled" . }} + {{- $shardsCount := int .Values.clickhouse.sharding.shardsCount }} + {{- $replicasPerShard := int .Values.clickhouse.sharding.replicasPerShard }} + {{- $fullname := include "portkey.fullname" . }} + {{- $chName := .Values.clickhouse.name }} + {{- $headlessSvc := include "clickhouse.headlessServiceName" . }} + {{- $nativePort := .Values.clickhouse.containerNativePort }} + {{- range $s := until $shardsCount }} true - {{- $replicas := int .Values.clickhouse.statefulSet.replicas }} - {{- $headlessSvc := include "clickhouse.headlessServiceName" . }} - {{- $nativePort := .Values.clickhouse.containerNativePort }} - {{- $fullname := include "portkey.fullname" . }} - {{- $chName := .Values.clickhouse.name }} - {{- range $i := until $replicas }} + {{- range $r := until $replicasPerShard }} + {{- $ordinal := add (mul $s $replicasPerShard) $r }} - {{ $fullname }}-{{ $chName }}-{{ $i }}.{{ $headlessSvc }} + {{ $fullname }}-{{ $chName }}-{{ $ordinal }}.{{ $headlessSvc }} {{ $nativePort }} {{- end }} + {{- end }} + {{- else }} + + true + + /clickhouse/discovery/{{ .Values.clickhouse.clusterName }} + + + {{- end }} diff --git a/charts/portkey-app/templates/clickhouse/stateful-set.yaml b/charts/portkey-app/templates/clickhouse/stateful-set.yaml index c2fb000..f5cc4e6 100644 --- a/charts/portkey-app/templates/clickhouse/stateful-set.yaml +++ b/charts/portkey-app/templates/clickhouse/stateful-set.yaml @@ -15,17 +15,22 @@ metadata: {{- end }} spec: serviceName: {{ include "portkey.fullname" . }}-{{ .Values.clickhouse.name }}{{- if include "clickhouse.replicationEnabled" . }}-headless{{- end }} + {{- if include "clickhouse.shardingEnabled" . }} + replicas: {{ mul (int .Values.clickhouse.sharding.shardsCount) (int .Values.clickhouse.sharding.replicasPerShard) }} + {{- else }} replicas: {{ .Values.clickhouse.statefulSet.replicas | default 1 }} + {{- end }} selector: matchLabels: {{- include "portkey.selectorLabels" . | nindent 6 }} app.kubernetes.io/component: {{ include "portkey.fullname" . }}-{{ .Values.clickhouse.name }} template: metadata: - {{- with .Values.clickhouse.statefulSet.annotations }} annotations: + checksum/config: {{ include (print $.Template.BasePath "/clickhouse/config-map.yaml") . | sha256sum }} + {{- with .Values.clickhouse.statefulSet.annotations }} {{- toYaml . | nindent 8 }} - {{- end }} + {{- end }} labels: {{- with .Values.clickhouse.statefulSet.labels }} {{- toYaml . | nindent 8 }} @@ -94,11 +99,17 @@ spec: - -c - | REPLICA=$(hostname) + ORDINAL=${REPLICA##*-} + {{- if include "clickhouse.shardingEnabled" . }} + SHARD=$(printf "%02d" $(( (ORDINAL / REPLICAS_PER_SHARD) + 1 ))) + {{- else }} + SHARD="01" + {{- end }} cat > /tmp/macros/macros.xml << EOF ${CLUSTER_NAME} - 01 + ${SHARD} ${REPLICA} @@ -106,6 +117,10 @@ spec: env: - name: CLUSTER_NAME value: {{ .Values.clickhouse.clusterName | quote }} + {{- if include "clickhouse.shardingEnabled" . }} + - name: REPLICAS_PER_SHARD + value: {{ .Values.clickhouse.sharding.replicasPerShard | quote }} + {{- end }} volumeMounts: - name: clickhouse-macros mountPath: /tmp/macros diff --git a/charts/portkey-app/values.yaml b/charts/portkey-app/values.yaml index b9a584b..2f7a255 100644 --- a/charts/portkey-app/values.yaml +++ b/charts/portkey-app/values.yaml @@ -686,6 +686,10 @@ clickhouse: containerNativePort: 9000 containerInterserverPort: 9009 clusterName: "portkey_cluster" + sharding: + enabled: false + shardsCount: 1 + replicasPerShard: 1 initContainer: resources: {} # resources: @@ -763,6 +767,7 @@ clickhouse: annotations: {} clickhouseKeeper: + enabled: false name: "clickhouse-keeper" replicas: 3 containerPort: 2181 From a23323a71f9bd0289b77da05cc0616e1693b44ce Mon Sep 17 00:00:00 2001 From: sk-portkey Date: Wed, 4 Mar 2026 11:42:20 +0530 Subject: [PATCH 3/3] fix: conditional annotations --- charts/portkey-app/templates/clickhouse/stateful-set.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/charts/portkey-app/templates/clickhouse/stateful-set.yaml b/charts/portkey-app/templates/clickhouse/stateful-set.yaml index f5cc4e6..09b50b3 100644 --- a/charts/portkey-app/templates/clickhouse/stateful-set.yaml +++ b/charts/portkey-app/templates/clickhouse/stateful-set.yaml @@ -26,11 +26,15 @@ spec: app.kubernetes.io/component: {{ include "portkey.fullname" . }}-{{ .Values.clickhouse.name }} template: metadata: + {{- if or .Values.clickhouse.statefulSet.annotations (include "clickhouse.replicationEnabled" .) }} annotations: + {{- if include "clickhouse.replicationEnabled" . }} checksum/config: {{ include (print $.Template.BasePath "/clickhouse/config-map.yaml") . | sha256sum }} + {{- end }} {{- with .Values.clickhouse.statefulSet.annotations }} {{- toYaml . | nindent 8 }} {{- end }} + {{- end }} labels: {{- with .Values.clickhouse.statefulSet.labels }} {{- toYaml . | nindent 8 }}