---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: metricbeat
  namespace: kube-system
  labels:
    k8s-app: metricbeat
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: metricbeat-daemonset-config
  namespace: kube-system 
  labels:
    k8s-app: metricbeat
data:
  metricbeat.yml: |-
    metricbeat.config.modules:
      # Mounted `metricbeat-daemonset-modules` configmap:
      path: ${path.config}/modules.d/*.yml
      # Reload module configs as they change:
      reload.enabled: false
    # To enable hints based autodiscover uncomment this:
    #metricbeat.autodiscover:
    #  providers:
    #    - type: kubernetes
    #      host: ${NODE_NAME}
    #      hints.enabled: true
    processors:
      - add_cloud_metadata:
    output.elasticsearch:
      hosts: ['${ELASTICSEARCH_HOST:middle-proxy.edoc2}:${ELASTICSEARCH_PORT:9200}']
      #username: ${ELASTICSEARCH_USERNAME}
      #password: ${ELASTICSEARCH_PASSWORD}
    setup.template.settings:
      index.number_of_shards: 1
      index.codec: best_compression
      index.max_result_window: 2000000000
    setup.template.overwrite: true
    setup.template.enabled: true
    setup.template.fields: "fields.yml"
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: metricbeat-daemonset-modules
  namespace: kube-system
  labels:
    k8s-app: metricbeat
data:
  system.yml: |-
    - module: system
      metricsets:
        - load
        - cpu
        - memory
        - fsstat
        - socket
        - socket_summary
        - process_summary 
      period: 10s
      cpu.metrics: [percentages, normalized_percentages]
    
    - module: system
      metricsets: ["filesystem"]
      filesystem.ignore_types: [tmpfs,overlay,rootfs,vfat,devtmpfs,proc,devpts,cgroup,mqueue,sysfs]
      period: 10s
      processors:
        - drop_event.when.regexp:
            system.filesystem.mount_point: '^/hostfs\/(sys|boot|var|run|cgroup|proc|dev|etc|host|merged)($|/)'
        - drop_event.when.regexp:
            system.filesystem.mount_point: '^/(sys|dev|proc)($|/)'
    
    - module: system
      metricsets: ["network"]
      period: 10s
      processors:
        - drop_event.when.regexp:
            system.network.name: '^(veth|docker|virbr|cali|dummy0|lo)'
    
    - module: system
      metricsets: ["diskio"]
      period: 10s
      processors:
        - drop_event.when.regexp:
            system.diskio.name: '^(dm|sr)'
    
    - module: docker
      metricsets:
        - "container"
        - "cpu"
        - "diskio"
        - "event"
        - "healthcheck"
        - "info"
        - "image"
        - "memory"
        - "network"
      hosts: ["unix:///var/run/docker.sock"]
      period: 10s
      processors:
        - dissect:
            tokenizer: "%{}_%{service_name}"
            field: "docker.container.labels.com_docker_swarm_service_name"
            target_prefix: "container"

  kubernetes.yml: |-
    - module: kubernetes
      metricsets:
        - node
        - system
        - pod
        - container
        - volume
      period: 10s
      host: ${NODE_NAME}
      hosts: ["https://192.168.251.252:8443"]
      # If using Red Hat OpenShift remove the previous hosts entry and 
      # uncomment these settings:
      #hosts: ["https://${HOSTNAME}:10250"]
      #bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      #ssl.certificate_authorities:
        #- /var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt
---
# 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:ecm-apm-v6.5.0.0
        args: [
          "-c", "/etc/metricbeat.yml",
          "-e",
          "-system.hostfs=/hostfs",
        ]
        env:
        - name: ELASTICSEARCH_HOSTS
          value: "middle-proxy.edoc2"
        - name: APM_ESURL
          value: "http://middle-proxy.edoc2:9200"
        - name: ELASTICSEARCH_PORT
          value: "9200"
        #- name: ELASTICSEARCH_USERNAME
        #  value: elastic
        #- name: ELASTICSEARCH_PASSWORD
        #  value: changeme
        - 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
          readOnly: true
          subPath: metricbeat.yml
        - name: modules
          mountPath: /usr/share/metricbeat/modules.d
          readOnly: true
        - 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: 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
