背景:kubectl top命令可以查看node或者pod的cpu和内存的使用情况,所以我们安装kubectl top命令,这里我使用helm 安装,具体步骤如下:

1、首先添加helm 仓库

helm repo add bitnami https://charts.bitnami.com/bitnami

2、在仓库里搜索chart名字

[root@master base]# helm search repo metrics-server

NAME CHART VERSION APP VERSION DESCRIPTION

bitnami/metrics-server 4.3.1 0.3.7 Metrics Server is a cluster-wide aggregator of ...

3、使用helm安装

[root@master base]# helm install metrics-server bitnami/metrics-server -n kube-system

4、安装完后会看到以下错误提示:

** Please be patient while the chart is being deployed **

The metric server has been deployed.

########################################################################################

### ERROR: The metrics.k8s.io/v1beta1 API service is not enabled in the cluster ###

########################################################################################

You have disabled the API service creation for this release. As the Kubernetes version in the cluster

does not have metrics.k8s.io/v1beta1, the metrics API will not work with this release unless:

Option A:

You complete your metrics-server release by running:

helm upgrade metrics-server bitnami/metrics-server \

--set apiService.create=true

解决方法:

按着他的提示开启apiService服务

[root@master ~]# helm upgrade metrics-server bitnami/metrics-server --set apiService.create=true -n kube-system

然后会得到以下返回值:

NAME: metrics-server

LAST DEPLOYED: Wed Sep 16 16:59:01 2020

NAMESPACE: kube-system

STATUS: deployed

REVISION: 1

TEST SUITE: None

NOTES:

** Please be patient while the chart is being deployed **

The metric server has been deployed.

In a few minutes you should be able to list metrics using the following

command:

kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes"

按他提示的命令查询一下,以验证安装是否成功,如果安装成功会返回一堆值,如果安装不成功使用helm merics-server status查询日志

[root@master base]# kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes"

{"kind":"NodeMetricsList","apiVersion":"metrics.k8s.io/v1beta1","metadata":{"selfLink":"/apis/metrics.k8s.io/v1beta1/nodes"},"items":[{"metadata":{"name":"10.1.6.100","selfLink":"/apis/metrics.k8s.io/v1beta1/nodes/10.1.6.100","creationTimestamp":"2020-09-16T09:27:58Z"},"timestamp":"2020-09-16T09:27:01Z","window":"30s","usage":{"cpu":"223236233n","memory":"4363840Ki"}},{"metadata":{"name":"10.1.6.101","selfLink":"/apis/metrics.k8s.io/v1beta1/nodes/10.1.6.101","creationTimestamp":"2020-09-16T09:27:58Z"},"timestamp":"2020-09-16T09:26:58Z","window":"30s","usage":{"cpu":"1084386738n","memory":"5706400Ki"}},{"metadata":{"name":"10.1.6.99","selfLink":"/apis/metrics.k8s.io/v1beta1/nodes/10.1.6.99","creationTimestamp":"2020-09-16T09:27:58Z"},"timestamp":"2020-09-16T09:27:03Z","window":"30s","usage":{"cpu":"740398897n","memory":"8287948Ki"}}]}

5、安装完后查看日志会有报错提示

E0916 09:08:22.238571 1 reststorage.go:160] unable to fetch pod metrics for pod kube-system/dashboard-metrics-scraper-c79c65bb7-xpqqb: no metrics known for pod

E0916 09:08:23.506206 1 manager.go:111] unable to fully collect metrics: [unable to fully scrape metrics from source kubelet_summary:10.1.6.101: unable to fetch metrics from Kubelet 10.1.6.101 (10.1.6.101): Get https://10.1.6.101:10250/stats/summary?only_cpu_and_memory=true: x509: certificate signed by unknown authority, unable to fully scrape metrics from source kubelet_summary:10.1.6.99: unable to fetch metrics from Kubelet 10.1.6.99 (10.1.6.99): Get https://10.1.6.99:10250/stats/summary?only_cpu_and_memory=true: x509: certificate signed by unknown authority, unable to fully scrape metrics from source kubelet_summary:10.1.6.100: unable to fetch metrics from Kubelet 10.1.6.100 (10.1.6.100): Get https://10.1.6.100:10250/stats/summary?only_cpu_and_memory=true: x509: certificate signed by unknow

6、这时需要修改deploy的一个值

kubectl -n kube-system edit deployments.apps metrics-server

spec:

containers:

- command:

- metrics-server

- --secure-port=8443

- --kubelet-insecure-tls

7、修改后就可以使用kubectl top node查看了

[root@master base]# kubectl top node

NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%

10.1.6.100 222m 1% 4264Mi 14%

10.1.6.101 1081m 9% 5579Mi 18%

10.1.6.99 703m 5% 8120Mi 27%

8、如果在pod重启或pod转移到其他节点后使用kubectl top node出现以下错误提示:

[root@master ~]# kubectl top nodes

Error from server (ServiceUnavailable): the server is currently unable to handle the request (get nodes.metrics.k8s.io)

则需要再修改deploy/metrics-server添加一行参数:

spec:

containers:

- command:

- metrics-server

- --secure-port=8443

- --kubelet-insecure-tls

- --kubelet-preferred-address-types=InternalIP

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