# borrowed from https://www.digitalocean.com/community/tutorials/how-to-set-up-an-elasticsearch-fluentd-and-kibana-efk-logging-stack-on-kubernetes

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: home-es
  namespace: es
spec:
  serviceName: elasticsearch
  replicas: 1
  selector:
    matchLabels:
      app: es
  template:
    metadata:
      labels:
        app: es
    spec:
      imagePullSecrets:
        - name: regcred
      containers:
      - name: elasticsearch
        image: docker.elastic.co/elasticsearch/elasticsearch:8.2.3
        resources:
          requests:
            cpu: 1200m
            memory: 2000Mi
          limits:
            cpu: 1200m
            memory: 2000Mi
        ports:
        - containerPort: 9200
          name: rest
          protocol: TCP
        - containerPort: 9300
          name: inter-node
          protocol: TCP
        volumeMounts:
        - name: data
          mountPath: /usr/share/elasticsearch/data
        env:
          - name: cluster.name
            value: home-es-single
          - name: xpack.security.enabled
            value: "false"
          - name: node.name
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          # - name: discovery.seed_hosts
          #   value: "es-0.elasticsearch.es.svc.cluster.local"
          # - name: cluster.initial_master_nodes
          #   value: "es-0"
          - name: discovery.type
            value: single-node
          - name: ES_JAVA_OPTS
            value: "-Xms1200m -Xmx1200m"
      initContainers:
      - name: fix-permissions
        image: mirror.gcr.io/alpine
        command: ["sh", "-c", "chown -R 1000:1000 /usr/share/elasticsearch/data"]
        securityContext:
          privileged: true
        volumeMounts:
        - name: data
          mountPath: /usr/share/elasticsearch/data
      - name: increase-vm-max-map
        image: mirror.gcr.io/alpine
        command: ["sysctl", "-w", "vm.max_map_count=262144"]
        securityContext:
          privileged: true
      - name: increase-fd-ulimit
        image: mirror.gcr.io/alpine
        command: ["sh", "-c", "ulimit -n 65536"]
        securityContext:
          privileged: true
  volumeClaimTemplates:
  - metadata:
      name: data
      labels:
        app: es
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: nfs-client
      resources:
        requests:
          storage: 150Gi

---

apiVersion: v1
kind: Service
metadata:
  name: elasticsearch
  labels:
    name: elasticsearch
spec:
  type: ClusterIP
  selector:
    app: es
  ports:
    - name: elasticsearch
      targetPort: rest
      port: 9200
      protocol: TCP
    - name: es
      targetPort: inter-node
      port: 9300
      protocol: TCP
---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: es-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    kubernetes.io/tls-acme: "true"
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/auth-url: "https://id.junv.cc/oauth2/auth"
    nginx.ingress.kubernetes.io/auth-signin: "https://id.junv.cc/oauth2/start?rd=https%3A%2F%2F$host$request_uri"
spec:
  tls:
  - hosts:
    - es.junv.cc
    secretName: es-ingress-tls
  rules:
  - host: es.junv.cc
    http:
      paths:
      - path: /
        backend:
          service:
            name: elasticsearch
            port:
              number: 9200
        pathType: Prefix

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kibana
  labels:
    app: kibana
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
  selector:
    matchLabels:
      app: kibana
  template:
    metadata:
      labels:
        app: kibana
        name: kibana
    spec:
      imagePullSecrets:
        - name: regcred
      containers:
        - name: kibana
          image: docker.elastic.co/kibana/kibana:8.2.3
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 5601
              name: http-kibana
              protocol: TCP
          env:
            - name: ELASTICSEARCH_HOSTS
              value: http://elasticsearch.es.svc.cluster.local:9200
          volumeMounts:
            - name: config
              mountPath: /usr/share/kibana/config
          resources:
            requests:
              cpu: 200m
              memory: 512Mi
            limits:
              cpu: 500m
              memory: 1024Mi
      volumes:
        - name: "config"
          configMap:
            name: kibana-config
---

apiVersion: v1
kind: Service
metadata:
  name: kibana
  labels:
    name: kibana
spec:
  type: ClusterIP
  selector:
    app: kibana
  ports:
    - name: kibana
      targetPort: http-kibana
      port: 5601
      protocol: TCP
---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kibana-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    kubernetes.io/tls-acme: "true"
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/auth-url: "https://id.junv.cc/oauth2/auth"
    nginx.ingress.kubernetes.io/auth-signin: "https://id.junv.cc/oauth2/start?rd=https%3A%2F%2F$host$request_uri"
spec:
  tls:
  - hosts:
    - kibana.junv.cc
    secretName: kibana-tls
  rules:
  - host: kibana.junv.cc
    http:
      paths:
      - backend:
          service:
            name: kibana
            port:
              number: 5601
        path: /
        pathType: Prefix

---

apiVersion: v1
kind: ConfigMap
metadata:
  name: kibana-config
  namespace: es
data:
  kibana.yml: |
    ---
    server.name: kibana
    server.publicBaseUrl: https://kibana.junv.cc
    server.host: "0.0.0.0"
    status.allowAnonymous: true
    xpack.security.encryptionKey: "MTExMXNzY2JiZ2dnc2RzZmRmdnIyMzhyKigpJjclCg=="
    xpack.encryptedSavedObjects.encryptionKey: MTExMXNzY2JiQCMlXiZnZ2dzZHNmZGZ2cjIzOHIqKCkmNyUK
    xpack.reporting.roles.enabled: false
    xpack.reporting.encryptionKey: "MTExMXNzY2JiQCMlXiZnZ2dzZHNmMzlldG5zdnhkZnZyMjM4ciooKSY3JQo="
    # xpack.monitoring.enabled: false
    # xpack.monitoring.ui.container.elasticsearch.enabled: false
    # xpack.monitoring.kibana.collection.enabled: false
    # xpack.security.enabled: false
    # logging.silent: false
    # logging.quiet: false

---

apiVersion: v1
kind: ConfigMap
metadata:
  name: elasticsearch-config
  namespace: es
data:
  elasticsearch.yml: |
    s3:
      client:
        default:
          endpoint: minio.db.svc.cluster.local:9000
          protocol: http
          region: ap-southeast-2
