apiVersion: apps/v1
kind:       StatefulSet
metadata:
  name: clickhouse
spec:
  selector: { }
  serviceName: clickhouse
  replicas:    3
  template:
    spec:
      securityContext:
        runAsUser:  999
        runAsGroup: 999
      containers:
        - name:            clickhouse
          image:           clickhouse/clickhouse-server:latest
          imagePullPolicy: IfNotPresent
          command:
            - sh
            - -c
            - |
              set -eux
              export POD_SEQ=$(( ${POD_NAME#clickhouse-} + 1 ))
              exec clickhouse-server -C /etc/clickhouse-server/config.xml
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
          ports:
            - containerPort: 8123
              protocol:      TCP
              name:          http
            - containerPort: 9000
              protocol:      TCP
              name:          native
            - containerPort: 2181
              protocol:      TCP
              name:          keeper
          volumeMounts:
            - mountPath: /etc/clickhouse-server/config.d
              name:      clickhouse-configd
              readOnly:  true
            - mountPath: /etc/clickhouse-server/users.d
              name:      clickhouse-usersd
              readOnly:  true
            - mountPath: /var/lib/clickhouse
              name:      clickhouse-data
          resources:
            requests:
              memory: 512Mi
              cpu:    500m
            limits:
              memory: 2Gi
              cpu:    2000m
      volumes:
        - name: clickhouse-configd
          configMap:
            name:     clickhouse-configd
            optional: false
        - name: clickhouse-usersd
          configMap:
            name:     clickhouse-usersd
            optional: false
  volumeClaimTemplates:
    - metadata:
        name: clickhouse-data
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 10Gi
