一、介绍
在新版的K8S中,系统资源的采集均使用Metrics-Server服务,可以通过Metrics-Server服务采集节点和Pod的内存、磁盘、CPU和网络的使用率等信息。
1、新版K8S资源使用情况的度量(如容器的 CPU 和内存使用)可以通过 Metrics API 获取。
注意:
Metrics API 只可以查询当前的度量数据,并不保存历史数据
Metrics API URI 为 /apis/metrics.k8s.io/
,在 k8s.io/metrics维护
必须部署 metrics-server 才能使用该 API,metrics-server 通过调用 Kubelet Summary API 获取数据
二、部署Metrics-Server服务
1、下载解压
https://github.com/kubernetes-sigs/metrics-server/archive/v0.3.6.tar.gz tar -zxvf v0.3.6.tar.gz
2、修改配置文件
cd metrics-server-0.3.6/deploy/1.8+/ 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: k8s.gcr.io/metrics-server-amd64:v0.3.6 imagePullPolicy: Always volumeMounts: - name: tmp-dir mountPath: /tmp
修改后的配置文件
--- 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 和 imagePullPolicy image: mirrorgooglecontainers/metrics-server-amd64:v0.3.6 imagePullPolicy: IfNotPresent # 新增command配置 command: - /metrics-server - --kubelet-insecure-tls - --kubelet-preferred-address-types=InternalDNS,InternalIP,ExternalDNS,ExternalIP,Hostname volumeMounts: - name: tmp-dir mountPath: /tmp # 新增resources配置 resources: limits: cpu: 300m memory: 200Mi requests: cpu: 200m memory: 100Mi
修改完metrics-server-deployment.yaml文件后保存退出。
3、安装Metrics-Server
kubectl apply -f metrics-server-0.3.6/deploy/1.8+/
4、查看node信息
[root@prod-128 1.8+]# kubectl top node NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% prod-121 356m 1% 5580Mi 2% prod-122 442m 1% 4503Mi 1% prod-124 269m 0% 5113Mi 1% prod-125 504m 1% 7412Mi 2% prod-126 491m 1% 18809Mi 7% prod-128 426m 1% 5910Mi 2%
注意事项:
下载Metrics-Server安装文件之后,一定要修改metrics-server-deployment.yaml文件,在使用kubectl top node
命令查看node信息时,会报如下错误。
error: metrics not available yet