---
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.6.2.0
        command: [ "/bin/bash", "-ce", "/entrypoint.sh"  ]
        env:
        # - name: ESURL
        #   value: "http://es.edoc2:9200"
        #- name: ESNAME
        #  value: elastic
        #- name: ESPASSWORD
        #  value: changeme
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        securityContext:
          runAsUser: 0
        resources:
          limits:
            memory: 1200Mi
          requests:
            cpu: 800m
            memory: 1000Mi
        volumeMounts:
        - name: metricbeat-config
          # mountPath: /etc/metricbeat.yml
          mountPath: /usr/share/metricbeat/bin/metricbeat.yml
          readOnly: true
          subPath: metricbeat.yml
        # - name: modules
        #   mountPath: /usr/share/metricbeat/modules.d
        #   readOnly: true
        - name: filebeat-config
          mountPath: /usr/share/filebeat/bin/filebeat.yml
          # readOnly: true
          subPath: filebeat.yml
        - name: entrypointsh
          mountPath: /entrypoint.sh
          subPath: entrypoint.sh
        - name: dockersock
          mountPath: /var/run/docker.sock
        # - name: mtab
        #   mountPath: /etc/mtab
        #   readOnly: true
        - 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: mtab
        hostPath:
          path: /etc/mtab
      - name: metricbeat-config
        configMap:
          defaultMode: 0600
          name: metricbeat-daemonset-config
      - 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: 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
