apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: elasticsearch
spec:
  selector:
    matchLabels:
      service: elasticsearch
  serviceName: elasticsearch
  replicas: 3
  podManagementPolicy: Parallel
  template: 
    metadata:
      labels:
        service: elasticsearch
    spec:
      terminationGracePeriodSeconds: 5
      securityContext:
        runAsUser: 1000
        runAsGroup: 1000
        fsGroup: 1000

      containers:
      - name: elasticsearch
        image: docker.elastic.co/elasticsearch/elasticsearch:7.15.0
        ports:
        - containerPort: 9200
          name: http
          protocol: TCP
        - containerPort: 9300
          name: transport
          protocol: TCP

        volumeMounts:
        - name: es-storage
          mountPath: /usr/share/elasticsearch/data

        readinessProbe:
          httpGet:
            path: /
            port: 9200
          initialDelaySeconds: 60
          periodSeconds: 10

        livenessProbe:
          tcpSocket:
            port: 9300
          initialDelaySeconds: 60
          periodSeconds: 20

        resources:
          requests:
            memory: 1024M
            cpu: 1
          limits:
            memory: 1024M
            cpu: 1

        env:
        - name: discovery.zen.ping.unicast.hosts
          valueFrom:
            fieldRef:
              fieldPath: metadata.labels['service']

        - name: node.name
          valueFrom:
            fieldRef:
              fieldPath: metadata.name

        - name: node.roles
          valueFrom:
            configMapKeyRef:
              name: elasticsearch-properties
              key: node.roles

        - name: cluster.name
          valueFrom:
            fieldRef:
              fieldPath: metadata.labels['service']

        - name: ES_JAVA_OPTS
          valueFrom:
            configMapKeyRef:
              name: elasticsearch-properties
              key: ES_JAVA_OPTS

        - name: node.store.allow_mmap
          valueFrom:
            configMapKeyRef:
              name: elasticsearch-properties
              key: node.store.allow_mmap

        - name: cluster.initial_master_nodes
          valueFrom:
            configMapKeyRef:
              name: elasticsearch-properties
              key: cluster.initial_master_nodes

        - name: discovery.type
          valueFrom:
            configMapKeyRef:
              name: elasticsearch-properties
              key: discovery.type

      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 10
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: service
                  operator: In
                  values:
                    - elasticsearch
              topologyKey: kubernetes.io/hostname

  volumeClaimTemplates:
  - metadata:
      name: es-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 500M
