<p>I'm trying to configure prometheus to target kubernetes pods in a namespace, but only port 8081, even though the pods expose both 8080 and 8081. With this configuration:</p>
<pre><code>      - job_name: 'api'
        metrics_path: &quot;/actuator/prometheus&quot;
        kubernetes_sd_configs:
          - role: endpoints
            namespaces:
              names:
                - &quot;api&quot;
</code></pre>
<p>I get two targets for each pod - one for each port. I can't figure out how to narrow down the targets further to just the 8081 ports. Thanks for any help with this!</p>

Kevin
<p>You can use the regex in <strong>kubernete_sd_configs</strong> to match the port and endpoint you are trying to target.
Example :</p>
<pre><code>- job_name: my_job
  metrics_path: /metrics
  kubernetes_sd_configs:
   - roles: endpoints
     namespaces:
      name:
       - api
  relable_configs:
   - source_labels: [_meta_kubernetes_service_name,_meta_kubernetes_endpoint_port_name]
     regex: my_service;8081
</code></pre>
<p>Here we are using regex to match the endpoints with the <strong>service name</strong> called <code>my_service</code> and <strong>port</strong> number <code>8081</code>. If you need to scrape the metrics from different endpoints then you need to modify the regular expression accordingly.</p>
<p>Make sure you mentioned appropriate service discovery annotations on your kubernetes services to ensure the prometheus can discover them using <strong>kubernetes_sd_configs</strong>.</p>
<p>For more information have a glance at this <a href="https://se7entyse7en.dev/posts/how-to-set-up-kubernetes-service-discovery-in-prometheus/" rel="nofollow noreferrer">blog</a> written by Lou Marvin Caraig.</p>
<p>Check this <a href="https://discuss.prometheus.io/t/how-to-scrape-only-a-single-port-of-a-pod-using-kubernetes-sd-config/203/5" rel="nofollow noreferrer">prometheus forum</a> discussion for more inputs.</p>
