kubernetes1.16 版本对应最新的metrics-server为v0.3.5。
下载metrics-server配置文件
git clone https://github.com/kubernetes-incubator/metrics-server
kubernetes1.16 对应的文件再目录metrics-server/deploy/1.8+
下。
下载后还要修改配置文件metrics-server/deploy/1.8+/resource-reader.yaml
。
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: system:metrics-server
rules:
- apiGroups:
- ""
resources:
- pods
- nodes
- nodes/stats
- namespaces ## 如果这行没有请添加
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: system:metrics-server
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:metrics-server
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
修改metrics-server/deploy/1.8+/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.5
command:
- /metrics-server
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP # 如果不配置此项,会报错找不到node
imagePullPolicy: IfNotPresent # 修改image拉取策略
volumeMounts:
- name: tmp-dir
mountPath: /tmp
安装metrics-server镜像
注:如果你的主机可以访问k8s.gcr.io就跳过这个步骤
因为国内用户无法直接拉取k8s.gcr.io上的镜像,解决的方法有几种。这里选择下载国内相同版本镜像再修改镜像的tag。
首先搜索镜像:
[root@k8s-node2 ~]# docker search metrics-server-amd64
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mirrorgooglecontainers/metrics-server-amd64 14
...
或者直接从https://hub.docker.com查找。
下载对应版本的镜像
[root@k8s-node2 ~]# docker pull mirrorgooglecontainers/metrics-server-amd64:v0.3.5
修改镜像tag,并删除原来的tag
[root@k8s-node2 ~]# docker tag docker.io/mirrorgooglecontainers/metrics-server-amd64:v0.3.5 k8s.gcr.io/metrics-server-amd64:v0.3.5
[root@k8s-node2 ~]# docker rmi docker.io/mirrorgooglecontainers/metrics-server-amd64:v0.3.5
安装metrics-server
准备完成后就安装metrics-server了:
[root@k8s-master 1.8+]# cd /root/metrics-server/deploy/1.8+/
[root@k8s-master 1.8+]# kubectl create -f .
[root@k8s-master 1.8+]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
...
metrics-server-5cb6c5d66b-7979s 1/1 Running 0 83m
安装完成后一段时间就可以看到以下的信息:
[root@k8s-master 1.8+]# 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":"k8s-master","selfLink":"/apis/metrics.k8s.io/v1beta1/nodes/k8s-master","creationTimestamp":"2019-10-09T14:01:45Z"},"timestamp":"2019-10-09T14:01:43Z","window":"30s","usage":{"cpu":"142548542n","memory":"936912Ki"}},{"metadata":{"name":"k8s-node1","selfLink":"/apis/metrics.k8s.io/v1beta1/nodes/k8s-node1","creationTimestamp":"2019-10-09T14:01:45Z"},"timestamp":"2019-10-09T14:01:39Z","window":"30s","usage":{"cpu":"36929393n","memory":"442216Ki"}},{"metadata":{"name":"k8s-node2","selfLink":"/apis/metrics.k8s.io/v1beta1/nodes/k8s-node2","creationTimestamp":"2019-10-09T14:01:45Z"},"timestamp":"2019-10-09T14:01:38Z","window":"30s","usage":{"cpu":"32871811n","memory":"451752Ki"}}]}