<p>I have deployed istio on kubernetes, and I installed prometheus from istio addons. My goal is to only monitor some pods of one application(such as all pods of bookinfo application). The job definition for monitoring pods is as below:</p>
<pre><code>    - bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      job_name: kubernetes-nodes-cadvisor
      kubernetes_sd_configs:
      - role: node
      relabel_configs:
      - action: labelmap
        regex: __meta_kubernetes_node_label_(.+)
      - replacement: kubernetes.default.svc:443
        target_label: __address__
      - regex: (.+)
        replacement: /api/v1/nodes/$1/proxy/metrics/cadvisor
        source_labels:
        - __meta_kubernetes_node_name
        target_label: __metrics_path__
      scheme: https
      tls_config:
        ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        insecure_skip_verify: true
</code></pre>
<p>My problem is that I don't know how to monitor only one namespace's pods. For example, I deploy the bookinfo application in a namespace named Book. I only want the metrics of pods from namespace Book. However, prometheus will collect all pods metrics of the nodes. Instead of changing annotations of the application like <a href="https://stackoverflow.com/questions/59070150/monitor-only-one-namespace-metrics-prometheus-with-kubernetes">Monitor only one namespace metrics - Prometheus with Kubernetes</a>, I want know if there is a method to select only one namespace by changing the job definition above. Or is there some way to choose the monitor pods by it's labels?</p>

gxxxh
<p>The following will match all target pods with label: <code>some_label</code> with any value.</p>
<pre><code>relabel_configs:
  - action: keep
    source_labels: [__meta_kubernetes_pod_label_some_label]
    regex: (.*)
</code></pre>
<p>If you want to keep targets with label: <code>monitor</code> and value: <code>true</code> you would do:</p>
<pre><code>relabel_configs:
  - action: keep
    source_labels: [__meta_kubernetes_pod_label_monitor]
    regex: true
</code></pre>
<p>All pods that don't match it will be dropped from scraping.</p>
<p>The same you should be able to do for namespaces:</p>
<pre><code>relabel_configs:
  - action: keep
    source_labels: [__meta_kubernetes_namespace]
    regex: Book
</code></pre>
<hr />
<p>EDIT &gt;</p>
<blockquote>
<p>is there a way to change the [container_label_io_kubernetes_container_name] labels into &quot;container_name&quot;?</p>
</blockquote>
<p>Try this:</p>
<pre><code>relabel_configs:
  - action: replace
    source_labels: [container_label_io_kubernetes_container_name]
    target_label: container_name
</code></pre>
<p>It's all explained in <a href="https://prometheus.io/docs/prometheus/latest/configuration/configuration/" rel="nofollow noreferrer">prometheus docs about configuration</a></p>
