[10.14 workshop] 自定义 Prometheus 监控指标并通过 Grafana 展示

本文涉及的产品
可观测可视化 Grafana 版,10个用户账号 1个月
简介: 阿里云Prometheus监控全面对接开源Prometheus生态,支持类型丰富的组件监控,提供多种开箱即用的预置监控大盘,且提供全面托管的Prometheus服务。借助阿里云Prometheus监控,您无需自行搭建Prometheus监控系统,因而无需关心底层数据存储、数据展示、系统运维等问题。

阿里云Prometheus监控全面对接开源Prometheus生态,支持类型丰富的组件监控,提供多种开箱即用的预置监控大盘,且提供全面托管的Prometheus服务。借助阿里云Prometheus监控,您无需自行搭建Prometheus监控系统,因而无需关心底层数据存储、数据展示、系统运维等问题。

您可以通过阿里云Prometheus监控查看容器服务Kubernetes版ACK(Container Service for Kubernetes)预先配置的监控大盘和监控性能指标。

本文介绍如何通过Annotations和ServiceMonitor来自定义监控指标并通过Grafana展示。

前提条件


背景信息


  • 阿里云 Prometheus监控兼容并提供三种主流采集规则的实现,包括标准开源prometheus.yaml采集规则配置文件、适合自定义K8s内监控的采集规则ServiceMonitor、以及默认采集规则Annotation。
  • 与开源Prometheus监控相比,阿里云 Prometheus监控无需重启,即可使用prometheus.yaml配置文件动态更新采集规则。
  • 在Deployment文件里也无需编写多行代码,仅需增加以下3个Annotation注解即可。具体参考托管 ARMS Prometheus 监控

操作步骤


方式一:通过 Annotations 自定义指标监控

通过给 Deployment Pod Template 加入 Annotations 的方式自定义监控,阿里云 ARMS pilot 将会自动配置 ARMS Prometheus 获取 Pod 自定义指标。

1、登录容器服务管理控制台,在左侧导航栏单击集群,在集群列表中点击目标集群。

2、在左侧边导航栏,单击工作负载,在右侧单击使用镜像创建

3、输入应用的基本信息,单击下一步

4、使用镜像registry.cn-hangzhou.aliyuncs.com/containerdemo/pindex创建一个Web应用,暴露 5000 端口。

mypindex-1

或者,可以直接采用如下 YAML 直接部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: pindex
  name: pindex
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: pindex
  template:
    metadata:
      annotations:
        prometheus.io/path: /access
        prometheus.io/port: '5000'
        prometheus.io/scrape: 'true'
      labels:
        app: pindex
    spec:
      containers:
        - image: 'registry.cn-hangzhou.aliyuncs.com/containerdemo/pindex:latest'
          imagePullPolicy: Always
          name: pindex
          ports:
            - containerPort: 5000
              name: web
              protocol: TCP
          resources:
            requests:
              cpu: 250m
              memory: 512Mi
---
apiVersion: v1
kind: Service
metadata:
  name: pindex-svc
  namespace: default
spec:
  externalTrafficPolicy: Local
  ports:
    - name: web
      port: 5000
      protocol: TCP
      targetPort: 5000
  selector:
    app: pindex
  sessionAffinity: None
  type: LoadBalancer

5、给 Pod 添加上 ARMS 相关的 annotations。prometheus.io/port 表示 Prometheus 要 scrape 的 endpoint 端口,prometheus.io/path 表示 Prometheus 要 scrape 的 endpoint 路径。

mypindex-2

6、单击下一步,创建一个Loadbalancer型Service,暴露服务到公网,单击创建完成服务部署。

7、登录 Prometheus 控制台,单击对应的 K8S 集群。

8、在左侧导航栏单击设置, 然后在右侧区域单击Targets页签,可见自定义的指标已经配置成功。

mypindex-3

9、访问该服务暴露出来的公网IP,增加一些指标值。

mypindex-web

10、单击 Prometheus 任意一个预置的图表进入 Grafana,然后单击添加 Panal ,选择 Graph 类型,将Metrics 填写为current_person_counts

11、保存配置即可观察到自定义指标的 Grafana 图形。

mypindex-4

方式二:通过 ServiceMonitor 自定义指标监控

通过 ServiceMonitor 方式自定义指标监控在部署的时候不需要配置 annotations,但是要给 service 对象添加标签。

1、部署 Demo 示例服务。

与方式一的步骤类似,但是部署服务的时候不需要配置 Annotations。

custom-1

2、添加服务时,需要添加标签(app=custom-metrics-pindex),该标签将被用于 serviceMonitor 的 selector:

custom-2

