kubernetes 从1.8版本开始不再集成cadvisor,也废弃了heapster,使用metrics server来提供metrics,这篇文章介绍如何在kubernetes 1.17中部署metrics server。
$ git clone https://github.com/kubernetes-incubator/metrics-server
由于默认镜像是谷歌,如果pull不下来,换成微软的镜像来覆盖默认的 gcr.io 镜像
# metrics-server-deployment.yaml 默认镜像
image: k8s.gcr.io/metrics-server-amd64:v0.3.6
# 替换成微软的镜像
image: gcr.azk8s.cn/google_containers/metrics-server-amd64:v0.3.6
部署metrics Server
$ kubectl apply -f metrics-server/deploy/1.8+/
检查metrics server状态
$ kubectl get pods -n kube-system |grep metrics
metrics-server-8484595bcc-cwgct 1/1 Running 0 9m44s
$ kubectl logs -f metrics-server-8484595bcc-cwgct -n kube-system
I1231 10:08:24.220741 1 serving.go:312] Generated self-signed cert (/tmp/apiserver.crt, /tmp/apiserver.key)
I1231 10:08:25.933196 1 secure_serving.go:116] Serving securely on [::]:4443
E1231 10:08:28.409943 1 reststorage.go:135] unable to fetch node metrics for node "node-100": no metrics known for node
E1231 10:08:28.409986 1 reststorage.go:135] unable to fetch node metrics for node "node-101": no metrics known for node
E1231 10:08:28.409996 1 reststorage.go:135] unable to fetch node metrics for node "node-102": no metrics known for node
E1231 10:08:30.098755 1 reststorage.go:135] unable to fetch node metrics for node "node-100": no metrics known for node
E1231 10:08:30.098803 1 reststorage.go:135] unable to fetch node metrics for node "node-101": no metrics known for node
E1231 10:08:30.098816 1 reststorage.go:135] unable to fetch node metrics for node "node-102": no metrics known for node
E1231 10:09:09.746135 1 reststorage.go:135] unable to fetch node metrics for node "node-100": no metrics known for node
E1231 10:09:09.746189 1 reststorage.go:135] unable to fetch node metrics for node "node-101": no metrics known for node
E1231 10:09:09.746199 1 reststorage.go:135] unable to fetch node metrics for node "node-102": no metrics known for node
测试发现通过修改metrics-server-deployment.yaml,添加特定参数可以解决
完整的Deployment
vim metrics-server-deployment.yaml
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: metrics-server
namespace: kube-system
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: metrics-server
namespace: kube-system
labels:
k8s-app: metrics-server
spec:
selector:
matchLabels:
k8s-app: metrics-server
template:
metadata:
name: metrics-server
labels:
k8s-app: metrics-server
spec:
serviceAccountName: metrics-server
volumes:
# mount in tmp so we can safely use from-scratch images and/or read-only containers
- name: tmp-dir
emptyDir: {}
containers:
- name: metrics-server
image: gcr.azk8s.cn/google_containers/metrics-server-amd64:v0.3.6 #换成了微软镜像
args:
- --cert-dir=/tmp
- --secure-port=4443
- --kubelet-insecure-tls #避免x509: cannot validate certificate
- --kubelet-preferred-address-types=InternalIP # 能够通过hostname解析
ports:
- name: main-port
containerPort: 4443
protocol: TCP
securityContext:
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 1000
imagePullPolicy: Always
volumeMounts:
- name: tmp-dir
mountPath: /tmp
nodeSelector:
beta.kubernetes.io/os: linux
重写应用yaml,生效后几分钟可以访问得到数据:
$ kubectl apply -f metrics-server/deploy/1.8+/
探测metrics api
有两种方式可以测试metrics api,分别是kubectl和rest api
首先我们看第一种
$ kubectl top nodes
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
node-100 544m 27% 1744Mi 30%
node-101 255m 12% 749Mi 20%
node-102 284m 14% 831Mi 22%
第二种方式
首先需要加个proxy
$ kubectl proxy --port=8081 &
$ curl http://localhost:8081/apis/metrics.k8s.io/v1beta1/nodes
{
"kind": "NodeMetricsList",
"apiVersion": "metrics.k8s.io/v1beta1",
"metadata": {
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes"
},
"items": [
{
"metadata": {
"name": "node-101",
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/node-101",
"creationTimestamp": "2019-12-31T10:40:40Z"
},
"timestamp": "2019-12-31T10:40:25Z",
"window": "30s",
"usage": {
"cpu": "265935339n",
"memory": "766944Ki"
}
},
{
"metadata": {
"name": "node-102",
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/node-102",
"creationTimestamp": "2019-12-31T10:40:40Z"
},
"timestamp": "2019-12-31T10:40:18Z",
"window": "30s",
"usage": {
"cpu": "269443376n",
"memory": "850940Ki"
}
},
{
"metadata": {
"name": "node-100",
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/node-100",
"creationTimestamp": "2019-12-31T10:40:40Z"
},
"timestamp": "2019-12-31T10:40:18Z",
"window": "30s",
"usage": {
"cpu": "498030367n",
"memory": "1778056Ki"
}
}
]
}
网友评论