---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: metricbeat
  namespace: kube-system
  labels:
    k8s-app: metricbeat
---
# Deploy a Metricbeat instance per node for node metrics retrieval
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: metricbeat
  namespace: kube-system
  labels:
    k8s-app: metricbeat
spec:
  selector:
    matchLabels:
      k8s-app: metricbeat
  template:
    metadata:
      labels:
        k8s-app: metricbeat
    spec:
      serviceAccountName: metricbeat
      terminationGracePeriodSeconds: 30
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      containers:
      - name: metricbeat
        image: registry.edoc2.com:5000/edoc2v5/metricbasic:v6.5.0.0
        #image: docker.elastic.co/beats/metricbeat:6.7.1
        #args: [
        #  "-c", "/etc/metricbeat.yml",
        #  "-e",
        #  "-system.hostfs=/hostfs",
        #]
        command: [ "/bin/bash", "-ce", "/entrypoint.sh"  ]
        env:
        - name: ELASTICSEARCH_HOSTS
          #value: "middle-proxy.edoc2:9200"
          value: "es.edoc2.svc.cluster.local:9200"
        - name: APM_ESURL
          #value: "http://middle-proxy.edoc2:9200"
          value: "http://es.edoc2.svc.cluster.local:9200"
        #- name: ELASTICSEARCH_PORT
        #  value: "9200"
        - name: ELASTICSEARCH_USERNAME
          value: elastic
        - name: ELASTICSEARCH_PASSWORD
          value: fY9aLwahqCG06tmYYU2E
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        securityContext:
          runAsUser: 0
        resources:
          limits:
            memory: 1200Mi
          requests:
            cpu: 800m
            memory: 1000Mi
        volumeMounts:
        - name: config
          # mountPath: /etc/metricbeat.yml
          mountPath: /usr/share/metricbeat/metricbeat.yml
          readOnly: true
          subPath: metricbeat.yml
        # - name: modules
        #   mountPath: /usr/share/metricbeat/modules.d
        #   readOnly: true
        - name: metricbeat-fields
          mountPath: /usr/share/metricbeat/bin/fields.yml
          # readOnly: true
          subPath: fields.yml
        - name: filebeat-config
          mountPath: /usr/share/filebeat/filebeat.yml
          # readOnly: true
          subPath: filebeat.yml
        - name: filebeat-modules
          #mountPath: /usr/share/filebeat/config/fields.yml
          mountPath: /usr/share/filebeat/config/modules.d
          # readOnly: true
          #subPath: fields.yml
        - name: entrypointsh
          mountPath: /entrypoint.sh
          subPath: entrypoint.sh
        - name: dockersock
          mountPath: /var/run/docker.sock
        - name: proc
          mountPath: /hostfs/proc
          readOnly: true
        - name: cgroup
          mountPath: /hostfs/sys/fs/cgroup
          readOnly: true
        - name: root 
          mountPath: /hostfs
          readOnly: true
      volumes:
      - name: proc
        hostPath:
          path: /proc
      - name: cgroup
        hostPath:
          path: /sys/fs/cgroup
      - name: root
        hostPath:
          path: /
      - name: dockersock
        hostPath:
          path: /run/docker.sock
      - name: config
        configMap:
          defaultMode: 0600
          name: metricbeat-daemonset-config
      # - name: modules
      #   configMap:
      #     defaultMode: 0600
      #     name: metricbeat-daemonset-modules
      - name: metricbeat-fields
        configMap:
          defaultMode: 0600
          name: metricbeat-fields
          items:
            - key: metricbeat-fields.yml
              path: fields.yml
      - name: filebeat-config
        configMap:
          defaultMode: 0600
          name: filebeat-daemonset-config
          items:
            - key: filebeat-config.yml
              path: filebeat.yml
      - name: entrypointsh
        configMap:
          defaultMode: 493
          name: entrypointsh
      - name: filebeat-modules
        configMap:
          defaultMode: 0600
          name: filebeat-daemonset-modules
      - name: data
        hostPath:
          path: /var/lib/metricbeat-data
          type: DirectoryOrCreate
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: metricbeat
subjects:
- kind: ServiceAccount
  name: metricbeat
  namespace: kube-system
roleRef:
  kind: ClusterRole
  name: metricbeat
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: metricbeat
  labels:
    k8s-app: metricbeat
rules:
- apiGroups: [""]
  resources:
  - nodes
  - namespaces
  - events
  - pods
  verbs: ["get", "list", "watch"]
- apiGroups: ["extensions"]
  resources:
  - replicasets
  verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
  resources:
  - statefulsets
  - deployments
  verbs: ["get", "list", "watch"]
- apiGroups:
  - ""
  resources:
  - nodes/stats
  verbs:
  - get
