在Kubernetes集群上安装Metrics Server


前言
Kubernetes集群使用Metrics Server来采集容器的CPU和内存使用情况,用来监控Node和Pod的CPU和内存使用情况,并用作HPA(自动横向扩展)。

本文描述了如何在Kubernetes集群上安装Metrics Server。

 

CentOS7 x86_84
kubeadm方式安装Kubernetes集群
Kubernetes v1.19.3
Metrics Server v0.3.7


参考文档
https://github.com/kubernetes-sigs/metrics-server
https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.7/components.yaml

 


安装前检查网络
Metrics Server依赖于Kubernetes网络插件来保持Metrics Server与kubelet的通信。

以Calico为例,需要先检查Calico的安装情况。

1 # Check daemonset 
2 kubectl get ds -n kube-system -l k8s-app=calico-node
3 
4 # Check pod status and ready
5 kubectl get pods -n kube-system -l k8s-app=calico-node
6 
7 # Check apiservice status
8 kubectl get apiservice v1.crd.projectcalico.org -o yaml

安装Metrics Server
下载Metrics Server的Manifest文件:

修改components.yaml:

将Deployment的.spec.template.spec.containers.image修改为可以正常拉取的metrics-server:v0.3.7镜像。

在Deployment的.spec.template.spec.containers.args下添加:

- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname

说明:

--kubelet-insecure-tls: skip verifying Kubelet CA certificates.

--kubelet-preferred-address-types: Order to consider Kubelet node address types when connecting to Kubelet.

一个完整的例子参见:

metrics-server-insecure.yaml
安装Metrics Server:

1 kubectl apply -f components.yaml
 1 验证Metrics Server:
 2 
 3 # Check deployment
 4 kubectl get deployment metrics-server -n kube-system
 5 
 6 # Check pod status and ready
 7 kubectl get pods -n kube-system -l k8s-app=metrics-server
 8 
 9 # Check apiservice status
10 kubectl get apiservice v1beta1.metrics.k8s.io -o yaml


等待几分钟后,验证是否可以从Metrics Server查询到资源使用情况:

1 kubectl top nodes
2 kubectl top pods -A


如果出现“error: metrics not available yet”,则等待几分钟后再尝试。如果一直报错,需要先检查网络组件是否正常工作,再检查Metrics Server的日志。

如果仍然启动Metrics Server失败,考虑使用hostNetWork方式,参见:

metrics-server-insecure-hostnetwork.yaml

The reason we had to use “hostNetwork: true” was because the ip of the node was included in the CIDR of the pod network.

参考文档
https://github.com/kubernetes-sigs/metrics-server/issues/300
https://github.com/kubernetes-sigs/metrics-server/issues/278
https://computingforgeeks.com/how-to-deploy-metrics-server-to-kubernetes-cluster/
https://github.com/kubernetes-sigs/metrics-server/blob/master/FAQ.md

posted @ 2021-04-02 16:10  纯捡垃圾吃的  阅读(493)  评论(0编辑  收藏  举报
返回顶部