0%

k8s部署metrics-server

metrics-server用于监测node,pod等的CPU,内存使用情况。

官方地址:GitHub

安装

1
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

components.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
k8s-app: metrics-server
rbac.authorization.k8s.io/aggregate-to-admin: "true"
rbac.authorization.k8s.io/aggregate-to-edit: "true"
rbac.authorization.k8s.io/aggregate-to-view: "true"
name: system:aggregated-metrics-reader
rules:
- apiGroups:
- metrics.k8s.io
resources:
- pods
- nodes
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
k8s-app: metrics-server
name: system:metrics-server
rules:
- apiGroups:
- ""
resources:
- pods
- nodes
- nodes/stats
- namespaces
- configmaps
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
labels:
k8s-app: metrics-server
name: metrics-server-auth-reader
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: extension-apiserver-authentication-reader
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: metrics-server
name: metrics-server:system:auth-delegator
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:auth-delegator
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: metrics-server
name: system:metrics-server
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:metrics-server
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: v1
kind: Service
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
spec:
ports:
- name: https
port: 443
protocol: TCP
targetPort: https
selector:
k8s-app: metrics-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
k8s-app: metrics-server
strategy:
rollingUpdate:
maxUnavailable: 0
template:
metadata:
labels:
k8s-app: metrics-server
spec:
containers:
- args:
- --cert-dir=/tmp
- --secure-port=4443
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
- --kubelet-use-node-status-port
- --metric-resolution=15s
- --kubelet-insecure-tls
image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.5.2
# image: k8s.gcr.io/metrics-server/metrics-server:v0.5.2
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 3
httpGet:
path: /livez
port: https
scheme: HTTPS
periodSeconds: 10
name: metrics-server
ports:
- containerPort: 4443
name: https
protocol: TCP
readinessProbe:
failureThreshold: 3
httpGet:
path: /readyz
port: https
scheme: HTTPS
initialDelaySeconds: 20
periodSeconds: 10
resources:
requests:
cpu: 100m
memory: 200Mi
securityContext:
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 1000
volumeMounts:
- mountPath: /tmp
name: tmp-dir
nodeSelector:
kubernetes.io/os: linux
priorityClassName: system-cluster-critical
serviceAccountName: metrics-server
volumes:
- emptyDir: {}
name: tmp-dir
---
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
labels:
k8s-app: metrics-server
name: v1beta1.metrics.k8s.io
spec:
group: metrics.k8s.io
groupPriorityMinimum: 100
insecureSkipTLSVerify: true
service:
name: metrics-server
namespace: kube-system
version: v1beta1
versionPriority: 100

安装注意:

  • 使用阿里镜像源registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.5.2
  • command中增加--kubelet-insecure-tls 关闭证书认证

使用

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
➜  k8s kubectl top pod
NAME CPU(cores) MEMORY(bytes)
izu-mrcar-app-565456b6-bcqw8 9m 617Mi
izu-mrcar-asset-b88685d7-g8z2j 11m 516Mi
izu-mrcar-business-67fbb6f98f-5fsrb 6m 601Mi
izu-mrcar-config-7c5688964-dbq9c 2m 415Mi
izu-mrcar-coupon-64579c6f74-klhkv 3m 508Mi
izu-mrcar-dispatching-6899bf8646-qtxdj 7m 626Mi
izu-mrcar-iot-55f8757d7f-5zxsd 12m 792Mi
izu-mrcar-mrcar-7bff44868c-x9tkh 5m 726Mi
izu-mrcar-order-86d9789997-sdhzj 9m 788Mi
izu-mrcar-user-5985bd4699-dcdm7 5m 503Mi
izu-mrcar-webfront-7bcfb4c76c-g4fxc 1m 339Mi
k8s-nginx-9486b446d-st4xz 1m 341Mi
loki-0 11m 85Mi
loki-grafana-588d9fdccf-kwrjj 2m 50Mi
loki-kube-state-metrics-7f9f667d7d-lft5f 2m 22Mi
loki-prometheus-alertmanager-9bb4c6f8f-tnnd6 3m 15Mi
loki-prometheus-node-exporter-9f4ql 1m 11Mi
loki-prometheus-node-exporter-gh2wq 0m 14Mi
loki-prometheus-pushgateway-664fd45795-9dfdh 1m 12Mi
loki-prometheus-server-5d6f9d5c6c-vmg6b 21m 469Mi
loki-promtail-6f5g2 12m 32Mi
loki-promtail-f8dvw 21m 48Mi
loki-promtail-kg758 34m 55Mi
promtail-test-64b9dbdbf8-jv4vv 11m 18Mi
➜ k8s kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
dev-docker-master-0-14 386m 9% 3132Mi 40%
dev-docker-node-0-15 445m 5% 9940Mi 63%
dev-docker-node-0-17 610m 7% 11593Mi 73%