apiVersion: v1
kind: Namespace
metadata:
  name: kv-store
---
apiVersion: v1
kind: Service
metadata:
  name: etcd
  namespace: kv-store
  labels:
    app.kubernetes.io/name: etcd
spec:
  clusterIP: None
  publishNotReadyAddresses: true
  selector:
    app.kubernetes.io/name: etcd
  ports:
    - name: client
      port: 2379
      targetPort: 2379
    - name: peer
      port: 2380
      targetPort: 2380
---
apiVersion: v1
kind: Service
metadata:
  name: etcd-client
  namespace: kv-store
  labels:
    app.kubernetes.io/name: etcd
spec:
  type: ClusterIP
  selector:
    app.kubernetes.io/name: etcd
  ports:
    - name: client
      port: 2379
      targetPort: 2379
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: etcd
  namespace: kv-store
spec:
  minAvailable: 2
  selector:
    matchLabels:
      app.kubernetes.io/name: etcd
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: etcd
  namespace: kv-store
  labels:
    app.kubernetes.io/name: etcd
spec:
  serviceName: etcd
  replicas: 3
  selector:
    matchLabels:
      app.kubernetes.io/name: etcd
  template:
    metadata:
      labels:
        app.kubernetes.io/name: etcd
    spec:
      terminationGracePeriodSeconds: 30
      securityContext:
        fsGroup: 1001
      containers:
        - name: etcd
          image: quay.io/coreos/etcd:v3.5.14
          imagePullPolicy: IfNotPresent
          command:
            - /usr/local/bin/etcd
          args:
            - --name=$(POD_NAME)
            - --data-dir=/var/lib/etcd
            - --listen-client-urls=http://0.0.0.0:2379
            - --advertise-client-urls=http://$(POD_NAME).etcd.kv-store.svc.cluster.local:2379
            - --listen-peer-urls=http://0.0.0.0:2380
            - --initial-advertise-peer-urls=http://$(POD_NAME).etcd.kv-store.svc.cluster.local:2380
            - --initial-cluster=etcd-0=http://etcd-0.etcd.kv-store.svc.cluster.local:2380,etcd-1=http://etcd-1.etcd.kv-store.svc.cluster.local:2380,etcd-2=http://etcd-2.etcd.kv-store.svc.cluster.local:2380
            - --initial-cluster-state=new
            - --initial-cluster-token=kv-store-prod
            - --auto-compaction-mode=periodic
            - --auto-compaction-retention=1h
            - --quota-backend-bytes=8589934592
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
          ports:
            - name: client
              containerPort: 2379
            - name: peer
              containerPort: 2380
          readinessProbe:
            exec:
              command:
                - /bin/sh
                - -ec
                - ETCDCTL_API=3 etcdctl --endpoints=http://127.0.0.1:2379 endpoint health
            initialDelaySeconds: 10
            periodSeconds: 5
            timeoutSeconds: 3
            failureThreshold: 6
          livenessProbe:
            exec:
              command:
                - /bin/sh
                - -ec
                - ETCDCTL_API=3 etcdctl --endpoints=http://127.0.0.1:2379 endpoint health
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          resources:
            requests:
              cpu: 250m
              memory: 512Mi
            limits:
              cpu: "1"
              memory: 1Gi
          volumeMounts:
            - name: data
              mountPath: /var/lib/etcd
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 20Gi