apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: consul
spec:
  selector:
    matchLabels:
      app: consul # has to match .spec.template.metadata.labels
  serviceName: consul
  replicas: 3
  template:
    metadata:
      labels:
        app: consul # has to match .spec.selector.matchLabels
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: app
                    operator: In
                    values:
                      - consul
              topologyKey: kubernetes.io/hostname
      containers:
        - name: consul
          image: "consul:1.10.1"
          env:
            - name: GOSSIP_ENCRYPTION_KEY
              valueFrom:
                secretKeyRef:
                  name: consul
                  key: gossip-encryption-key
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
          args:
            - "agent"
            - "-advertise=$(POD_IP)"
            - "-bind=0.0.0.0"
            - "-bootstrap-expect=3"
            - "-client=0.0.0.0"
            - "-config-file=/etc/consul/config/server.json"
            - "-datacenter=dc1"
            - "-data-dir=/var/lib/consul"
            - "-disable-host-node-id"
            - "-domain=consul."
            - "-encrypt=$(GOSSIP_ENCRYPTION_KEY)"
            - "-retry-join=consul-0.consul.$(NAMESPACE).svc.cluster.local"
            - "-retry-join=consul-1.consul.$(NAMESPACE).svc.cluster.local"
            - "-retry-join=consul-2.consul.$(NAMESPACE).svc.cluster.local"
            - "-server"
            - "-ui"
          volumeMounts:
            - name: consul-config
              mountPath: /etc/consul/config
            - name: consul-data
              mountPath: /var/lib/consul
            - name: consul-tls
              mountPath: /etc/consul/tls
          lifecycle:
            preStop:
              exec:
                command:
                  - /bin/sh
                  - -c
                  - consul leave
          ports:
            - name: dns-tcp
              containerPort: 8600
              protocol: "TCP"
            - name: dns-udp
              containerPort: 8600
              protocol: "UDP"
            - name: http
              containerPort: 8500
              protocol: "TCP"
            - name: https
              containerPort: 8443
              protocol: "TCP"
            - name: serf-lan-tcp
              containerPort: 8301
              protocol: "TCP"
            - name: serf-lan-udp
              containerPort: 8301
              protocol: "UDP"
            - name: serf-wan-tcp
              containerPort: 8302
              protocol: "TCP"
            - name: serf-wan-udp
              containerPort: 8302
              protocol: "UDP"
            - name: server-rpc
              containerPort: 8300
              protocol: "TCP"
          resources:
            requests:
              cpu: "250m"
              memory: "1Gi"
      terminationGracePeriodSeconds: 10
      securityContext:
        fsGroup: 1000
      volumes:
        - name: consul-config
          configMap:
            name: consul
        - name: consul-tls
          secret:
            secretName: consul
  volumeClaimTemplates:
    - metadata:
        name: consul-data
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 10Gi
