一、概述
Prometheus是一个开源的系统监控和 警系统,现在已经加入到CNCF基金会,成为继k8s之后第二个在CNCF托管的项目,在kubernetes容器管理系统中,通常会搭配prometheus进行监控,同时也支持多种exporter采集数据,还支持pushgateway进行数据上 ,Prometheus性能足够支撑上万台规模的集群。
Prometheus+Grafana环境部署可以参考我这篇文章:【云原生】Prometheus+Grafana on K8s 环境部署
二、监控架构
三、给Grafana配置Prometheus数据源
有许多与 Grafana Alerting 兼容的数据源。每个数据源都由一个插件支持。您可以使用下面列出的内置数据源之一,使用外部数据源插件,或创建自己的数据源插件。以下就是与 Grafana Alerting 兼容并支持的数据源:
Grafana web地址:https://alertmanager.k8s.local/
账 :admin,密码通过下面命令获取0D0NfEWWFx9qsBiKR8PuFVxf6PPa9o8YGhZZaNXY
kubectl get secret --namespace grafana grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo
# 配置这个内 地址(推荐)http://prometheus-server.prometheus:80# 或者配置对外地址https://prometheus.k8s.local
配置对外地址时,如果域名是通过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。
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 常用容器监控指标:
container_memory_usage_bytes{image!=""}
sum(rate(container_network_receive_bytes_total{image!=""}[1m])) without (interface)
sum(rate(container_network_transmit_bytes_total{image!=""}[1m])) without (interface)
sum(rate(container_fs_reads_bytes_total{image!=""}[1m])) without (device)
sum(rate(container_fs_writes_bytes_total{image!=""}[1m])) without (device)
##容器 络接收的字节数(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使用率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等组成。
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!