apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: etcd
  namespace: datastore
  labels:
    app.kubernetes.io/name: etcd
spec:
  serviceName: etcd
  replicas: 3
  podManagementPolicy: Parallel
  updateStrategy:
    type: RollingUpdate
  selector:
    matchLabels:
      app.kubernetes.io/name: etcd
  template:
    metadata:
      labels:
        app.kubernetes.io/name: etcd
    spec:
      terminationGracePeriodSeconds: 60
      securityContext:
        fsGroup: 1001
      containers:
        - name: etcd
          image: quay.io/coreos/etcd:v3.5.15
          imagePullPolicy: IfNotPresent
          ports:
            - name: client
              containerPort: 2379
            - name: peer
              containerPort: 2380
            - name: metrics
              containerPort: 2381
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: ETCD_NAME
              value: "$(POD_NAME)"
            - name: ETCD_DATA_DIR
              value: /var/lib/etcd
            - name: ETCD_LISTEN_CLIENT_URLS
              value: http://0.0.0.0:2379
            - name: ETCD_ADVERTISE_CLIENT_URLS
              value: http://$(POD_NAME).etcd.datastore.svc.cluster.local:2379
            - name: ETCD_LISTEN_PEER_URLS
              value: http://0.0.0.0:2380
            - name: ETCD_INITIAL_ADVERTISE_PEER_URLS
              value: http://$(POD_NAME).etcd.datastore.svc.cluster.local:2380
            - name: ETCD_INITIAL_CLUSTER
              value: etcd-0=http://etcd-0.etcd.datastore.svc.cluster.local:2380,etcd-1=http://etcd-1.etcd.datastore.svc.cluster.local:2380,etcd-2=http://etcd-2.etcd.datastore.svc.cluster.local:2380
            - name: ETCD_INITIAL_CLUSTER_STATE
              value: new
            - name: ETCD_INITIAL_CLUSTER_TOKEN
              value: production-etcd-cluster
            - name: ETCD_LISTEN_METRICS_URLS
              value: http://0.0.0.0:2381
            - name: ETCD_AUTO_COMPACTION_MODE
              value: revision
            - name: ETCD_AUTO_COMPACTION_RETENTION
              value: "10000"
            - name: ETCD_QUOTA_BACKEND_BYTES
              value: "8589934592"
          resources:
            requests:
              cpu: 500m
              memory: 1Gi
            limits:
              cpu: "2"
              memory: 4Gi
          readinessProbe:
            httpGet:
              path: /readyz
              port: client
            initialDelaySeconds: 10
            periodSeconds: 5
            timeoutSeconds: 3
            failureThreshold: 6
          livenessProbe:
            httpGet:
              path: /livez
              port: client
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 3
            failureThreshold: 3
          volumeMounts:
            - name: data
              mountPath: /var/lib/etcd
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchLabels:
                  app.kubernetes.io/name: etcd
              topologyKey: kubernetes.io/hostname
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: fast-ssd
        resources:
          requests:
            storage: 50Gi
---
apiVersion: v1
kind: Service
metadata:
  name: etcd
  namespace: datastore
  labels:
    app.kubernetes.io/name: etcd
spec:
  clusterIP: None
  publishNotReadyAddresses: true
  selector:
    app.kubernetes.io/name: etcd
  ports:
    - name: client
      port: 2379
      targetPort: client
    - name: peer
      port: 2380
      targetPort: peer
    - name: metrics
      port: 2381
      targetPort: metrics