二十五、Kubernetes安装metrics-server详解、实例

有时候,不是因为你没有能力,也不是因为你缺少勇气,只是因为你付出的努力还太少,所以,成功便不会走向你。而你所需要做的,就是坚定你的梦想,你的目标,你的未来,然后以不达目的誓不罢休的那股劲,去付出你的努力,成功就会慢慢向你靠近。

导读:本篇文章讲解 二十五、Kubernetes安装metrics-server详解、实例,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

1、概述

        Metrics Server 是一种可扩展、高效的容器资源指标来源,适用于 Kubernetes 内置的自动缩放管道。
        Metrics Server 从 Kubelets 收集资源指标,并通过Metrics API将它们暴露在 Kubernetes apiserver 中,供 Horizontal Pod Autoscaler和Vertical Pod Autoscaler使用。指标 API 也可以通过 访问kubectl top,从而更容易调试自动缩放管道。

2、metrics-server安装要求

        Metrics Server 对集群和网络配置有特定的要求。这些要求并不是所有集群分布的默认要求。在使用 Metrics Server 之前,请确保您的集群分布支持这些要求:

  • kube-apiserver 必须启用聚合层。
  • 节点必须启用 Webhook身份验证和授权。
  • Kubelet 证书需要由集群证书颁发机构签名(或通过传递–kubelet-insecure-tls给 Metrics Server 来禁用证书验证)注意这里是重点,如果本地没有签名需要传递args : “–kubelet-insecure-tls” 给Metrics Server  
  • 容器运行时必须实现容器指标 RPC(或具有cAdvisor支持)
  • 网络应支持以下通信:
    • 控制平面到指标服务器。控制平面节点需要到达 Metrics Server 的 pod IP 和端口 10250(或节点 IP 和自定义端口,如果hostNetwork已启用)
    • 所有节点上的 Kubelet 的指标服务器。指标服务器需要到达节点地址和 Kubelet 端口。地址和端口在 Kubelet 中配置并作为 Node 对象的一部分发布。字段中的地址.status.addresses和端口.status.daemonEndpoints.kubeletEndpoint.port(默认 10250)。Metrics Server 将根据kubelet-preferred-address-types命令行标志(InternalIP,ExternalIP,Hostname清单中的默认值)提供的列表选择第一个节点地址

3、版本对照

Metrics Server Metrics API group/version 支持k8s版本
0.6.x metrics.k8s.io/v1beta1 1.19+
0.5.x metrics.k8s.io/v1beta1 *1.8+
0.4.x metrics.k8s.io/v1beta1 *1.8+
0.3.x metrics.k8s.io/v1beta1 1.8-1.21

4、kubernetes1.25安装metrics-server0.6.2

由于我们本地是kubernetes1.25版本的,按照metrics-server 要求,需要安装metrics-server0.6版本

4.1、下载metrics-server0.6.2 yaml 并修改

下载地址:https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.6.2/components.yaml

主要 改两个地方

  • 134行添加参数 “–kubelet-insecure-tls” 用以禁用证书验证
  • 140行更改镜像地址为阿里云:“registry.cn-hangzhou.aliyuncs.com/chenby/metrics-server:v0.6.2”

更改后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:
        - --kubelet-insecure-tls
        - --cert-dir=/tmp
        - --secure-port=4443
        - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
        - --kubelet-use-node-status-port
        - --metric-resolution=15s
        image: registry.cn-hangzhou.aliyuncs.com/chenby/metrics-server:v0.6.2
        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

4.2、执行并验证

# 执行yaml
[root@k8s-master ~]# 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


# 验证 metrics-server 使用的命名空间为 kube-system  一下pod中 metrics-server-f68c598fc-vt4pz 为 metrics-server 已正常启动
[root@k8s-master ~]# kubectl get pod -n kube-system
NAME                                       READY   STATUS    RESTARTS   AGE
calico-kube-controllers-59697b644f-bqhsg   1/1     Running   0          10d
calico-node-6x9rq                          1/1     Running   0          10d
calico-node-9npwl                          1/1     Running   0          10d
calico-node-s9g7k                          1/1     Running   0          10d
coredns-c676cc86f-n4nj8                    1/1     Running   0          10d
coredns-c676cc86f-rhvwg                    1/1     Running   0          10d
etcd-k8s-master                            1/1     Running   0          10d
kube-apiserver-k8s-master                  1/1     Running   0          10d
kube-controller-manager-k8s-master         1/1     Running   0          10d
kube-proxy-62sbc                           1/1     Running   0          10d
kube-proxy-qq58v                           1/1     Running   0          10d
kube-proxy-w6p29                           1/1     Running   0          10d
kube-scheduler-k8s-master                  1/1     Running   0          10d
metrics-server-f68c598fc-vt4pz             1/1     Running   0          61s
[root@k8s-master ~]#
[root@k8s-master ~]#
# 查看每个节点资源使用情况
[root@k8s-master ~]# kubectl top nodes
NAME         CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
k8s-master   410m         5%     2845Mi          18%
k8s-node1    98m          2%     4511Mi          59%
k8s-node2    990m         12%    21520Mi         33%
[root@k8s-master ~]#
[root@k8s-master ~]#
# 查看pod 资源使用情况
[root@k8s-master ~]# kubectl top pods metrics-server-f68c598fc-vt4pz -n kube-system
NAME                             CPU(cores)   MEMORY(bytes)
metrics-server-f68c598fc-vt4pz   3m           16Mi

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/144671.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!