本文章视频教程地址:https://www.bilibili.com/video/BV1TdxkedE1K
前言
笔者之前写过一篇文章关于在Kubernetes上安装 Prometheus
和 Grafana
监控去查看Kubernetes各个节点的资源占用率,文章地址:https://blog.csdn.net/m0_51510236/article/details/132601350
但是有一个更方便的方法,就是直接使用 kubectl
命令查看各个节点的资源使用情况。具体的命令如下:
kubectl top nodes
但默认的执行结果是:
原因是你没有安装 metrics-server
,metrics-server
的github地址为:https://github.com/kubernetes-sigs/metrics-server。
安装 metrics-server
根据代码仓库的Releases列表显示,最新版本为 v0.7.2
:
其中 v0.7.2
这个版本需要你的 Kubernetes 集群的版本为 1.19
或更高:
官方给出的安装命令为:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
⚠️不要执行这个命令来安装 metrics-server
,需要先将这个文件保存下来,并修改一些内容。可执行下面这行命令修改:
curl -LO https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
如果你是自己搭建的Kubernetes集群,你需要在 yaml 文件的 Deployment 当中添加这么一个执行参数:--kubelet-insecure-tls
。如图加在约 137 行位置:
还有在 image
的配置中,默认镜像是 registry.k8s.io/metrics-server/metrics-server:v0.7.2
,是在谷歌上拉取的,阿里云也有这个镜像,需要改成阿里云上的镜像: registry.aliyuncs.com/google_containers/metrics-server:v0.7.2
,如图:
然后需要将 Deployment.spec.template.spec.hostNetwork
改为 false,如图:
修改好后的文件内容:
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-insecure-tls
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
- --kubelet-use-node-status-port
- --metric-resolution=15s
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
hostNetwork: false
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
然后执行该文件就可以安装成功了,同样我也修改好了这个文件并提交至github,可以啥也不改直接执行下面这样一行命令进行安装:
kubectl apply -f https://raw.githubusercontent.com/xiaohh-me/kubernetes-yaml/main/plugin/metrics-server/components-v0.7.2.yaml
执行完成如图:
可以执行这行命令查看是否安装成功:
kubectl get pod -n kube-system -o wide
成功运行如图:
使用 kubectl 查看 cpu 和内存占用率
接下来就可以执行这行命令查看 cpu 和内存占用率:
kubectl top nodes
执行结果如图: