apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: etm-demo
  labels:
    app: "elasticsearch"
    component: "data"
    release: "${elasticsearch-version}"
  name: "elasticsearch-data"
spec:
  serviceName: "elasticsearch-data"
  replicas: 2
  selector:
    matchLabels:
      app: "elasticsearch"
      component: "data"
      release: "${elasticsearch-version}"
  template:
    metadata:
      labels:
        app: "elasticsearch"
        component: "data"
        release: "${elasticsearch-version}"
    spec:
      securityContext:
        fsGroup: 1000
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            podAffinityTerm:
              topologyKey: kubernetes.io/hostname
              labelSelector:
                matchLabels:
                  app: "elasticsearch"
                  component: "data"
      initContainers:
      # see https://www.elastic.co/guide/en/elasticsearch/reference/current/vm-max-map-count.html
      # and https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration-memory.html#mlockall
      - name: "sysctl"
        image: "busybox"
        imagePullPolicy: "Always"
        command: ["sysctl", "-w", "vm.max_map_count=262144"]
        securityContext:
          privileged: true
      - name: "chown"
        image: "docker.elastic.co/elasticsearch/elasticsearch-oss:${elasticsearch-version}"
        imagePullPolicy: "IfNotPresent"
        command:
        - /bin/bash
        - -c
        - chown -R elasticsearch:elasticsearch /usr/share/elasticsearch/data &&
          chown -R elasticsearch:elasticsearch /usr/share/elasticsearch/logs
        securityContext:
          runAsUser: 0
        volumeMounts:
        - mountPath: /usr/share/elasticsearch/data
          name: data
      containers:
      - name: "elasticsearch"
        env:
        - name: cluster.name
          value: etm-demo-01
        - name: node.master
          value: "false"
        - name: node.data
          value: "true"
        - name: node.ingest
          value: "false"
        - name: discovery.seed_hosts
          value: "elasticsearch-master-0.elasticsearch-discovery.etm-demo.svc.cluster.local,elasticsearch-master-1.elasticsearch-discovery.etm-demo.svc.cluster.local,elasticsearch-master-2.elasticsearch-discovery.etm-demo.svc.cluster.local"
        - name: cluster.initial_master_nodes
          value: "elasticsearch-master-0.elasticsearch-discovery.etm-demo.svc.cluster.local,elasticsearch-master-1.elasticsearch-discovery.etm-demo.svc.cluster.local,elasticsearch-master-2.elasticsearch-discovery.etm-demo.svc.cluster.local"
        - name: ES_JAVA_OPTS
          value: "-Djava.net.preferIPv4Stack=true -Xms512M -Xmx512M"
        - name: node.name
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        readinessProbe:
          httpGet:
            path: /_cluster/health?local=true
            port: 9200
          initialDelaySeconds: 5
        image: "docker.elastic.co/elasticsearch/elasticsearch-oss:${elasticsearch-version}"
        imagePullPolicy: "IfNotPresent"
        ports:
        - containerPort: 9200
          name: http
        - containerPort: 9300
          name: transport
        volumeMounts:
        - mountPath: /usr/share/elasticsearch/data
          name: data
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: ssd
      resources:
        requests:
            storage: 10Gi
