使用 Prometheus 监控 Kubernetes 集群中的 Spring 启动应用程序

Using Prometheus to monitor Spring Boot Applications in Kubernetes Cluster

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

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

我用我的应用程序创建了一个 POD,并在 kubernetes 中成功 运行。它有效,我可以访问它(对于 8080,我创建了一个映射端口的服务),我可以从同一台 PC 执行 "business" 级别的 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 启动 pod(以及我拥有的其他微服务,所有 spring 启动使用相同的执行器设置驱动)。

我从 (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(.*)

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

作为旁注。在这一点上,我不关心可伸缩性问题,我相信一个普罗米修斯服务器就能完成这项工作,但我必须将 Grafana 添加到图片中。

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

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

Spring 引导千分尺 example 在 kubernetes 上使用 Prometheus。 普罗米修斯部署 guide.

为了让 Prometheus 从您的 Spring 启动应用程序收集指标,您需要向其添加特定的依赖项。您可以在此处找到说明如何完成的指南: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:

In order to compare the performance of different JDKs for reactive Spring Boot services, I made a setup in which a Spring Boot application is wrapped in a Docker container. This makes it easy to create different containers for different JDKs with the same Spring Boot application running in it. The Spring Boot application exposes metrics to Prometheus. Grafana can read these metrics and allows to make nice visualizations from it. This blog post describes a setup to get you up and running in minutes.

如果有帮助,请告诉我。