Kubernetes 部署metrics-server
Metrics Server是Kubernetes内置自动缩放管道的可扩展,高效的容器资源指标来源。
Metrics Server从Kubelet收集资源指标,并通过Metrics API在Kubernetes apiserver中公开它们,以供Horizontal Pod Autoscaler使用,也可以通过访问Metrics API kubectl top,用于监测node,pod等的CPU,内存使用情况。
metrics-server github地址:
https://github.com/kubernetes-sigs/metrics-server
https://github.com/kubernetes-sigs/metrics-server/archive/v0.3.6.tar.gz
软件版本信息:
Kubernetes v1.18.0
metrics-server 0.3.6
1、开启聚合层,查看证书是否存在:
[root@k8s-master ~]# ll /etc/kubernetes/pki/ total 56 -rw-r--r-- 1 root root 1224 Jan 25 15:18 apiserver.crt -rw-r--r-- 1 root root 1090 Jan 25 15:18 apiserver-etcd-client.crt -rw------- 1 root root 1675 Jan 25 15:18 apiserver-etcd-client.key -rw------- 1 root root 1675 Jan 25 15:18 apiserver.key -rw-r--r-- 1 root root 1099 Jan 25 15:18 apiserver-kubelet-client.crt -rw------- 1 root root 1679 Jan 25 15:18 apiserver-kubelet-client.key -rw-r--r-- 1 root root 1025 Jan 25 15:18 ca.crt -rw------- 1 root root 1675 Jan 25 15:18 ca.key drwxr-xr-x 2 root root 162 Jan 25 15:18 etcd -rw------- 1 root root 1675 Jan 25 15:18 sa.key -rw------- 1 root root 451 Jan 25 15:18 sa.pub ## 以下几个文件 -rw-r--r-- 1 root root 1038 Jan 25 15:18 front-proxy-ca.crt -rw------- 1 root root 1675 Jan 25 15:18 front-proxy-ca.key -rw-r--r-- 1 root root 1058 Jan 25 15:18 front-proxy-client.crt -rw------- 1 root root 1675 Jan 25 15:18 front-proxy-client.key [root@k8s-master ~]#
2、修改apiserver文件,新增配置
[root@k8s-master ~]# vim /etc/kubernetes/manifests/kube-apiserver.yaml ... - --requestheader-allowed-names=aggregator - --requestheader-extra-headers-prefix=X-Remote-Extra- - --requestheader-group-headers=X-Remote-Group - --requestheader-username-headers=X-Remote-User - --runtime-config=api/all=true - --enable-aggregator-routing=true ...
3、重启apiservide服务:
[root@k8s-master ~]# systemctl restart kubelet.service
4、下载并部署metrics-server
下载地址:
https://github.com/kubernetes-sigs/metrics-server/archive/v0.3.6.tar.gz
5、修改对应文件,添加和修改
[root@k8s-master ~]# cd k8s-addn/metrics-server-0.3.6/deploy/1.8+/
# vim metrics-server-deployment.yaml
...... - name: metrics-server ## 修改国内镜像 image: registry.cn-hangzhou.aliyuncs.com/ljck8s/metrics-server-amd64:v0.3.6 imagePullPolicy: IfNotPresent # 添加command command: - /metrics-server - --metric-resolution=30s - --kubelet-insecure-tls - --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP ......
6、部署metrics-server
#部署服务 [root@k8s-master 1.8+]# kubectl apply -f . #查看 metrics-server服务是否正常 #查看apiservice [root@k8s-master deploy]# kubectl get apiservice |grep metrics-server v1beta1.metrics.k8s.io kube-system/metrics-server True 31m #查看 metrics-server pod [root@k8s-master deploy]# kubectl get pod -A NAMESPACE NAME READY STATUS RESTARTS AGE kube-system coredns-7ff77c879f-4d2wt 1/1 Running 0 14d kube-system coredns-7ff77c879f-kjvtg 1/1 Running 0 14d kube-system etcd-k8s-master 1/1 Running 0 14d kube-system kube-apiserver-k8s-master 1/1 Running 0 35m kube-system kube-controller-manager-k8s-master 1/1 Running 1 14d kube-system kube-flannel-ds-amd64-49dr9 1/1 Running 0 14d kube-system kube-flannel-ds-amd64-fhd55 1/1 Running 0 14d kube-system kube-flannel-ds-amd64-tx5gg 1/1 Running 0 14d kube-system kube-proxy-92v9b 1/1 Running 0 14d kube-system kube-proxy-dj6gz 1/1 Running 0 14d kube-system kube-proxy-dr7nc 1/1 Running 0 14d kube-system kube-scheduler-k8s-master 1/1 Running 1 14d kube-system metrics-server-6495d87ccb-4zp2l 1/1 Running 0 33m kubernetes-dashboard dashboard-metrics-scraper-694557449d-nhxc9 1/1 Running 0 14d kubernetes-dashboard kubernetes-dashboard-9774cc786-r2qnx 1/1 Running 0 14d #查看 metrics-server的svc服务 [root@k8s-master deploy]# kubectl get svc -A NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE default kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 14d kube-system kube-dns ClusterIP 10.1.0.10 <none> 53/UDP,53/TCP,9153/TCP 14d kube-system metrics-server ClusterIP 10.1.131.73 <none> 443/TCP 130m kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.1.150.226 <none> 8000/TCP 14d kubernetes-dashboard kubernetes-dashboard NodePort 10.1.134.131 <none> 443:30001/TCP 14d
7、服务都已经正常,执行kubectl top命令查看资源情况.
[root@k8s-master deploy]# kubectl top nodes NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% k8s-master 335m 8% 1105Mi 64% k8s-node01 105m 2% 924Mi 25% k8s-node02 103m 2% 767Mi 20% [root@k8s-master deploy]# [root@k8s-master deploy]# kubectl top pods -A NAMESPACE NAME CPU(cores) MEMORY(bytes) kube-system coredns-7ff77c879f-4d2wt 6m 15Mi kube-system coredns-7ff77c879f-kjvtg 8m 14Mi kube-system etcd-k8s-master 40m 83Mi kube-system kube-apiserver-k8s-master 137m 372Mi kube-system kube-controller-manager-k8s-master 48m 62Mi kube-system kube-flannel-ds-amd64-49dr9 4m 18Mi kube-system kube-flannel-ds-amd64-fhd55 3m 16Mi kube-system kube-flannel-ds-amd64-tx5gg 3m 19Mi kube-system kube-proxy-92v9b 2m 20Mi kube-system kube-proxy-dj6gz 2m 32Mi kube-system kube-proxy-dr7nc 2m 17Mi kube-system kube-scheduler-k8s-master 8m 26Mi kube-system metrics-server-6495d87ccb-4zp2l 2m 14Mi
可以看到每个服务的CPU和内存使用信息,metrics-server部署成功.
8、试试metrics-server的API服务,执行命令kubectl proxy --port=8080,用来开代理端口;
再开打一个同样的ssh连接,执行命令curl localhost:8080/apis/metrics.k8s.io/v1beta1/,如下图,可以看到API返回的内容:
[root@k8s-master deploy]# curl localhost:8080/apis/metrics.k8s.io/v1beta1/ { "kind": "APIResourceList", "apiVersion": "v1", "groupVersion": "metrics.k8s.io/v1beta1", "resources": [ { "name": "nodes", "singularName": "", "namespaced": false, "kind": "NodeMetrics", "verbs": [ "get", "list" ] }, { "name": "pods", "singularName": "", "namespaced": true, "kind": "PodMetrics", "verbs": [ "get", "list" ] } ] } [root@k8s-master deploy]#
查看名为 metrics-server-6495d87ccb-4zp2l 的pod的基本信息,命令是:
[root@k8s-master deploy]# curl localhost:8080/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/metrics-server-6495d87ccb-4zp2l { "kind": "PodMetrics", "apiVersion": "metrics.k8s.io/v1beta1", "metadata": { "name": "metrics-server-6495d87ccb-4zp2l", "namespace": "kube-system", "selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/metrics-server-6495d87ccb-4zp2l", "creationTimestamp": "2021-02-09T05:18:36Z" }, "timestamp": "2021-02-09T05:18:02Z", "window": "30s", "containers": [ { "name": "metrics-server", "usage": { "cpu": "724562n", "memory": "14584Ki" } } ] } [root@k8s-master deploy]#
部署metrics-server时遇到的问题:
[root@k8s-master 1.8+]# kubectl top nodes Error from server (ServiceUnavailable): the server is currently unable to handle the request (get nodes.metrics.k8s.io)
解决方法:
修改apiserver文件(在第一步,开启聚合层)
参考文档:
https://github.com/kubernetes-sigs/metrics-server
https://www.jianshu.com/p/e990aaf3ae28
https://blog.csdn.net/boling_cavalry/article/details/105006295
https://blog.csdn.net/passnetY/article/details/104659618