本文将介绍如何在 Kubernetes 集群上部署 Metrics Server,并使用它来监控集群中各种资源的使用情况。
1. 前提条件
- Kubernetes 集群已经运行,并且 kubectl 工具已经正确配置。
- Metrics Server 的版本符合 Kubernetes 版本要求。例如,如果 Kubernetes 版本为 1.22,则应使用与之对应的 Metrics Server 版本。
2. 部署 Metrics Server
2.1下载 Metrics Server 的部署文件:
$ wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
2.2编辑下载的部署文件,并添加 --kubelet-insecure-tls
参数,以便 Metrics Server 可以使用不安全的 TLS 连接与 kubelet 通信。
$ vim components.yaml
找到以下部分:
containers:
- name: metrics-server
image: k8s.gcr.io/metrics-server/metrics-server:v0.5.0
command:
- /metrics-server
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
- --kubelet-insecure-tls # 添加此行
或者直接使用下面的代码:
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=4443
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
- --kubelet-use-node-status-port
- --metric-resolution=15s
- --kubelet-insecure-tls
image: bitnami/metrics-server:latest
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 3
httpGet:
path: /livez
port: https
scheme: HTTPS
periodSeconds: 10
name: metrics-server
ports:
- containerPort: 4443
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
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 1000
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
2.3 部署 Metrics Server:
$ kubectl apply -f components.yaml
2.4 等待 Metrics Server 部署完成:
$ kubectl get deployment metrics-server -n kube-system
输出示例:
NAME READY UP-TO-DATE AVAILABLE AGE
metrics-server 1/1 1 1 2m
如果 READY 的值为 1/1,则表示 Metrics Server 部署成功。
3. 使用 Metrics Server
现在,Metrics Server 已经在 Kubernetes 集群中部署成功。可以使用 kubectl 命令来获取集群中的度量指标。
3.1 获取节点的 CPU 使用情况:
$ kubectl top node
3.2 获取命名空间中的 Pod 的 CPU 和内存使用情况:
kubectl top pod -n <namespace>
3.3 获取命名空间中的部署的 CPU 和内存使用情况:
$ kubectl top deploy -n <namespace>
总结
在 Kubernetes 集群中部署 Metrics Server 可以实现对集群中各种资源的实时监控和度量指标收集,从而帮助管理员和开发人员更好地管理和优化 Kubernetes 应用程序的性能和可靠性。通过本文所述的步骤,可以轻松部署 Metrics Server 并使用它来监控 Kubernetes 集群中的资源使用情况。
上次更新时间 13 3 月, 2023 at 09:59 上午