【问题标题】:Using Prometheus to monitor Spring Boot Applications in Kubernetes Cluster使用 Prometheus 监控 Kubernetes 集群中的 Spring Boot 应用程序
【发布时间】:2020-05-06 05:28:39
【问题描述】:

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

  • 8080 用于“业务”http 请求
  • 8081/管理执行器。因此,我可以访问 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"]

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

我的问题是我应该如何准确定义 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(.*)

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

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

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

【问题讨论】:

  • 也许 devops SE 是问这个问题的好地方
  • 是的,可能我没有在那里注册,也许是时候加入我们的战友社区了 :)

标签: java spring spring-boot kubernetes prometheus


【解决方案1】:

为了让 Prometheus 从您的 Spring Boot 应用程序中收集指标,您需要为其添加特定的依赖项。在这里您可以找到说明如何完成此操作的指南:Spring Boot metrics monitoring using Prometheus & Grafana。这是一个例子:

<dependency>  
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_spring_boot</artifactId>
            <version>0.1.0</version>
        </dependency>

        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_hotspot</artifactId>
            <version>0.1.0</version>
        </dependency>

如果你想使用一些不同的策略,你也可以看看这个:Monitoring Spring Boot applications with Prometheus and Grafana:

为了比较不同 JDK 的响应式性能 Spring Boot 服务,我做了一个设置,其中一个 Spring Boot 应用程序包装在 Docker 容器中。这使得很容易 使用相同的 Spring 为不同的 JDK 创建不同的容器 启动在其中运行的应用程序。 Spring Boot 应用程序公开 Prometheus 的指标。 Grafana 可以读取这些指标并允许 从中做出漂亮的可视化。这篇博文描述了一个设置 让您在几分钟内启动并运行。

如果有帮助,请告诉我。

【讨论】:

  • 非常感谢您的回答,但问题是关于 kubernetes 配置的。 Java 相关部分使用千分尺为我工作。话虽如此,我没有使用 prometheus 客户端库的经验,并选择了 micrometer,因为它是与 spring boot 2 集成的计量框架,并在 spring boot 文档中描述为解决方案。如果您对这个库有真正的经验,并能说明为什么这个解决方案比千分尺更可取,那可能会很棒……如果我无法设置基于千分尺的解决方案,我会尝试这个。到那时我还差 10 分 :)
【解决方案2】:

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

prometheus.io/scrape: "true"
prometheus.io/path=/manage/prometheus
prometheus.io/port=8081
prometheus.io/scheme=http

Spring boot 千分尺example 在 kubernetes 上使用 Prometheus。 Prometheus 部署guide.

【讨论】:

  • 非常感谢您的回答,我明天试试并更新
  • 这种方法对我有用。赞成并接受答案。非常感谢
  • 您是否在其他地方定义了prometheus.io/scheme=http?我在您提供的 deployment.yaml 示例中没有看到它
猜你喜欢
  • 2020-05-21
  • 2020-03-26
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
  • 2019-10-13
  • 2019-07-31
  • 2020-04-17
  • 2016-02-29
相关资源
最近更新 更多