apiVersion: v1
kind: Namespace
metadata:
  name: log-collector
  labels:
    name: log-collector
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: log-collector-serviceaccount
  namespace: log-collector
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: log-collector-clusterrole
rules:
  - apiGroups: [""]
    resources:
      - nodes
      - services
      - pods
    verbs:
      - get
      - watch
      - list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: log-collector-clusterrolebinding
subjects:
  - kind: ServiceAccount
    name: log-collector-serviceaccount
    namespace: log-collector
roleRef:
  kind: ClusterRole
  name: log-collector-clusterrole
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: log-collector-config
  namespace: log-collector
data:
  log-collector.yaml: |
    server:
      http_listen_port: 9080
      grpc_listen_port: 0
    clients:
      - url: http://<Applicare_Ip>:3100/loki/api/v1/push
    positions:
      filename: /tmp/positions.yaml
    scrape_configs:
      - job_name: pod-logs
        kubernetes_sd_configs:
          - role: pod
        pipeline_stages:
          - docker: {}
        relabel_configs:
          - source_labels: [__meta_kubernetes_pod_node_name]
            target_label: __host__
          - action: labelmap
            regex: __meta_kubernetes_pod_label_(.+)
          - action: replace
            replacement: $1
            separator: /
            source_labels:
              - __meta_kubernetes_namespace
              - __meta_kubernetes_pod_name
            target_label: job
          - action: replace
            source_labels: [__meta_kubernetes_namespace]
            target_label: namespace
          - action: replace
            source_labels: [__meta_kubernetes_pod_name]
            target_label: pod
          - action: replace
            source_labels: [__meta_kubernetes_pod_container_name]
            target_label: container
          - replacement: /var/log/pods/*$1/*.log
            separator: /
            source_labels:
              - __meta_kubernetes_pod_uid
              - __meta_kubernetes_pod_container_name
            target_label: __path__
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: log-collector-daemonset
  namespace: log-collector
spec:
  selector:
    matchLabels:
      name: log_collector
  template:
    metadata:
      labels:
        name: log_collector
    spec:
      serviceAccount: log-collector-serviceaccount
      containers:
        - name: log-collector-container
          image: grafana/promtail:2.9.0
          args:
            - -config.file=/etc/log-collector/log-collector.yaml
          env:
            - name: HOSTNAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
          volumeMounts:
            - name: logs
              mountPath: /var/log
            - name: log-collector-config
              mountPath: /etc/log-collector
            - mountPath: /var/lib/docker/containers
              name: varlibdockercontainers
              readOnly: true
      volumes:
        - name: logs
          hostPath:
            path: /var/log
        - name: varlibdockercontainers
          hostPath:
            path: /var/lib/docker/containers
        - name: log-collector-config
          configMap:
            name: log-collector-config
