1、Metrics Server介绍
Metrics Server 是集群级别的资源利用率数据的聚合器。从 Kubelets收集资源指标,并通过 Metrics API 在 Kubernetes apiserver 中公开它们,以供 Horizontal Pod Autoscaler 和Vertical Pod Autoscaler 使用。
Metrics API 也可以通过访问 kubectl top,从而更容易调试自动缩放管道。
Metrics Server基于内存存储,重启后数据将全部丢失,而且它仅能留存最近收集到的指标数据。
二、安装Metrics Server
1、下载配置文件
$ wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
2、修改components.yaml配置文件
$ cat components.yaml
apiVersion: v1
kind: ServiceAccount
metadata:labels:k8s-app: metrics-servername: metrics-servernamespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:labels:k8s-app: metrics-serverrbac.authorization.k8s.io/aggregate-to-admin: "true"rbac.authorization.k8s.io/aggregate-to-edit: "true"rbac.authorization.k8s.io/aggregate-to-view: "true"name: system:aggregated-metrics-reader
rules:
- apiGroups:- metrics.k8s.ioresources:- pods- nodesverbs:- get- list- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:labels:k8s-app: metrics-servername: system:metrics-server
rules:
- apiGroups:- ""resources:- nodes/metricsverbs:- get
- apiGroups:- ""resources:- pods- nodesverbs:- get- list- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:labels:k8s-app: metrics-servername: metrics-server-auth-readernamespace: kube-system
roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: extension-apiserver-authentication-reader
subjects:
- kind: ServiceAccountname: metrics-servernamespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:labels:k8s-app: metrics-servername: metrics-server:system:auth-delegator
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: system:auth-delegator
subjects:
- kind: ServiceAccountname: metrics-servernamespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:labels:k8s-app: metrics-servername: system:metrics-server
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: system:metrics-server
subjects:
- kind: ServiceAccountname: metrics-servernamespace: kube-system
---
apiVersion: v1
kind: Service
metadata:labels:k8s-app: metrics-servername: metrics-servernamespace: kube-system
spec:ports:- name: httpsport: 443protocol: TCPtargetPort: httpsselector:k8s-app: metrics-server
---
apiVersion: apps/v1
kind: Deployment
metadata:labels:k8s-app: metrics-servername: metrics-servernamespace: kube-system
spec:selector:matchLabels:k8s-app: metrics-serverstrategy:rollingUpdate:maxUnavailable: 0template:metadata:labels:k8s-app: metrics-serverspec:containers:- args:- --cert-dir=/tmp- --secure-port=10250- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname- --kubelet-use-node-status-port- --metric-resolution=15s- --kubelet-insecure-tls#image: registry.k8s.io/metrics-server/metrics-server:v0.7.2image: registry.aliyuncs.com/google_containers/metrics-server:v0.7.2imagePullPolicy: IfNotPresentlivenessProbe:failureThreshold: 3httpGet:path: /livezport: httpsscheme: HTTPSperiodSeconds: 10name: metrics-serverports:- containerPort: 10250name: httpsprotocol: TCPreadinessProbe:failureThreshold: 3httpGet:path: /readyzport: httpsscheme: HTTPSinitialDelaySeconds: 20periodSeconds: 10resources:requests:cpu: 100mmemory: 200MisecurityContext:allowPrivilegeEscalation: falsecapabilities:drop:- ALLreadOnlyRootFilesystem: truerunAsNonRoot: truerunAsUser: 1000seccompProfile:type: RuntimeDefaultvolumeMounts:- mountPath: /tmpname: tmp-dirnodeSelector:kubernetes.io/os: linuxpriorityClassName: system-cluster-criticalserviceAccountName: metrics-servervolumes:- emptyDir: {}name: tmp-dir
---
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:labels:k8s-app: metrics-servername: v1beta1.metrics.k8s.io
spec:group: metrics.k8s.iogroupPriorityMinimum: 100insecureSkipTLSVerify: trueservice:name: metrics-servernamespace: kube-systemversion: v1beta1versionPriority: 100
修改镜像并新增--kubelet-insecure-tls参数:
3、安装
$ kubectl apply -f components.yaml
serviceaccount/metrics-server created
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
clusterrole.rbac.authorization.k8s.io/system:metrics-server created
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
service/metrics-server created
deployment.apps/metrics-server created
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
4、查看
$ kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-658d97c59c-qjdrt 1/1 Running 12 (56m ago) 6h48m
kube-system calico-node-5msx4 1/1 Running 0 6h48m
kube-system calico-node-5q749 1/1 Running 2 (54m ago) 6h48m
kube-system calico-node-z9gtq 1/1 Running 0 6h48m
kube-system coredns-66f779496c-2v6l9 1/1 Running 1 7h18m
kube-system coredns-66f779496c-kj6db 1/1 Running 3 (81m ago) 7h18m
kube-system etcd-k8s-master 1/1 Running 7 7h18m
kube-system kube-apiserver-k8s-master 1/1 Running 6 7h18m
kube-system kube-controller-manager-k8s-master 1/1 Running 6 (54m ago) 7h18m
kube-system kube-proxy-77vqw 1/1 Running 0 7h18m
kube-system kube-proxy-cmnxt 1/1 Running 0 7h16m
kube-system kube-proxy-ksrmj 1/1 Running 0 7h17m
kube-system kube-scheduler-k8s-master 1/1 Running 6 (55m ago) 7h18m
kube-system metrics-server-794bb5d868-ghk49 1/1 Running 0 61s
可见已在Kube-system名称空间中成功运行
5、测试
$ kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
k8s-master 199m 9% 1520Mi 41%
k8s-node1 77m 3% 914Mi 53%
k8s-node2 87m 4% 911Mi 52%
$ kubectl top pod -A
NAMESPACE NAME CPU(cores) MEMORY(bytes)
kube-system calico-kube-controllers-658d97c59c-qjdrt 1m 11Mi
kube-system calico-node-5msx4 45m 105Mi
kube-system calico-node-5q749 44m 69Mi
kube-system calico-node-z9gtq 42m 128Mi
kube-system coredns-66f779496c-2v6l9 2m 13Mi
kube-system coredns-66f779496c-kj6db 2m 11Mi
kube-system etcd-k8s-master 25m 86Mi
kube-system kube-apiserver-k8s-master 76m 288Mi
kube-system kube-controller-manager-k8s-master 23m 50Mi
kube-system kube-proxy-77vqw 1m 18Mi
kube-system kube-proxy-cmnxt 1m 31Mi
kube-system kube-proxy-ksrmj 1m 36Mi
kube-system kube-scheduler-k8s-master 3m 22Mi
kube-system metrics-server-794bb5d868-ghk49 4m 15Mi