首页 > 解决方案 > ServiceMonitor 使用 TLS 抓取 Spring 微服务

问题描述

我们在 AWS EKS 集群中部署了一堆微服务。这些微服务中的每一个都启用了 spring boot actuator/prometheus-micrometer 依赖项。我们可以通过 /actuator/prometheus 抓取指标

从我们可用的 prometheus 集群中,我部署了一个 servicemonitor 来抓取这些指标。

servicemonitor 看起来像这样:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    app: spring-apps
    release: prometheus
spec:
  endpoints:
    - paths: "/actuator/prometheus"
      interval: 10s
      port: http
      targetPort: 8443
      scheme: https
      tlsConfig:
        insecureverify: "true"
  selector:
    matchLabels:
      scrape.actuator: "true"
  namespaceSelector:
    any: true  

一切正常/当我们有 tlsConfig.insecureverify: "true" 时,我们能够抓取指标

但是,当我关闭 tlsConfig.insecureverify: "false" 并应用 ca 文件时,它根本无法抓取。

这就是我们为微服务设置的方式。我们有一个指向证书的入口控制器

证书已被获取,我们使用以下命令从中生成了一个秘密

kubectl -n somenamespace create secret generic api.cer --from-file=api.cer=somecertificate.cer

秘密是安装在 prometheus pod 中的卷,并且服务监视器已更新为下面给出的。

    apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    app: spring-apps
    release: prometheus
spec:
  endpoints:
    - paths: "/actuator/prometheus"
      interval: 10s
      port: http
      targetPort: 8443
      scheme: https
      tlsConfig:
        insecureverify: "false"
        serverName: "someapi.com"
        caFile: "/etc/prometheus/secrets/api.cer"
  selector:
    matchLabels:
      scrape.actuator: "true"
  namespaceSelector:
    any: true  

但是,我们无法再抓取指标。对于 promql up{namespace="app"},我确实看到列出了微服务,但值为 0。例如:hello-world:0

请帮忙,到目前为止,互联网上的很多搜索都没有帮助

标签: spring-bootprometheusspring-boot-actuatorspring-micrometerservicemonitor

解决方案


推荐阅读