Kubernetes遇到error: Metrics API not available

Kubernetes cluster encountered "error: Metrics API not available"

The cause of this problem is that Metrics Server is not installed. Next, I will introduce the role of Metrics Server in the cluster and the explanation on the official website. If you want to solve the error directly, please refer to the chapter "Solving "error: Metrics API not available" error".

1. Understand the role of Metrics Server

Kubernetes official website link
Kubernetes Metrics Server is a Kubernetes cluster component that collects metric data for various objects in Kubernetes (such as nodes, pods, containers, etc.), and then aggregates and stores these data so that other Kubernetes components and tools can use these data for Monitor and adjust.

Metrics Server adopts the standard of Kubernetes Metrics API, which uses HTTP interface to expose metric data, and collects these data through polling, and then stores them in memory.

Through the Kubernetes Metrics API, you can get various metrics data, such as CPU utilization, memory usage, network I/O, disk I/O, etc. These indicators can help users understand the health status of each object in the Kubernetes cluster, and help users troubleshoot and optimize performance.

Metrics Server is very important for the normal operation of Kubernetes cluster. If the Metrics Server is not running properly, the status and performance of each object in the cluster will not be monitored, and some Metrics API-based tools and components, such as Horizontal Pod Autoscaler (HPA), will not be available.

In the Kubernetes cluster, the Metrics Server component is not included by default and needs to be installed and configured manually. The methods and processes for installing and configuring Metrics Server vary depending on the Kubernetes version and environment. Usually, you can use the official yaml file provided by Kubernetes for installation, or use some third-party tools and plug-ins for installation and configuration.

2. Solve the "error: Metrics API not available" error

Official solution The
official solution includes two types of Metrics Server installations:
a. Conventional installation of single-copy Metrics Server
b. Multi-copy high-availability installation of Metrics Server.
The following describes the single-copy installation method

#下载yaml文件
wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

#修改yaml文件中的部分内容
vi components.yaml
#需要修改的内容包括Deployment下spec.template.spec.containers.args内添加“- --kubelet-insecure-tls”
#Deployment下spec.template.spec.containers.image修改为“image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.3”

#直接定位到“containers”位点
    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: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.3 #修改为国内镜像仓库地址

If the Kubernetes cluster version is 1.19+, you can directly use the following yaml file to deploy

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: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.3
        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

Flowers are over! ! !

Guess you like

Origin blog.csdn.net/weixin_46660849/article/details/130601159