Prometheus config to ignore scraping of metrics for a specific namespace in Kubernetes

2/24/2021

Is there a way to configure prometheus to ignore scraping metrics for all the resources belonging to a particular namespace? I am not able to figure it out by reading the documentation.

-- Aman
configuration
istio
kubernetes
monitoring
prometheus

1 Answer

2/24/2021

You can drop targets with relabel_config by using drop action. From the documentation:

drop: Drop targets for which regex matches the concatenated source_labels.

Example:

  relabel_configs:
  # This will ignore scraping targets from 'ignored_namespace_1', 
  # 'ignored_namespace_2', and 'ignored_namespace_N'.
  - source_labels: [__meta_kubernetes_namespace]
    action: drop
    regex: ignored_namespace_1|ignored_namespace_2|ignored_namespace_N
-- anemyte
Source: StackOverflow