简体   繁体   English

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

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

I have spring boot powered microservices deployed in my local kubernetes cluster.我在本地 kubernetes 集群中部署了 Spring Boot 驱动的微服务。 The microservices are using micrometer and prometheus registry but due to our company policy the actuator is available on another port:微服务使用 micrometer 和 prometheus 注册表,但由于我们公司的政策,执行器可在另一个端口上使用:

  • 8080 for "business" http requests 8080 用于“业务”http 请求
  • 8081/manage for actuator. 8081/manage 用于执行器。 So, I can access http://host:8081/manage/prometheus and see the metrics when running the process locally (without kubernetes).因此,我可以访问http://host:8081/manage/prometheus并在本地运行进程时查看指标(没有 kubernetes)。

Now, I'm a beginner in Prometheus and have a rather limited knowledge in kubernetes (I'm coming with a Java developer background).现在,我是 Prometheus 的初学者,对 kubernetes 的知识相当有限(我有 Java 开发人员背景)。

I've created a POD with my application and succesfully run it in kubernetes.我已经用我的应用程序创建了一个 POD 并成功地在 kubernetes 中运行它。 It works and I can access it (for 8080 I've created a service to map the ports) and I can execute "business" level http requests it from the same PC.它可以工作并且我可以访问它(对于 8080,我已经创建了一个服务来映射端口)并且我可以从同一台 PC 执行“业务”级别的 http 请求。

But I haven't find any examples of adding a prometheus into the picture.但是我还没有找到任何在图片中添加普罗米修斯的例子。 Prometheus is supposed to be deployed in the same kubernetes cluster just as another pod. Prometheus 应该像另一个 pod 一样部署在同一个 kubernetes 集群中。 So I've started with:所以我开始了:


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 looks like: 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

My question is about how exactly I should define prometheus.yml so that it will get the metrics from my spring boot pod (and other microservices that I have, all spring boot driven with the same actuator setup).我的问题是我应该如何定义prometheus.yml以便它从我的 spring boot pod(以及我拥有的其他微服务,所有 spring boot 都使用相同的执行器设置驱动)获取指标。

I've started with ( prometheus.yml ):我已经开始( 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(.*)

But apparently it doesn't work, so I've asking for the advices:但显然它不起作用,所以我寻求建议:

  • If someone has a working example it would be the best :)如果有人有一个工作示例,那将是最好的:)
  • Intuitively I understand I need to specify the port mapping for my 8081 port but I'm not exactly know how直觉上我明白我需要为我的8081端口指定端口映射,但我不完全知道如何
  • Since prometheus is supposed to run on another port am I supposed to expose a kubernetes service for port 8081 at the kubernetes level?由于 prometheus 应该在另一个端口上运行,我是否应该在 kubernetes 级别为端口 8081 公开 kubernetes 服务?
  • Do I need any security related resources in kubernetes to be defined?我需要在 kubernetes 中定义任何与安全相关的资源吗?

As a side note.作为旁注。 At this point I don't care about scalability issues, I believe one prometheus server will do the job, but I'll have to add Grafana into the picture.在这一点上,我不关心可扩展性问题,我相信一台 prometheus 服务器可以完成这项工作,但我必须将 Grafana 添加到图片中。

Rather than hardcoding it in prometheus config you need to make use of annotations on your pods to to tell prometheus which pods, what path and which port Prometheus should scrape.与其在 prometheus 配置中对其进行硬编码,您需要使用 pod 上的注释来告诉 prometheus Prometheus 应该抓取哪些 pod、什么路径和哪个端口。

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

Spring boot micrometer example with Prometheus on kubernetes.在 kubernetes 上使用 Prometheus 的 Spring Boot 千分尺示例 Prometheus deployment guide .普罗米修斯部署指南

In order to let Prometheus collect metrics from your Spring Boot Application you need to add a specific dependencies to it.为了让 Prometheus 从您的 Spring Boot 应用程序收集指标,您需要向其添加特定的依赖项。 Here you can find a guide showing how to make it done: Spring Boot metrics monitoring using Prometheus & Grafana .在这里,您可以找到展示如何完成的指南: 使用 Prometheus 和 Grafana 的 Spring Boot 指标监控 Here is an example:下面是一个例子:

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

If you would like to use a bit different strategy you can also check out this one: Monitoring Spring Boot applications with Prometheus and Grafana :如果你想使用一些不同的策略,你也可以查看这个: 使用 Prometheus 和 Grafana 监控 Spring Boot 应用程序

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.为了比较不同 JDK 对响应式 Spring Boot 服务的性能,我进行了一个设置,其中将 Spring Boot 应用程序包装在 Docker 容器中。 This makes it easy to create different containers for different JDKs with the same Spring Boot application running in it.这使得为​​不同的 JDK 创建不同的容器变得容易,其中运行相同的 Spring Boot 应用程序。 The Spring Boot application exposes metrics to Prometheus. Spring Boot 应用程序向 Prometheus 公开指标。 Grafana can read these metrics and allows to make nice visualizations from it. Grafana 可以读取这些指标并允许从中进行很好的可视化。 This blog post describes a setup to get you up and running in minutes.这篇博文介绍了一种设置,可让您在几分钟内启动并运行。

Please let me know if that helped.如果这有帮助,请告诉我。

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

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