使用 Prometheus 监控 Kubernetes 集群中的 Spring Boot 应用

Mar*_*nik 2 java spring spring-boot kubernetes prometheus

我在本地 kubernetes 集群中部署了 Spring Boot 驱动的微服务。微服务使用 micrometer 和 prometheus 注册表,但由于我们公司的政策,执行器可在另一个端口上使用:

  • 8080 用于“业务”http 请求
  • 8081/manage 用于执行器。因此,我可以http://host:8081/manage/prometheus在本地运行进程时访问和查看指标(没有 kubernetes)。

现在,我是 Prometheus 的初学者,对 kubernetes 的知识相当有限(我有 Java 开发人员背景)。

我已经用我的应用程序创建了一个 POD 并成功地在 kubernetes 中运行它。它可以工作并且我可以访问它(对于 8080,我已经创建了一个服务来映射端口)并且我可以从同一台 PC 执行“业务”级别的 http 请求。

但是我还没有找到任何在图片中添加普罗米修斯的例子。Prometheus 应该像另一个 pod 一样部署在同一个 kubernetes 集群中。所以我开始了:


FROM @docker.registry.address@/prom/prometheus:v2.15.2

COPY entrypoint.sh /
USER root
RUN chmod 755 /entrypoint.sh

ADD ./prometheus.yml  /etc/prometheus/

ENTRYPOINT ["/entrypoint.sh"]

Run Code Online (Sandbox Code Playgroud)

entrypoint.sh看起来像:

#!/bin/sh
echo "About to run prometheus"
/bin/prometheus --config.file=/etc/prometheus/prometheus.yml \
                --storage.tsdb.path=/prometheus \
                --storage.tsdb.retention.time=3d \
                --web.console.libraries=/etc/prometheus/console_libraries \
                --web.console.templates=/etc/prometheus/consoles
Run Code Online (Sandbox Code Playgroud)

我的问题是我应该如何定义,prometheus.yml以便它从我的 spring boot pod(以及我拥有的其他微服务,所有 spring boot 都使用相同的执行器设置驱动)获取指标。

我从 ( prometheus.yml)开始:

global:
  scrape_interval: 10s
  evaluation_interval: 10s

scrape_configs:

  - job_name: 'prometheus'
    metrics_path: /manage/prometheus
    kubernetes_sd_configs:
      - role: pod
    bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token  
    relabel_configs:
      - source_labels: [__meta_kubernetes_pod_label_app]
        action: keep
        regex: sample-pod-app(.*)|another-pod-app(.*)

Run Code Online (Sandbox Code Playgroud)

但显然它不起作用,所以我寻求建议:

  • 如果有人有一个工作示例,那将是最好的:)
  • 直觉上我明白我需要为我的8081端口指定端口映射,但我不完全知道如何
  • 由于 prometheus 应该在另一个端口上运行,我是否应该在 kubernetes 级别为端口 8081 公开 kubernetes 服务?
  • 我需要在 kubernetes 中定义任何与安全相关的资源吗?

作为旁注。在这一点上,我不关心可扩展性问题,我相信一台 prometheus 服务器可以完成这项工作,但我必须将 Grafana 添加到图片中。

Arg*_*dhu 6

与其在 prometheus 配置中对其进行硬编码,您需要使用 pod 上的注释来告诉 prometheus Prometheus 应该抓取哪些 pod、什么路径和哪个端口。

prometheus.io/scrape: "true"
prometheus.io/path=/manage/prometheus
prometheus.io/port=8081
prometheus.io/scheme=http
Run Code Online (Sandbox Code Playgroud)

在 kubernetes 上使用 Prometheus 的Spring Boot 千分尺示例。普罗米修斯部署指南