【问题标题】:Prometheus cannot scrape from spring-boot application over HTTPSPrometheus 无法通过 HTTPS 从 spring-boot 应用程序中抓取
【发布时间】:2022-02-02 04:12:14
【问题描述】:

我正在通过 docker 部署 spring-boot 应用程序和 prometheus 容器,并成功暴露了 spring-boot /actuator/prometheus 端点。但是,当我启用 prometheus 调试日志时,我可以看到它无法抓取指标:

ts=2022-02-02T03:54:46.210Z
caller=scrape.go:1292
level=debug
component="scrape manager"
scrape_pool=spring-actuator
target=https://127.0.0.1:8443/actuator/prometheus/
msg="Scrape failed"
err="Get \"https://127.0.0.1:8443/actuator/prometheus/\": dial tcp 127.0.0.1:8443: connect: connection refused"

我认为这与我设置 spring-boot HTTPS 的方式有关。我在构建我的 spring-boot 应用程序期间使用以下命令生成自签名证书:

keytool
  -genkey
  -alias <alias>
  -dname <dname>
  -keyalg RSA
  -keysize 4096
  -storetype PKCS12
  -keystore <path_to_keystore>
  -validity 3650
  -storepass <keystore_pass>

然后我将证书导出到 .pem 文件,并提取 .crt 和 .key:

openssl pkcs12 -in cert.p12 -out cert.pem -nodes -passin pass:<pass>

这是通过共享卷安装到我的 prometheus 容器中的,该容器有一个 --web.config.file 包含:

tls_server_config:
  cert_file: /path/to/cert.crt
  key_file: /path/to/cert.key

为了更好地衡量,我在 prometheus.yml 配置中添加了 insecure_skip_verify: true

- job_name: 'spring-actuator'
metrics_path: '/actuator/prometheus/'
scrape_interval: 60s
scheme: https
static_configs:
  - targets: [ '127.0.0.1:8443' ]
tls_config:
  insecure_skip_verify: true

【问题讨论】:

    标签: spring-boot openssl prometheus


    【解决方案1】:

    好的,我想我找到了我的问题。我做了两处改动:

    首先,我将 web.config.file 的内容移动到“spring-actuator”下的 prometheus.yml 文件中。 然后我将目标更改为使用我的后端容器的主机名,而不是 127.0.0.1。

    最终结果是一个 prometheus.yml 文件:

    - job_name: 'spring-actuator'
    metrics_path: '/actuator/prometheus/'
    scrape_interval: 60s
    scheme: https
    static_configs:
      - targets: [ 'backend:8443' ]
    tls_config:
      cert_file: /path/to/cert.crt
      key_file: /path/to/cert.key
      insecure_skip_verify: true
    

    所以只是一些愚蠢的错误,不是由我所看到的证书引起的。 :)

    【讨论】:

    • 我假设仅当 prometheus 本身通过 HTTPS 提供服务时才需要 TLS 配置(可能是您的情况)。在我的情况下,我的 Spring Boot 应用程序通过 HTTPS 提供服务,但我的 prometheus 不是,所以我只需在 prometheus.yml 中添加 scheme: https
    【解决方案2】:

    下面是调试问题的关键部分

    target=https://127.0.0.1:8443/actuator/prometheus/
    msg="Scrape failed"
    err="Get \"https://127.0.0.1:8443/actuator/prometheus/\": dial tcp 127.0.0.1:8443: connect: connection refused"
    

    它指出scrape 失败,因为它无法将connect 连接到target 服务器。作为目标服务器127.0.0.1:8443,它应该在运行 Prometheus 的同一主机中。

    Prometheus 检索作业,也称为scraper,从目标服务中提取数据、聚合数据并将其传递到数据库。 Prometheus 从基于 Prometheus YAML 的配置文件中名为 static_configs 的静态列表(或文件)中获取 targets(IP 地址和端口)的列表。更复杂的dynamic 环境(可能随时启动新实例)使用service discovery mechanisms,它提供要监控的机器列表并显示这些机器的组织方式信息。

    Prometheus 抓取一个目标时,它会自动将一些标签附加到抓取的时间序列上,用于识别抓取的目标。

    up{job="<job-name>", instance="<instance-id>"}: 1 if the instance is healthy, i.e. reachable, or 0 if the scrape failed.
    

    job:目标所属的已配置作业名称
    实例:被抓取的目标 URL 的 : 部分。

    配置 Prometheus 实例

    1. 当 Prometheus 在 localhost 或同一主机上运行时。
    scrape_configs:
    - job_name: node
      static_configs:
      - targets: ['localhost:9100']
    
    1. 当 Prometheus 在不同的环境或主机上运行时
    scrape_configs: 
      - job_name: prometheus 
        static_configs: 
          - targets: ["localhost:9090"] 
      - job_name: eventservice 
        static_configs: 
          - targets: ["events:9090"] 
      - job_name: bookingservice 
        static_configs: 
          - targets: ["bookings:9090"] 
    
    1. 使用 IP 在多个端口中运行时
    static_configs:
      - targets: ['192.168.1.117:':8080', '192.168.1.117:8081']
    

    TLS 配置

    TLS 用于在 Prometheus 实例和抓取目标之间建立安全的私有传输。默认情况下,Prometheus 实例会尝试根据其信任库验证scrape targets 暴露的certificate,以确定抓取目标的真实性。

    scrape_configs:
      - job_name: 'node'
        scheme: https
        tls_config:
            # Prometheus will check that the node_exporter presents a certificate
            # signed by this ca.
            ca_file: 'ca.crt'
            # The cert and key are presented to node_exporter to authenticate
            # Prometheus as a client.
            cert_file: 'client.crt'
            key_file: 'client.key'
    
        static_configs:
        - targets: ['myserver.net:443']
    

    【讨论】:

      猜你喜欢
      • 2021-06-23
      • 2019-02-21
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 2021-06-03
      • 2021-06-12
      • 1970-01-01
      • 2018-02-19
      相关资源
      最近更新 更多