700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 使用Prometheus发现在Kubernetes上运行的应用程序

使用Prometheus发现在Kubernetes上运行的应用程序

时间:2024-06-08 07:01:30

相关推荐

使用Prometheus发现在Kubernetes上运行的应用程序

Prometheus支持抓取应用程序的多个实例。 由于其IP地址会发生变化,因此需要动态发现在协调环境中运行的应用程序。 可以将Prometheus配置为使用Kubernetes API动态地发现正在运行的实例列表中的更改。

下面显示了一个简单的Prometheus示例,该示例对具有多个Pod实例的应用程序进行检测。

普罗米修斯配置

我们配置Prometheus来发现我们的config-example应用程序的pod。

global: scrape_interval: 15s external_labels: monitor: 'example-monitor' scrape_configs: - job_name: 'example-metric' scrape_interval: 5s metrics_path: /metrics/ scheme: https basic_auth: username: admin password: adminadmin tls_config: insecure_skip_verify: true kubernetes_sd_configs: - role: endpoints namespaces: names: - default relabel_configs: - source_labels: [__meta_kubernetes_service_label_app] separator: ; regex: config-example replacement: $ 1 action: keep - source_labels: [__meta_kubernetes_endpoint_port_name] separator: ; regex: https replacement: $ 1 action: keep - source_labels: [__meta_kubernetes_namespace] separator: ; regex: (.*) target_label: namespace replacement: $ 1 action: replace - source_labels: [__meta_kubernetes_pod_name] separator: ; regex: (.*) target_label: pod replacement: $ 1 action: replace - source_labels: [__meta_kubernetes_service_name] separator: ; regex: (.*) target_label: service replacement: $ 1 action: replace - source_labels: [__meta_kubernetes_service_name] separator: ; regex: (.*) target_label: job replacement: ${ 1 } action: replace - separator: ; regex: (.*) target_label: endpoint replacement: https action: replace

我们需要调整app标签(在此处为config-example)和端口名称(https),在该端口下可以使用监视端点。

仪表应用

本示例使用在Open Liberty之上运行的MicroProfile Metrics端点。 该应用程序在端口9443和路径/metrics/下以Prometheus格式公开/metrics/

在这里,您可以看到示例服务和部署:

kind: Service apiVersion: v1 metadata: name: config-example labels: app: config-example spec: selector: app: config-example ports: - port: 9443 name: https - port: 9080 name: http

kind: Deployment apiVersion: apps/v1beta1 metadata: name: config-example spec: replicas: 2 template: metadata: labels: app: config-example spec: containers: - name: config-example image: sdaschner/config-example: 1 # ...

您可以在GitHub上找到完整的示例,包括正在运行的应用程序。

Prometheus设置和RBAC

为了使该示例适用于RBAC Kubernetes设置,Prometheus服务帐户需要具有正确的权限。 因此,我们将在此处创建服务帐户和相应的群集角色,如YAML定义中所述。 同样,我们在创建Prometheus部署时指定服务帐户。

我们为Prometheus实例创建部署和服务。

kind: Service apiVersion: v1 metadata: name: prometheus labels: app: prometheus spec: selector: app: prometheus ports: - port: 9090 name: http

kind: Deployment apiVersion: apps/v1beta1 metadata: name: prometheus spec: replicas: 1 template: metadata: labels: app: prometheus version: v1 spec: serviceAccountName: prometheus containers: - name: prometheus image: prom/prometheus:v2. 7.1 ports: 9090 - containerPort: 9090 volumeMounts: - name: prometheus-config-volume mountPath: /etc/prometheus/prometheus.yml subPath: prometheus.yml volumes: - name: prometheus-config-volume configMap: name: prometheus-config restartPolicy: Always

前面显示的配置可以从配置映射中注入实例。

kind: ConfigMap apiVersion: v1 metadata: name: prometheus-config data: prometheus.yml: | global: scrape_interval: 15s # content as shown earlier ...

有关Prometheus配置的完整说明,请参阅文档 。

访问目标

现在,正在运行的Prometheus实例可以发现两个正在运行的配置示例应用程序,它们都作为Kubernetes容器运行:

gt; kubectl get pods NAME READY STATUS RESTARTS AGE config-example-69974cbc96-dqd96 gt; kubectl get pods NAME READY STATUS RESTARTS AGE config-example-69974cbc96-dqd96 1 / 1 Running 0 4m config-example-69974cbc96-zstg7 1 / 1 Running 0 4m grafana-8694db9d4f-nvn5s 1 / 1 Running 0 3m prometheus-594dd9cdb8-95ftz 1 / 1 Running 0 3m

我们可以在Prometheus配置下看到实际目标,包括其IP地址。

看一下GitHub上的完整示例。 您可能还会看到以下视频,了解如何使用MicroProfile在Java EE应用程序中实现业务指标。

对于定义了更多应用程序的更复杂的微服务示例,使用纯Prometheus配置的这种方法可能有点麻烦。 为了减少样板代码,开发人员可以使用抽象化较低层配置的解决方案,例如Prometheus Operator 。 在下一篇文章中,我们将看到Prometheus Operator如何促进应用程序的配置。

监控愉快!

翻译自: //02/applications-running-kubernetes-prometheus.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。