How to scrape only a single port of a pod using kubernetes_sd_config

Hello,

I have been trying to scrape targets in my Kubernetes namespace using kubernetes_sd_config. The pods have 2 processes- instrumentation server and application server. With the below annotations in deployment, Prometheus still has 2 targets. Let me know how can I enable it for only a single port.

        prometheus.io/port: '8081'
        prometheus.io/scrape: 'true'

Welcome to the community.

We will need more details to help you. It also feels like a question related to Prometheus-Operator, did you try to reach that community more specifically?

Hello @roidelapluie, Thanks.

The Prometheus deployment is not done through the operator as we are still not there with OCP(lower version without operator support). What we have is a single pod(single container) that has 2 HTTP servers running(port-8080/port-8081)
Port - 8080 is for application backend APIs.
Port - 8081 is for instrumentation APIs(metrics, version, health)

We only want a single scrape target(pod:8081) but we see 2 targets in our case with kubernetes_sd_config. Hope I am able to describe the problem.

Can you provide your kubernetes_sd_config ?

Below is the config

- job_name: optisam
  scrape_interval: 10s
  scrape_timeout: 10s
  metrics_path: /metrics
  scheme: http
  kubernetes_sd_configs:
  - api_server: null
    role: pod
    namespaces:
      names:
      - int-optisam
  relabel_configs:
  - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
    separator: ;
    regex: "true"
    replacement: $1
    action: keep

Could you try:

  relabel_configs:
  - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
    regex: "true"
    replacement: $1
    action: keep
  - source_labels: [__address__;__meta_kubernetes_pod_annotation_prometheus_io_port]
    target_label: __address__
    regex: "(.*:)\d+;(\d+)"
    replacement: $1$2
1 Like

It works. Thanks for your help.