如何在Prometheus中针对特定端口的终端点进行定位

huangapple go评论88阅读模式
英文:

How do I target endpoints with specific ports in prometheus

问题

我正在尝试配置Prometheus以针对命名空间中的Kubernetes Pod,但只针对端口8081,尽管Pod同时暴露了8080和8081端口。使用这个配置:

- job_name: 'api'
  metrics_path: "/actuator/prometheus"
  kubernetes_sd_configs:
    - role: endpoints
      namespaces:
        names:
          - "api"

我会为每个Pod获得两个目标 - 每个端口一个。我无法弄清楚如何进一步缩小目标,仅限于8081端口。感谢任何关于此的帮助!

英文:

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:

      - job_name: 'api'
        metrics_path: "/actuator/prometheus"
        kubernetes_sd_configs:
          - role: endpoints
            namespaces:
              names:
                - "api"

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!

答案1

得分: 2

您可以使用kubernete_sd_configs中的正则表达式来匹配您要定位的端口和端点。示例:

- job_name: my_job
  metrics_path: /metrics
  kubernetes_sd_configs:
    - roles: endpoints
      namespaces:
        name:
          - api
  relabel_configs:
    - source_labels: [_meta_kubernetes_service_name,_meta_kubernetes_endpoint_port_name]
      regex: my_service;8081

在这里,我们使用正则表达式来匹配带有服务名称my_service端口号为8081的端点。如果您需要从不同的端点抓取指标,那么您需要相应地修改正则表达式。

确保在您的Kubernetes服务上提到适当的服务发现注释,以确保Prometheus可以使用kubernetes_sd_configs来发现它们。

欲了解更多信息,请查看由Lou Marvin Caraig编写的这篇博客

查看这个Prometheus论坛的讨论以获取更多信息。

英文:

You can use the regex in kubernete_sd_configs to match the port and endpoint you are trying to target.
Example :

- 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

Here we are using regex to match the endpoints with the service name called my_service and port number 8081. If you need to scrape the metrics from different endpoints then you need to modify the regular expression accordingly.

Make sure you mentioned appropriate service discovery annotations on your kubernetes services to ensure the prometheus can discover them using kubernetes_sd_configs.

For more information have a glance at this blog written by Lou Marvin Caraig.

Check this prometheus forum discussion for more inputs.

huangapple
  • 本文由 发表于 2023年3月8日 15:24:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定