apiVersion: apps/v1
kind: Deployment
metadata:
  name: fusionauth-search
spec:
  selector:
    matchLabels:
      app: fusionauth-search
  template:
    metadata:
      labels:
        app: fusionauth-search
    spec:
      initContainers:
        # fix Failed to create node environment issue from elasticsearch
        # https://github.com/elastic/elasticsearch-docker/issues/32
        # NOTE:
        # This is to fix the permission on the volume
        # By default elasticsearch container is not run as
        # non root user.
        # https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#_notes_for_production_use_and_defaults
        - name: fix-the-volume-permission
          image: busybox
          command: ["sh", "-c", "chown -R 1000:1000 /usr/share/elasticsearch/data"]
          securityContext:
            privileged: true
          volumeMounts:
            - name: es-data
              mountPath: /usr/share/elasticsearch/data
        # NOTE:
        # To increase the default vm.max_map_count to 262144
        # https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker-cli-run-prod-mode
        - name: increase-the-vm-max-map-count
          image: busybox
          command: ["sysctl", "-w"]
          args: ["vm.max_map_count=262144"]
          securityContext:
            privileged: true
        # To increase the ulimit
        # https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#_notes_for_production_use_and_defaults
        - name: increase-fd-ulimit
          image: busybox
          command: ["sh", "-c", "ulimit", "-n"] 
          args: ["65536"]
          securityContext:
            privileged: true
      containers:
        - name: fusionauth-search
          image: docker.elastic.co/elasticsearch/elasticsearch:6.3.1
          env:
            - name: CLUSTER_NAME
              value: cluster.name=fusionauth
            - name: BOOSTRAP_MEMORY_LOCK
              value: bootstrap.memory_lock=true
            - name: ES_JAVA_OPTS
              value: -Xms256m -Xmx256m
            - name: discoveryzen
              value: discovery.seed_host=elasticsearch1
          resources:
            limits:
              memory: "1G"
              cpu: "500m"
          ports:
            - containerPort: 9200
              name: http
            - containerPort: 9300
              name: transport
          volumeMounts:
            - mountPath: /usr/share/elasticsearch/data
              name: es-data
      restartPolicy: Always
      volumes:
        - name: es-data
          persistentVolumeClaim:
            claimName: es-data
