---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: filebeat
  namespace: monitoring
  labels:
    k8s-app: filebeat
spec:
  selector:
    matchLabels:
      k8s-app: filebeat
  template:
    metadata:
      labels:
        k8s-app: filebeat
    spec:
      serviceAccountName: filebeat
      terminationGracePeriodSeconds: 30
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      containers:
      - name: filebeat
        image: docker.elastic.co/beats/filebeat:7.15.1
        args: [
#          "-c", "/etc/filebeat.yml",
          "-e",
        ]
        env:
        - name: ELASTICSEARCH_HOST
          value: elastic-prod-master-headless
        - name: ELASTICSEARCH_PORT
          value: "9200"
            #        - name: ELASTICSEARCH_USERNAME
            #          value: elastic
            #        - name: ELASTICSEARCH_PASSWORD
            #          value: changeme
            #        - name: ELASTIC_CLOUD_ID
            #          value:
            #        - name: ELASTIC_CLOUD_AUTH
            #          value:
        - name: REDIS_HOSTS
          value: "['redis-master:6379']"
        - name: LOG_TYPE
          value: k3s_cluster
        - name: CLUSTER_NAME
          value: k3s_prod
        - name: REDIS_KEY_NAME
          value: "k3s"
        - name: BUFFER_FILE
          value: "/var/buffer/filebeat/filebeat_spool.dat"
        - name: BUFFER_FILE_SIZE
          value: "1024MiB"
        - name: BUFFER_PAGE_SIZE
          value: "4KiB"
        - name: BUFFER_FLUSH_SIZE
          value: "10MiB"
        - name: BUFFER_FLUSH_TIMEOUT
          value: "1s"
        - name: BUFFER_FLUSH_EVENTS
          value: "50000"
        - name: REDIS_ENABLE_SSL
          value: "false"
        - name: REDIS_SSL_VERIFY
          value: "none"
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        securityContext:
          runAsUser: 0
        resources:
          limits:
            cpu: 100m
            memory: 200Mi
          requests:
            cpu: 10m
            memory: 100Mi
        volumeMounts:
        - name: filebeat-config-volume
          mountPath: /usr/share/filebeat/filebeat.yml
          subPath: filebeat.yml
          readOnly: true
        - name: filebeat-inputs-volume
          mountPath: /usr/share/filebeat/inputs.d
          readOnly: true
        - name: filebeat-modules-volume
          mountPath: /usr/share/filebeat/modules.d
          readOnly: true
        - name: data
          mountPath: /usr/share/filebeat/data
        - name: varlibdockercontainers
          mountPath: /var/lib/docker/containers
          readOnly: true
        - name: varlog
          mountPath: /var/log
          readOnly: true
        - name: redis-certs
          mountPath: "/etc/filebeat/ssl/"
      volumes:
      - name: filebeat-config-volume
        configMap:
          defaultMode: 0640
          name: filebeat-config
      - name: filebeat-inputs-volume
        configMap:
          defaultMode: 0640
          name: filebeat-inputs
      - name: filebeat-modules-volume
        configMap:
          defaultMode: 0640
          name: filebeat-modules
      - name: varlibdockercontainers
        hostPath:
          path: /var/lib/docker/containers
      - name: varlog
        hostPath:
          path: /var/log
      # data folder stores a registry of read status for all files, so we don't send everything again on a Filebeat pod restart
      - name: data
        hostPath:
          # When filebeat runs as non-root user, this directory needs to be writable by group (g+w).
          path: /var/lib/filebeat-data
          type: DirectoryOrCreate
      - name: redis-certs
        secret:
          secretName: redis-certs
          defaultMode: 256

