apiVersion: v1
kind: ConfigMap
metadata:
  name: promtail
  labels:
    app: promtail
data:
  promtail.yaml: |
    client:
      batchwait: 1s # Maximum wait period before sending batch
      batchsize: 102400 # Maximum batch size to accrue before sending, unit is byte

      timeout: 10s # Maximum time to wait for server to respond to a request

      backoff_config:
        minbackoff: 100ms # Initial backoff time between retries
        maxbackoff: 5s # Maximum backoff time between retries
        maxretries: 5 # Maximum number of retries when sending batches, 0 means infinite retries

    server:
      http_listen_port: 3101
    positions:
      filename: /run/promtail/positions.yaml
    target_config:
      sync_period: 10s # Period to resync directories being watched and files being tailed

    scrape_configs:
    - job_name: kubernetes-pods
      kubernetes_sd_configs:
      - role: pod
      relabel_configs:
      - action: keep
        source_labels: [__meta_kubernetes_pod_label_app]
        regex: prometheus|prometheus-meta|loki|promtail|prombench-test-[\d]+
      - source_labels: [__meta_kubernetes_pod_label_app]
        target_label: __service__
      - source_labels: [__meta_kubernetes_pod_node_name]
        target_label: __host__
      - action: replace
        replacement: $1
        separator: /
        source_labels: [__meta_kubernetes_namespace, __service__]
        target_label: job
      - action: replace
        source_labels: [__meta_kubernetes_namespace]
        target_label: namespace
      - action: replace
        source_labels: [__meta_kubernetes_pod_name]
        target_label: instance
      - action: replace
        source_labels: [__meta_kubernetes_pod_container_name]
        target_label: container_name
      - action: replace
        source_labels: [__meta_kubernetes_pod_label_prometheus]
        target_label: prometheus
      - action: replace
        source_labels: [__meta_kubernetes_pod_label_promtail]
        target_label: promtail
      - 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: promtail
  labels:
    app: promtail
spec:
  selector:
    matchLabels:
      app: promtail
      promtail: meta
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: promtail
        promtail: meta
    spec:
      serviceAccountName: promtail
      containers:
        - name: promtail
          image: grafana/promtail:1.4.1
          imagePullPolicy: IfNotPresent
          args:
            - "-config.file=/etc/promtail/promtail.yaml"
            - "-client.url=http://loki:3100/api/prom/push"
          volumeMounts:
            - name: config
              mountPath: /etc/promtail
            - name: run
              mountPath: /run/promtail
            - name: pods
              mountPath: /var/log/pods
              readOnly: true
            - name: docker
              mountPath: /var/lib/docker/containers
              readOnly: true
          env:
          - name: HOSTNAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          ports:
            - containerPort: 3101
              name: http-metrics
          securityContext:
            readOnlyRootFilesystem: true
            runAsGroup: 0
            runAsUser: 0
          readinessProbe:
            failureThreshold: 5
            httpGet:
              path: /ready
              port: http-metrics
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 1
      nodeSelector:
        node-name: main-node
      tolerations:
        - key: node-role.kubernetes.io/master
          effect: NoSchedule
      volumes:
        - name: config
          configMap:
            name: promtail
        - name: run
          hostPath:
            path: /run/promtail
        - name: docker
          hostPath:
            path: /var/lib/docker/containers
        - name: pods
          hostPath:
            path: /var/log/pods
