Prometheus+Grafana 监控 k8s 资源实战操作
Tags: Grafanak8sPrometheus监控
一、概述
Prometheus是一个开源的系统监控和报警系统,现在已经加入到CNCF基金会,成为继k8s之后第二个在CNCF托管的项目,在kubernetes容器管理系统中,通常会搭配prometheus进行监控,同时也支持多种exporter采集数据,还支持pushgateway进行数据上报,Prometheus性能足够支撑上万台规模的集群。
二、监控架构
- 通过cadvisor采集容器、Pod相关的性能指标数据,并通过暴露的/metrics接口用prometheus抓取。
- 通过node-exporter采集主机的性能指标数据,并通过暴露的/metrics接口用prometheus抓取。
- 通过kube-state-metrics采集k8s资源对象的状态指标数据,并通过暴露的/metrics接口用prometheus抓取。
- 应用侧自己采集容器中进程主动暴露的指标数据(暴露指标的功能由应用自己实现,并添加平台侧约定的annotation,平台侧负责根据annotation实现通过Prometheus的抓取)。
- 通过etcd、kubelet、kube-apiserver、kube-controller-manager、kube-scheduler自身暴露的/metrics获取节点上与k8s集群相关的一些特征指标数据。
三、给Grafana配置Prometheus数据源
►有许多与 Grafana Alerting 兼容的数据源。每个数据源都由一个插件支持。您可以使用下面列出的内置数据源之一,使用外部数据源插件,或创建自己的数据源插件。
►以下就是与 Grafana Alerting 兼容并支持的数据源:
·AWS CloudWatch
· Azure Monitor
· Elasticsearch
·Google Cloud Monitoring
· Graphite
· InfluxDB
· Loki
·Microsoft SQL Server MSSQL
·MySQL
·Open TSDB
·PostgreSQL
· Prometheus
· Jaeger
·Zipkin
·Tempo
·Testdata
►Grafana web地址:
https://alertmanager.k8s.local/
►账号:admin,密码通过下面命令获取
0D0NfEWWFx9qsBiKR8PuFVxf6PPa9o8YGhZZaNXY
1 | kubectl get secret --namespace grafana grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo |
►# 配置这个内网地址(推荐)
http://prometheus-server.prometheus:80
►# 或者配置对外地址
►配置对外地址时,如果域名是通过hosts配置的,得需要在values.yaml文件配置这个hostAliases,IP对应宿主机的IP。就会将这个配置加载到pod里的/etc/hosts文件中。
...
hostAliases:
- ip: "192.168.182.110"
hostnames:
- "prometheus.k8s.local"
...
►滚动更新
helm upgrade grafana -n grafana ./grafana
四、cAdvisor 介绍
►cAdvisor是Google开源的容器资源监控和性能分析工具,它是专门为容器而生,在Kubernetes中,我们不需要单独去安装,cAdvisor作为kubelet内置的一部分程序可以直接使用,也就是我们可以直接使用cadvisor采集数据,可以采集到和容器运行相关的所有指标,数据路径为/api/v1/nodes/[节点名称]/proxy/metrics/cadvisor。
►kubelet的节点使用cAdvisor提供的metrics接口获取该节点所有容器相关的性能指标数据。
►cAdvisor 是一个开源容器资源使用收集器。它专为容器而构建,支持本地 Docker 容器。cAdisor 会自动发现给定节点中的所有容器,并收集CPU、内存、文件系统和网络使用情况统计信息,不过它仅会收集基本资源利用率。
►仅当容器具有 X%CPU 利用率时,cAdvisor 才能告诉我们应用程序的实际性能。cAdvisor 本身并不提供任何长期存储或分析功能。
►cadvisor中获取到的典型监控指标如下:
指标名称 | 类型 | 含义 |
---|---|---|
container_cpu_load_average_10s | gauge | 过去10秒容器CPU的平均负载 |
container_cpu_usage_seconds_total | counter | 容器在每个CPU内核上的累积占用时间 (单位:秒) |
container_cpu_system_seconds_total | counter | System CPU累积占用时间(单位:秒) |
container_cpu_user_seconds_total | counter | User CPU累积占用时间(单位:秒) |
container_fs_usage_bytes | gauge | 容器中文件系统的使用量(单位:字节) |
container_fs_limit_bytes | gauge | 容器可以使用的文件系统总量(单位:字节) |
container_fs_reads_bytes_total | counter | 容器累积读取数据的总量(单位:字节) |
container_fs_writes_bytes_total | counter | 容器累积写入数据的总量(单位:字节) |
container_memory_max_usage_bytes | gauge | 容器的最大内存使用量(单位:字节) |
container_memory_usage_bytes | gauge | 容器当前的内存使用量(单位:字节) |
container_spec_memory_limit_bytes | gauge | 容器的内存使用量限制 |
machine_memory_bytes | gauge | 当前主机的内存总量 |
container_network_receive_bytes_total | counter | 容器网络累积接收数据总量(单位:字节) |
container_network_transmit_bytes_total | counter | 容器网络累积传输数据总量(单位:字节) |
►cadvisor 常用容器监控指标:
- · 查询容器内存使用量(单位:字节)
1 | container_memory_usage_bytes{image!=""} |
- · 查询容器网络接收量(速率)(单位:字节/秒)
1 | sum(rate(container_network_receive_bytes_total{image!=""}[1m])) without (interface) |
- · 容器网络传输量 字节/秒
1 | sum(rate(container_network_transmit_bytes_total{image!=""}[1m])) without (interface) |
- · 容器文件系统读取速率 字节/秒
1 | sum(rate(container_fs_reads_bytes_total{image!=""}[1m])) without (device) |
- · 容器文件系统写入速率 字节/秒
1 | sum(rate(container_fs_writes_bytes_total{image!=""}[1m])) without (device) |
- · 网络流量监控
1 2 3 4 5 | ##容器网络接收的字节数(1分钟内),根据名称查询 name=~".+" sum(rate(container_network_receive_bytes_total{name=~".+"}[1m])) by (name) ##容器网络传输的字节数(1分钟内),根据名称查询 name=~".+" sum(rate(container_network_transmit_bytes_total{name=~".+"}[1m])) by (name) |
- · 容器 CPU相关监控
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ### 容器CPU使用率 sum(irate(container_cpu_usage_seconds_total{image!=""}[1m])) without (cpu) ###所用容器system cpu的累计使用时间(1min钟内) sum(rate(container_cpu_system_seconds_total[1m])) ###每个容器system cpu的使用时间(1min钟内) sum(irate(container_cpu_system_seconds_total{image!=""}[1m])) without (cpu) #每个容器的cpu使用率 sum(rate(container_cpu_usage_seconds_total{name=~".+"}[1m])) by (name) * 100 #总容器的cpu使用率 sum(sum(rate(container_cpu_usage_seconds_total{name=~".+"}[1m])) by (name) * 100) |
五、监控配置
►1)监控k8s集群中的pod
kubelet的节点使用cAdvisor提供的metrics接口获取该节点所有容器相关的性能指标数据。cAdvisor默认集成在k8s中,无需再安装了。
导入grafana模板,集群资源监控:3119
►官方模块下载地址:
https://grafana.com/grafana/dashboards/
►上面截图第一个就是3119模板,当然也可以使用上面截图的其它模板,可以根据需要自己选择。将json下来导入到grafana中。
►导入模板
►2) 监控k8s集群中的node
使用文档:https://prometheus.io/docs/guides/node-exporter/
GitHub:https://github.com/prometheus/node_exporter
exporter列表:https://prometheus.io/docs/instrumenting/exporters/
所有node节点部署node_exporter
kubectl get pods -n prometheus -owide|grep node-exporter
►查看prometheus是否收集到kubernetes-nodes
►导入grafana模板,集群资源监控:13105,13824,8919
官方模块下载地址:https://grafana.com/grafana/dashboards/
►3)监控k8s资源对象
kube-state-metrics是一个简单的服务,它监听Kubernetes API服务器并生成有关对象状态的指标。它不关注单个Kubernetes组件的运行状况,而是关注内部各种对象的运行状况,例如部署,节点和容器。
GitHub地址:https://github.com/kubernetes/kube-state-metrics
导入grafana模板,集群资源监控:16520
如果上面的模板不满足需求,也可以自定义。
六、自定义Dashboard
►当然,在有些时候我们需要对我们的应用进行指标监控,这个时候需要我们自定义dashboard。这就需要我们对Grafana有更深入的了解,比如什么时候metrics,Grafana的查询语法等等。笔者尝试用最简单的方式和大家进行介绍。
►1)Folder文件夹
dashboard必须属于某个文件夹,可以理解成分类,当dashboard多了之后方便管理,默认的folder是 general,我们可以根据实际情况进行分文件夹,比如 MySQL,K8s等待。
►2)dashboard的组成
一个dashboard由General、Annotations、Variables、Links、Versions、Permissions、JSON Model、Pannels等组成。