本章概述
- 部署kube-state-metrics
- Prometheus配置job采集数据
- Grafana查看监控数据
前言
Kube-state-metrics:用来监控k8s资源状态的组件
通过监听API Server生成有关资源对象的状态指标,比如Deployment、Node、Pod,需要注意的是kube-state-metrics的使用常见不是用于监控对方是否存活,而是用于周期性获取目标对象的metrics指标数据并在web界面进行显示或被prometheus抓取(如pod的状态是running还是Terminating、pod的创建时间等)。目前的kube-state-metrics收集的指标数据可参考官方的文档:https://github.com/kubernetes/kube-state-metrics/tree/master/docs
另外,kube-state-metrics只是简单的提供一个metrics数据,并不会存储这些指标数据,所以我们可以使用Prometheus来抓取这些数据然后存储,主要关注的是业务相关的一些元数据,比如Deployment、Pod、副本状态等,以及调度了多少个 replicas?现在可用的有几个?多少个Pod running/stopped/terminated 状态?Pod 重启了多少次? 目前有多少 job 在运行中。
github链接:
https://github.com/kubernetes/kube-state-metrics
镜像:
https://hub.docker.com/r/bitnami/kube-state-metrics
https://quay.io/repository/coreos/kube-state-metrics?tag=latest&tab=tags
指标数据:
https://github.com/kubernetes/kube-state-metrics/tree/master/docs
目前kube-state-metrics的版本兼容性:
6.1 部署kube-state-metrics
架构图:
(1)kube-state-metrics从api-server获取k8s集群的数据,通过nodeport暴露端口
(2)prometheus通过暴露的端从kube-state-metrics拿到监控数据
注意:镜像文件在谷歌镜像仓库,也可以在dockerhub官网查找,这里使用dokerhub官网的镜像来部署
1、部署kube-state-metrics
(1)编辑yaml文件
vim case5-kube-state-metrics-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: kube-state-metrics
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
app: kube-state-metrics
template:
metadata:
labels:
app: kube-state-metrics
spec:
serviceAccountName: kube-state-metrics
containers:
- name: kube-state-metrics
image: bitnami/kube-state-metrics:2.5.0
ports:
- containerPort: 8080
#创建账号,授权该账号有权限访问k8s集群
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: kube-state-metrics
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: kube-state-metrics
rules:
- apiGroups: [""]
resources: ["nodes", "pods", "services", "resourcequotas", "replicationcontrollers", "limitranges", "persistentvolumeclaims", "persistentvolumes", "namespaces", "endpoints"]
verbs: ["list", "watch"]
- apiGroups: ["extensions"]
resources: ["daemonsets", "deployments", "replicasets"]
verbs: ["list", "watch"]
- apiGroups: ["apps"]
resources: ["statefulsets"]
verbs: ["list", "watch"]
- apiGroups: ["batch"]
resources: ["cronjobs", "jobs"]
verbs: ["list", "watch"]
- apiGroups: ["autoscaling"]
resources: ["horizontalpodautoscalers"]
verbs: ["list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kube-state-metrics
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kube-state-metrics
subjects:
- kind: ServiceAccount
name: kube-state-metrics
namespace: kube-system
#创建service,暴露端口,用于在k8s外部访问
---
apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/scrape: 'true'
name: kube-state-metrics
namespace: kube-system
labels:
app: kube-state-metrics
spec:
type: NodePort
ports:
- name: kube-state-metrics
port: 8080
targetPort: 8080
nodePort: 31666
protocol: TCP
selector:
app: kube-state-metrics
(2)部署kube-state-metrics
kubectl apply -f case5-kube-state-metrics-deploy.yaml
2、验证:
浏览器访问:172.31.7.111:31666
点击metrics按钮,查看数据
6.2 Prometheus配置job采集数据
1、在prometheus监控主机上修改配置
vim /apps/prometheus/prometheus.yml
- job_name: "prometheus-kube-state-metrics"
static_configs:
- targets: ["172.31.7.111:31666"]
2、重启服务
systemctl restart prometheus.service
3、查看监控页面
6.3 grafana查看监控数据
这里使用2.6章节已经部署完成的grafana查看监控数据
在官网找到合适的kube-state-metrics模板导入grafana
1、模板id为:13332
导入模板需要注意:
查看监控数据
2、模板id:13824
这里不再列出导入步骤,直接查看监控效果图:
3、模板id:14518
文章评论