k8s集群部署metrics-server
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-server
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
k8s-app: metrics-server
rbac.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.io
resources:
- pods
- nodes
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
k8s-app: metrics-server
name: system:metrics-server
rules:
- apiGroups:
- ""
resources:
- nodes/metrics
verbs:
- get
- apiGroups:
- ""
resources:
- pods
- nodes
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
labels:
k8s-app: metrics-server
name: metrics-server-auth-reader
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: extension-apiserver-authentication-reader
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: metrics-server
name: metrics-server:system:auth-delegator
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:auth-delegator
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: metrics-server
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
---
apiVersion: v1
kind: Service
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
spec:
ports:
- name: https
port: 443
protocol: TCP
targetPort: https
selector:
k8s-app: metrics-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
spec:
selector:
matchLabels:
k8s-app: metrics-server
strategy:
rollingUpdate:
maxUnavailable: 0
template:
metadata:
labels:
k8s-app: metrics-server
spec:
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.2
image: registry.aliyuncs.com/google_containers/metrics-server:v0.7.2
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 3
httpGet:
path: /livez
port: https
scheme: HTTPS
periodSeconds: 10
name: metrics-server
ports:
- containerPort: 10250
name: https
protocol: TCP
readinessProbe:
failureThreshold: 3
httpGet:
path: /readyz
port: https
scheme: HTTPS
initialDelaySeconds: 20
periodSeconds: 10
resources:
requests:
cpu: 100m
memory: 200Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 1000
seccompProfile:
type: RuntimeDefault
volumeMounts:
- mountPath: /tmp
name: tmp-dir
nodeSelector:
kubernetes.io/os: linux
priorityClassName: system-cluster-critical
serviceAccountName: metrics-server
volumes:
- emptyDir: {}
name: tmp-dir
---
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
labels:
k8s-app: metrics-server
name: v1beta1.metrics.k8s.io
spec:
group: metrics.k8s.io
groupPriorityMinimum: 100
insecureSkipTLSVerify: true
service:
name: metrics-server
namespace: kube-system
version: v1beta1
versionPriority: 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