或者通过如下的 YAML 直接部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: pindex
  name: pindex
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: pindex
  template:
    metadata:
      annotations:
        prometheus.io/path: /access
        prometheus.io/port: '5000'
        prometheus.io/scrape: 'true'
      labels:
        app: pindex
    spec:
      containers:
        - image: 'registry.cn-hangzhou.aliyuncs.com/containerdemo/pindex:latest'
          imagePullPolicy: Always
          name: pindex
          ports:
            - containerPort: 5000
              name: web
              protocol: TCP
          resources:
            requests:
              cpu: 250m
              memory: 512Mi
---
apiVersion: v1
kind: Service
metadata:
  name: pindex-svc
  namespace: default
  labels:
    app: custom-metrics-pindex
spec:
  externalTrafficPolicy: Local
  ports:
    - name: web
      port: 5000
      protocol: TCP
      targetPort: 5000
  selector:
    app: pindex
  sessionAffinity: None
  type: LoadBalancer

3、登录 Prometheus 控制台,单击对应的 K8S 集群。

4、在左侧导航栏单击设置, 然后在右侧区域单击服务发现页签。

5、单击页面右上角添加 ServiceMonitor,模板文件如下,单击确定完成ServiceMonitor创建。

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  #  填写一个唯一名称
  name: custom-metrics-pindex
  #  填写目标命名空间
  namespace: default
spec:
  endpoints:
  - interval: 30s
    #  填写service.yaml中对应的Port的Name字段的值(参考上图)
    port: web
    #  填写Service对应的Path的值
    path: /access
  namespaceSelector:
    any: true
    #  Nginx Demo的命名空间
  selector:
    matchLabels:
      #  填写service.yaml的Label字段的值以定位目标service.yaml
      app: custom-metrics-pindex

6、在Targets页签,可见 Prometheus 已经获得服务 Scape Endpioint。

custom-3

说明 这里 ServiceMonitor 定义比 Annotations 方式更美观,指示出了 Namespace 和 Service 名称。

7、再次进入之前的 Grafana 页面,因为是相同的指标,与上述相同的 Metrics 配置,可见 4 个实例的指标监控信息。

custom-4

相关文章
|
18天前
|
存储 JSON Prometheus
如何精简 Prometheus 的指标和存储占用
如何精简 Prometheus 的指标和存储占用
|
10天前
|
Prometheus 监控 Cloud Native
Ubantu docker学习笔记(九)容器监控 自带的监控+sysdig+scope+cAdvisor+prometheus
Ubantu docker学习笔记(九)容器监控 自带的监控+sysdig+scope+cAdvisor+prometheus
|
11天前
|
弹性计算 Prometheus 监控
基于 Prometheus 的超算弹性计算场景下主机监控最佳实践
超算快速弹性伸缩场景下,如何构建一套准确、快速、可靠的监控体系成为关键点。阿里云在超算场景的主机监控落地实践,解决超算场景面临的挑战,交付一套可靠和全面的主机监控体系。
59687 13
|
11天前
|
Prometheus 监控 Cloud Native
Prometheus监控平台配置--监控集群资源信息
在scrape_configs 配置项下添加Linux 监控的job,其中 IP 修改为上面部署node_exporter机器的ip,端口号为9100,需要注意缩进。
174 6
|
18天前
|
Prometheus 监控 Cloud Native
应用监控(Prometheus + Grafana)
应用监控(Prometheus + Grafana)
186 2
|
18天前
|
Prometheus 监控 Cloud Native
使用Prometheus配置监控与报警
通过以上步骤,你可以使用Prometheus和Alertmanager实现监控和报警配置,以确保系统在出现性能问题或故障时能够及时通知相关人员。欢迎关注威哥爱编程,一起学习成长。
141 0
|
18天前
|
Prometheus 监控 Cloud Native
Prometheus+Grafana+NodeExporter 打造一款出色的监控系统,帅呆了!
Prometheus+Grafana+NodeExporter 打造一款出色的监控系统,帅呆了!
88 2
|
18天前
|
数据采集 监控 数据库
请问OceanBase社区版能否通过zabbix监控,然后将报错信息展现到grafana?
【2月更文挑战第25天】请问OceanBase社区版能否通过zabbix监控,然后将报错信息展现到grafana?
27 2
|
18天前
|
存储 Prometheus Cloud Native
Grafana 系列 - 统一展示 -2-Prometheus 数据源
Grafana 系列 - 统一展示 -2-Prometheus 数据源
|
18天前
|
JSON Prometheus Cloud Native
Grafana 系列 - 统一展示 -3-Prometheus 仪表板
Grafana 系列 - 统一展示 -3-Prometheus 仪表板