---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: esdata
  # namespace: elasticsearch
  labels:
    component: elasticsearch
    role: esdata
spec:
  updateStrategy:
    type: RollingUpdate
  serviceName: es-data
  selector:
    matchLabels:
      component: elasticsearch
      role: esdata
  replicas: 3
  template:
    metadata:
      labels:
        component: elasticsearch
        role: esdata
    spec:
      serviceAccountName: elasticsearch
      securityContext:
        fsGroup: 1000
      initContainers:
        - name: map-count-prereq
          image: busybox
          imagePullPolicy: IfNotPresent
          command: ["sysctl", "-w", "vm.max_map_count=262144"]
          securityContext:
            privileged: true
      containers:
        - name: data
          image: elasticsearch:6.7.0
          imagePullPolicy: IfNotPresent
          env:
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: "node.name"
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: "cluster.name"
              value: "es"
            - name: "discovery.zen.minimum_master_nodes"
              value: "2"
            - name: "node.data"
              value: "true"
            - name: "node.master"
              value: "false"
            - name: "node.ingest"
              value: "false"
            - name: "discovery.zen.ping.unicast.hosts"
              value: "es-discovery"
            - name: "http.enabled"
              value: "false"
            - name: ES_JAVA_OPTS
              value: -Xms4096m -Xmx4096m
            - name: "network.host"
              value: "_eth0:ipv4_"
            - name: "processors"
              valueFrom:
                resourceFieldRef:
                  resource: limits.cpu
          ports:
            - containerPort: 9300
              name: transport
          livenessProbe:
            tcpSocket:
              port: transport
            initialDelaySeconds: 30
            periodSeconds: 10
          readinessProbe:
            tcpSocket:
              port: transport
            initialDelaySeconds: 30
            periodSeconds: 10
          resources:
            limits:
              memory: 4Gi
            requests:
              memory: 2Gi
          volumeMounts:
            - name: data
              mountPath: /usr/share/elasticsearch/data
            - name: log4j
              mountPath: /usr/share/elasticsearch/config/log4j2.properties
              subPath: log4j2.properties
          securityContext:
            privileged: false
      tolerations: []
      volumes:
        - name: log4j
          configMap:
            name: log4j
            items:
              - key: log4j.properties
                path: log4j2.properties

  volumeClaimTemplates:
    - metadata:
        name: "data"
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: "fast"
        resources:
          requests:
            storage: 10Gi   # <--- change this to appropriate value