spring 使用Prometheus监控Kubernetes集群中的Sping Boot 应用程序

pdkcd3nj  于 5个月前  发布在  Spring
关注(0)|答案(2)|浏览(60)

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

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

现在,我是Prometheus的初学者,对kubernetes的知识相当有限(我有Java开发人员背景)。
我已经用我的应用程序创建了一个POD,并成功地在kubernetes中运行了它。它可以工作,我可以访问它(对于8080,我已经创建了一个服务来Map端口),我可以从同一台PC执行“业务”级别的http请求。
但是我还没有找到任何添加prometheus的例子。Prometheus应该部署在同一个kubernetes集群中,就像另一个pod一样。所以我开始了:

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端口指定端口Map,但我不知 prop 体如何操作
  • 既然prometheus应该在另一个端口上运行,我应该在kubernetes级别为端口8081公开一个kubernetes服务吗?
  • 我需要在Kubernetes中定义任何与安全相关的资源吗?

顺便说一句,在这一点上,我不关心可伸缩性问题,我相信一个prometheus服务器就可以完成这项工作,但我必须把Grafana添加到图片中。

3yhwsihp

3yhwsihp1#

而不是硬编码在prometheus配置中,你需要使用pod上的注解来告诉prometheus哪些pod,什么路径和哪个端口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

u5rb5r59

u5rb5r592#

为了让Prometheus从您的Sping 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
为了比较响应式Sping Boot 服务的不同JDK的性能,我做了一个设置,其中Sping Boot 应用程序被 Package 在Docker容器中。这使得为不同的JDK创建不同的容器变得很容易,其中运行相同的Sping Boot 应用程序。Sping Boot 应用程序将指标公开给Prometheus。Grafana可以读取这些指标并允许从中制作漂亮的可视化。此博客这篇文章描述了一个可以让你在几分钟内启动并运行的设置。
请让我知道这是否有帮助。

相关问题