繁体   English   中英

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

[英]Using Prometheus to monitor Spring Boot Applications in Kubernetes Cluster

我在本地 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"]

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 添加到图片中。

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

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

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

为了让 Prometheus 从您的 Spring Boot 应用程序收集指标,您需要向其添加特定的依赖项。 在这里,您可以找到展示如何完成的指南: 使用 Prometheus 和 Grafana 的 Spring Boot 指标监控 下面是一个例子:

<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>

如果你想使用一些不同的策略,你也可以查看这个: 使用 Prometheus 和 Grafana 监控 Spring Boot 应用程序

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

如果这有帮助,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM