交付kube-state-metrics

kube-state-metrics官方地址
注:我的环境为1.16.7,所以镜像版本为1.9.4

准备镜像

1
2
3
4
5
[root@ops-200 ~]# docker pull quay.io/coreos/kube-state-metrics:v1.9.4
[root@ops-200 ~]# docker images|grep kube-state
quay.io/coreos/kube-state-metrics v1.9.4 3eab960b6ee2 2 months ago 32.8MB
[root@ops-200 ~]# docker tag 3eab960b6ee2 harbor.od.com/public/kube-state-metrics:v1.9.4
[root@ops-200 ~]# docker push harbor.od.com/public/kube-state-metrics:v1.9.4

准备资源配置清单

创建资源目录

1
2
[root@ops-200 ~]# mkdir /data/k8s-yaml/kube-state-metrics
[root@ops-200 ~]# cd /data/k8s-yaml/kube-state-metrics

编写yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
[root@ops-200 kube-state-metrics]# vim rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
app.kubernetes.io/name: kube-state-metrics
app.kubernetes.io/version: v1.9.4
name: kube-state-metrics
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/name: kube-state-metrics
app.kubernetes.io/version: v1.9.4
name: kube-state-metrics
rules:
- apiGroups:
- ""
resources:
- configmaps
- secrets
- nodes
- pods
- services
- resourcequotas
- replicationcontrollers
- limitranges
- persistentvolumeclaims
- persistentvolumes
- namespaces
- endpoints
verbs:
- list
- watch
- apiGroups:
- extensions
resources:
- daemonsets
- deployments
- replicasets
- ingresses
verbs:
- list
- watch
- apiGroups:
- apps
resources:
- statefulsets
- daemonsets
- deployments
- replicasets
verbs:
- list
- watch
- apiGroups:
- batch
resources:
- cronjobs
- jobs
verbs:
- list
- watch
- apiGroups:
- autoscaling
resources:
- horizontalpodautoscalers
verbs:
- list
- watch
- apiGroups:
- authentication.k8s.io
resources:
- tokenreviews
verbs:
- create
- apiGroups:
- authorization.k8s.io
resources:
- subjectaccessreviews
verbs:
- create
- apiGroups:
- policy
resources:
- poddisruptionbudgets
verbs:
- list
- watch
- apiGroups:
- certificates.k8s.io
resources:
- certificatesigningrequests
verbs:
- list
- watch
- apiGroups:
- storage.k8s.io
resources:
- storageclasses
- volumeattachments
verbs:
- list
- watch
- apiGroups:
- admissionregistration.k8s.io
resources:
- mutatingwebhookconfigurations
- validatingwebhookconfigurations
verbs:
- list
- watch
- apiGroups:
- networking.k8s.io
resources:
- networkpolicies
verbs:
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
app.kubernetes.io/name: kube-state-metrics
app.kubernetes.io/version: v1.9.4
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

[root@ops-200 kube-state-metrics]# vim deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
grafanak8sapp: "true"
app.kubernetes.io/name: kube-state-metrics
app.kubernetes.io/version: v1.9.4
name: kube-state-metrics
namespace: kube-system
spec:
selector:
matchLabels:
grafanak8sapp: "true"
app.kubernetes.io/name: kube-state-metrics
template:
metadata:
labels:
grafanak8sapp: "true"
app.kubernetes.io/name: kube-state-metrics
app.kubernetes.io/version: v1.9.4
spec:
containers:
- name: kube-state-metrics
image: harbor.od.com/public/kube-state-metrics:v1.9.4
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
name: http-metrics
- containerPort: 8081
name: telemetry
readinessProbe:
httpGet:
path: /
port: 8081
initialDelaySeconds: 5
timeoutSeconds: 5
serviceAccountName: kube-state-metrics

应用资源配置清单

1
2
3
4
5
6
7
[root@ops-200 kube-state-metrics]# kubectl apply -f rbac.yaml 
serviceaccount/kube-state-metrics created
clusterrole.rbac.authorization.k8s.io/kube-state-metrics created
clusterrolebinding.rbac.authorization.k8s.io/kube-state-metrics created
[root@ops-200 kube-state-metrics]# vim deployment.yaml
[root@ops-200 kube-state-metrics]# kubectl apply -f deployment.yaml
deployment.apps/kube-state-metrics created

查看数据

1
2
3
[root@k8s01-21 ~]# kubectl get pod -n kube-system -o wide|grep kube-state
kube-state-metrics-6dfcf5c7d8-2xjqc 1/1 Running 0 3m6s 172.7.23.10 k8s03-23.host.com <none> <none>
[root@k8s01-21 ~]# [root@k8s01-21 ~]# curl 172.7.23.10:8080/metrics