---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: elasticsearch
  labels:
    name: elasticsearch
    app: elasticsearch
spec:
  serviceName: "elasticsearch"
  podManagementPolicy: "Parallel"
  revisionHistoryLimit: 1
  selector:
    matchLabels:
      app: elasticsearch
  template:
    metadata:
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9114"
        prometheus.io/path: "/metrics"
      labels:
        name: elasticsearch
        app: elasticsearch
    spec:
      shareProcessNamespace: true
      terminationGracePeriodSeconds: 60
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - preference:
                matchExpressions:
                  - key: "kubernetes.io/arch"
                    operator: "In"
                    values:
                      - "amd64"
              weight: 1
      containers:
        - image: docker.io/saidsef/elasticsearch:latest
          imagePullPolicy: Always
          name: elasticsearch
          ports:
            - protocol: TCP
              containerPort: 9200
              name: elasticsearch
            - protocol: TCP
              containerPort: 9300
              name: cluster
          lifecycle:
            preStop:
              exec:
                command:
                  - /usr/bin/curl
                  - -XPOST
                  - http://127.0.0.1:9200/_flush
          livenessProbe:
            exec:
              command:
                - pgrep
                - java
            initialDelaySeconds: 10
            periodSeconds: 3
            successThreshold: 1
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              port: 9200
              path: _cat/health
            initialDelaySeconds: 30
            periodSeconds: 5
            successThreshold: 1
            timeoutSeconds: 5
          resources:
            requests:
              memory: "4Gi"
              cpu: "250m"
            limits:
              memory: "4Gi"
              cpu: "500m"
          securityContext:
            allowPrivilegeEscalation: false
            privileged: false
            runAsGroup: 1000
            runAsUser: 1000
            capabilities:
             drop:
               - ALL
             add:
               - NET_BIND_SERVICE
               - SYS_TIME
          volumeMounts:
            - name: elasticsearch-storage
              mountPath: /usr/share/elasticsearch/data
              subPath: data
        - image: quay.io/prometheuscommunity/elasticsearch-exporter:latest
          imagePullPolicy: IfNotPresent
          name: prometheus
          args:
          - '--es.uri=http://127.0.0.1:9200'
          - '--es.all'
          - '--es.indices'
          - '--es.snapshots'
          - '--es.shards'
          - '--es.ssl-skip-verify'
          - '--es.timeout=3s'
          ports:
            - protocol: TCP
              containerPort: 9114
              name: prometheus
          livenessProbe:
            exec:
              command:
                - pgrep
                - elasticsearch_exporter
            initialDelaySeconds: 10
            periodSeconds: 3
            successThreshold: 1
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              port: 9114
              path: /health
            initialDelaySeconds: 10
            periodSeconds: 5
            successThreshold: 1
            timeoutSeconds: 5
          resources:
            requests:
              memory: "512Mi"
              cpu: "50m"
            limits:
              memory: "512Mi"
              cpu: "100m"
          securityContext:
            allowPrivilegeEscalation: false
            privileged: false
            runAsGroup: 1000
            runAsUser: 1000
            capabilities:
             drop:
               - ALL
             add:
               - NET_BIND_SERVICE
               - SYS_TIME
      volumes:
        - name: elasticsearch-storage
          emptyDir: {}
