- 一、kube-state-metrics简介
- 二、kube-state-metrics部署
- 三、prometheus采集数据
- 四、Grafana可视化导入模板
- 五、kube-state-metrics指标详解
- 六、kube-state-metrics告警规则文件
- 总结:整理不易,如果对你有帮助,可否点赞关注一下?
一、kube-state-metrics简介
官方介绍:
kube-state-metrics (KSM) is a simple service that listens to the Kubernetes API server and generates metrics about the state of the objects. (See examples in the Metrics section below.) It is not focused on the health of the individual Kubernetes components, but rather on the health of the various objects inside, such as deployments, nodes and pods。
从官方介绍可以看出,kube-state-metrics可以监听Kubernetes API服务器并生成有关对象状态的指标,它不关注单个Kubernetes组件的运行状况,而是关注内部各种对象的运行状况,例如部署,节点和容器。
监控指标如下:
CertificateSigningRequest Metrics ConfigMap Metrics CronJob Metrics DaemonSet Metrics Deployment Metrics Endpoint Metrics Horizontal Pod Autoscaler Metrics Ingress Metrics Job Metrics Lease Metrics LimitRange Metrics MutatingWebhookConfiguration Metrics Namespace Metrics NetworkPolicy Metrics Node Metrics PersistentVolume Metrics PersistentVolumeClaim Metrics Pod Disruption Budget Metrics Pod Metrics ReplicaSet Metrics ReplicationController Metrics ResourceQuota Metrics Secret Metrics Service Metrics StatefulSet Metrics StorageClass Metrics ValidatingWebhookConfiguration Metrics VerticalPodAutoscaler Metrics VolumeAttachment Metrics
二、kube-state-metrics部署
CSDN:K8S资源对象监控kube-state-metrics-2.0.0镜像及资源清单文件
Github:K8S资源对象监控kube-state-metrics-2.0.0资源清单文件
注意:根据k8s集群版本选择对应的kube-state-metrics版本,默认是kube-state-metrics创建的svc是ClusterIP类型,只能被集群内部访问,如果需要被集群外部访问请修改service.yaml或者通过ingress代理的方式将端口暴露出来。
部署步骤如下:
1、创建容器
[root@k8s-master-13 kube-state-metrics]# ll -rw-r--r-- 1 root root 376 12月 22 15:25 cluster-role-binding.yaml -rw-r--r-- 1 root root 1623 12月 22 15:25 cluster-role.yaml -rw-r--r-- 1 root root 1134 12月 22 15:25 deployment.yaml -rw-r--r-- 1 root root 192 12月 22 15:25 service-account.yaml -rw-r--r-- 1 root root 405 12月 22 15:25 service.yaml [root@k8s-master-13 kube-state-metrics]# kubectl apply -f . clusterrolebinding.rbac.authorization.k8s.io/kube-state-metrics created clusterrole.rbac.authorization.k8s.io/kube-state-metrics created deployment.apps/kube-state-metrics created serviceaccount/kube-state-metrics created service/kube-state-metrics created
如下图所示:
说明:8080端口用于公开kubernetes的指标数据的端口,8081端口用于公开自身kube-state-metrics的指标数据的端口。
三、prometheus采集数据
部署步骤如下:
1、创建monitor名称空间
[root@k8s-master-13 ~]# kubectl create ns monitor
2、创建一个sa账号monitor-sa
[root@k8s-master-13 ~]# kubectl create serviceaccount monitor-sa -n monitor
3、对monitor-sa账号进行rbac授权
[root@k8s-master-13 ~]# kubectl create clusterrolebinding monitor-clusterrolebinding -n monitor --clusterrole=cluster-admin --serviceaccount=monitor:monitor-sa
4、采集数据
Prometheus2.32.1版本镜像及资源清单文件
[root@k8s-master-13 prometheus]# ll -rw-r--r-- 1 root root 1120 12月 23 13:24 prometheus-configmap.yaml -rw-r--r-- 1 root root 2400 12月 21 20:11 prometheus-deployment.yaml -rw-r--r-- 1 root root 472 12月 21 20:11 prometheus-service.yaml [root@k8s-master-13 kube-state-metrics]# kubectl apply -f . [root@k8s-master-13 prometheus]# kubectl apply -f . configmap/prometheus-config created deployment.apps/prometheus-server created service/prometheus created
如下图所示:
5、测试访问
四、Grafana可视化导入模板
1、导入数据源
2、导入监控模板
在官方模板的基础上个人做了一些修改和调整,主要是适应自己环境和个人审美
官方模板下载地址
个人模板下载地址
个人模板效果图如下:
说明:还有一些图,因为环境原因没有相关数据,所以这里就不截图了。
五、kube-state-metrics指标详解
kube-state-metrics指标记录(一)
kube-state-metrics指标记录(二)
六、kube-state-metrics告警规则文件
总结:整理不易,如果对你有帮助,可否点赞关注一下?
更多详细内容请参考:企业级K8s集群运维实战



