Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promtail kubernetes host log & drop namespace #748

Closed
prasenforu opened this issue Jul 13, 2019 · 14 comments
Closed

Promtail kubernetes host log & drop namespace #748

prasenforu opened this issue Jul 13, 2019 · 14 comments
Labels
stale A stale issue or PR that will automatically be closed. type/question

Comments

@prasenforu
Copy link

I am running Loki with promtail in kubernetes platform.

Promtail running as a daemonset and promtail configuration as follows ..

    scrape_configs:
      - job_name: kubernetes-pods
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - source_labels:
          - __meta_kubernetes_pod_node_name
          target_label: __host__
        - action: drop
          regex: ^$
          source_labels:
          - __meta_kubernetes_pod_label_name
        - action: replace
          replacement: $1
          separator: /
          source_labels:
          - __meta_kubernetes_namespace
          - __meta_kubernetes_pod_label_name
          target_label: job
        - action: drop
          regex: default
          source_labels:
          - __meta_kubernetes_namespace
        - 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: labelmap
          regex: __meta_kubernetes_pod_label_(.+)
        - replacement: /var/log/pods/$1/*.log
          separator: /
          source_labels:
          - __meta_kubernetes_pod_uid
          - __meta_kubernetes_pod_container_name
          target_label: __path__
      - job_name: kubernetes-pods-app
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - source_labels:
          - __meta_kubernetes_pod_node_name
          target_label: __host__
        - action: drop
          regex: .+
          source_labels:
          - __meta_kubernetes_pod_label_name
        - action: replace
          replacement: $1
          separator: /
          source_labels:
          - __meta_kubernetes_namespace
          - __meta_kubernetes_pod_label_app
          target_label: job
        - action: drop
          regex: default
          source_labels:
          - __meta_kubernetes_namespace
        - 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: labelmap
          regex: __meta_kubernetes_pod_label_(.+)
        - replacement: /var/log/pods/$1/*.log
          separator: /
          source_labels:
          - __meta_kubernetes_pod_uid
          - __meta_kubernetes_pod_container_name
          target_label: __path__

I have a two question?

Q1# How can I capture kubernetes host log (/var/log/messages) along with hostname and what will be config file definition?

I checked with https://github.com/grafana/loki/blob/master/docs/promtail-examples.md

But looks like its for static configuration.

scrape_configs:
 - job_name: system
   pipeline_stages:
   - docker:
   static_configs:
   - targets:
      - localhost
     labels:
      job: varlogs
      host: yourhost
      __path__: /var/log/*.log

Q2# How can I DROP all logs from particular namespace (default)

I tried with following but no luck, not sure if I missing anything ...
I added below text in job "kubernetes-pods" & "kubernetes-pods-app"

        - action: drop
          regex: default
          source_labels:
          - __meta_kubernetes_namespace
@stale
Copy link

stale bot commented Sep 3, 2019

This issue has been automatically marked as stale because it has not had any activity in the past 30 days. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale A stale issue or PR that will automatically be closed. label Sep 3, 2019
@stale stale bot closed this as completed Sep 10, 2019
@minhdanh
Copy link
Contributor

@prasenforu Could you have your questions answered? I'm wondering the same.

@zhushilu
Copy link

Take a look at this article. https://itnext.io/grafana-logging-using-loki-45665916aec9

@prasenforu
Copy link
Author

I followed that URL but not working.

Finally I moved to fluent-bit

@cyriltovena
Copy link
Contributor

cyriltovena commented Oct 28, 2019

Q1 - We don't add the host automatically but you can use external label flags see #510

Q2 - Labels after relabeling are accessible in pipeline stage, we support dropping via the match stage so that should work now.

@imranrazakhan
Copy link

@prasenforu how you able to exclude namespace in fluent-bit?

@cloudcafetech
Copy link

@imranrazakhan

in fluent-bit conf add in INPUT section

    [INPUT]
        Name           tail
        Tag            kube.*
        Path           /var/log/containers/*.log
        Exclude_Path   /var/log/containers/*_monitoring_*.log,/var/log/containers/*_logging_*.log,/var/log/containers/*_kube-system_*.log,/var/log/containers/*_kube-node-lease_*.log,/var/log/containers/*_kube-public_*.log
        Parser         docker
        DB             /run/fluent-bit/flb_kube.db
        Mem_Buf_Limit  5MB

@cloudcafetech
Copy link

@cyriltovena

Q2 - Labels after relabeling are accessible in pipeline stage, we support dropping via the match stage so that should work now.

Can you please help me to get sample configuration for dorp namespace

@eranbetzalel
Copy link

eranbetzalel commented Nov 25, 2021

Drop all namespaces but "default" on promtail

    - match:
        pipeline_name: "drop-all"
        selector: '{namespace!="default"}'
        action: drop

This is a working example.

@aroldobossoni
Copy link

aroldobossoni commented Aug 24, 2022

Drop all namespaces but "default" on promtail

    - match:
        pipeline_name: "drop-all"
        selector: '{namespace!="default"}'
        action: drop

This is a working example.

This setting only works on newer versions of Promtail in Chart version: 3.6.1 Promtail version: 2.2.1 does not work.

@mhoyer
Copy link

mhoyer commented Nov 25, 2022

It might be helpful to also point out that there is a probably more efficient way to filter per namespace. Thus, this only works when it's about an include list of valid namespaces.

There is the kubernetes_sd_config section which enables you to configure namespaces. Promtail will then only discover and tail on log files of pods/containers for that given set of namespaces. Ergo, less work (IOps) to do for Promtail.

scrape_configs:
- job_name: kubernetes-pods
  pipeline_stages:
    - cri: {}
  kubernetes_sd_configs:
    - role: pod
      namespaces:
        names:
          - default
          # - another-namespace

@mhoyer
Copy link

mhoyer commented Nov 29, 2022

ℹ️ With my recent comment I just trapped into the issue mentioned here: #808 (comment)

Filtering log files by e.g. just the "default" namespace led to zero matching files. Thus, nothing to be tailed by promtail. Which resulted in a broken readiness check by Kubernetes. I managed to simply make this ready check pass again by setting server.health_check_target: false (ref #1919).

@kuzm1ch
Copy link

kuzm1ch commented Mar 7, 2023

jfyi: putting helm values which work for me

      values: |
        config:
          snippets:
            pipelineStages:
            - drop:
                source:     "namespace"
                expression: "(kube-public|kube-system)"

@furkanalp
Copy link

jfyi: putting helm values which work for me

      values: |
        config:
          snippets:
            pipelineStages:
            - drop:
                source:     "namespace"
                expression: "(kube-public|kube-system)"

Thank you for sharing your solution with us. It worked for me as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale A stale issue or PR that will automatically be closed. type/question
Projects
None yet
Development

No branches or pull requests