kubernetes 1.14安装部署metrics-server插件

本文介绍Kubernetes自动扩容功能所需插件,1.13版本后官方推荐用metrics-server替代heapster。还给出测试环境,详细说明了部署操作步骤,包括下载yaml文件、修改metrics-server - deployment.yaml文件、应用配置文件,最后检查其状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单介绍

如果使用kubernetes的自动扩容功能的话,那首先得有一个插件,然后该插件将收集到的信息(cpu、memory..)与自动扩容的设置的值进行比对,自动调整pod数量。关于该插件,在kubernetes的早些版本中采用的是heapster,1.13版本正式发布后,丢弃了heapster,官方推荐采用metrics-sever。

测试环境

部署的集群环境,如下:

SystemHostnameIP
CentOS 7.6k8s-master192.168.71.134
CentOS 7.6k8s-node1192.168.71.135
CentOS 7.6k8s-node2138.138.82.136

 

 

 

 

 

操作步骤

1. 下载相关yaml文件

https://github.com/kubernetes-incubator/metrics-server

~]# git clone https://github.com/kubernetes-incubator/metrics-server.git

复制代码

复制代码

[root@k8s-master ~]# cd metrics-server/deploy/1.8+/
[root@k8s-master 1.8+]# ll
总用量 28
-rw-r--r-- 1 root root 384 4月  28 09:46 aggregated-metrics-reader.yaml
-rw-r--r-- 1 root root 308 4月  28 09:46 auth-delegator.yaml
-rw-r--r-- 1 root root 329 4月  28 09:46 auth-reader.yaml
-rw-r--r-- 1 root root 298 4月  28 09:46 metrics-apiservice.yaml
-rw-r--r-- 1 root root 815 4月  28 09:46 metrics-server-deployment.yaml
-rw-r--r-- 1 root root 291 4月  28 09:46 metrics-server-service.yaml
-rw-r--r-- 1 root root 502 4月  28 09:46 resource-reader.yaml

复制代码

复制代码

 

2. 修改其中的metrics-server-deployment.yaml文件(用红色标亮处)

复制代码

复制代码

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: metrics-server
  namespace: kube-system
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: metrics-server
  namespace: kube-system
  labels:
    k8s-app: metrics-server
spec:
  selector:
    matchLabels:
      k8s-app: metrics-server
  template:
    metadata:
      name: metrics-server
      labels:
        k8s-app: metrics-server
    spec:
      serviceAccountName: metrics-server
      volumes:
      # mount in tmp so we can safely use from-scratch images and/or read-only containers
      - name: tmp-dir
        emptyDir: {}
      containers:
      - name: metrics-server
        image: mirrorgooglecontainers/metrics-server-amd64:v0.3.2
      # image: k8s.gcr.io/metrics-server-amd64:v0.3.2
        imagePullPolicy: IfNotPresent
        command:
            - /metrics-server
            - --kubelet-preferred-address-types=InternalIP
            - --kubelet-insecure-tls
        volumeMounts:
        - name: tmp-dir
          mountPath: /tmp

复制代码

复制代码

注意:以上修改了3个地方:①是被墙的镜像;②镜像拉取策略;③添加命令和相关参数;

如果不修改command区域的参数则会报如下错误:

复制代码

复制代码

E0428 19:35:05.894325       1 manager.go:111] unable to fully collect metrics: [unable to fully scrape metrics from source kubelet_summary:k8s-node2: unable to fetch metrics from Kubelet k8s-node2 (138.138.82.16): Get https://138.138.82.16:10250/stats/summary/: x509: cannot validate certificate for 138.138.82.16 because it doesn't contain any IP SANs, unable to fully scrape metrics from source kubelet_summary:k8s-node1: unable to fetch metrics from Kubelet k8s-node1 (138.138.82.15): Get https://138.138.82.15:10250/stats/summary/: x509: cannot validate certificate for 138.138.82.15 because it doesn't contain any IP SANs, unable to fully scrape metrics from source kubelet_summary:k8s-master: unable to fetch metrics from Kubelet k8s-master (138.138.82.14): Get https://138.138.82.14:10250/stats/summary/: x509: cannot validate certificate for 138.138.82.14 because it doesn't contain any IP SANs]

复制代码

复制代码

附上metrcs-server命令参数详解:

复制代码

复制代码

Usage:
   [flags]

Flags:
      --alsologtostderr                                         log to standard error as well as files
      --authentication-kubeconfig string                        kubeconfig file pointing at the 'core' kubernetes server with enough rights to create tokenaccessreviews.authentication.k8s.io.
      --authentication-skip-lookup                              If false, the authentication-kubeconfig will be used to lookup missing authentication configuration from the cluster.
      --authentication-token-webhook-cache-ttl duration         The duration to cache responses from the webhook token authenticator. (default 10s)
      --authorization-kubeconfig string                         kubeconfig file pointing at the 'core' kubernetes server with enough rights to create  subjectaccessreviews.authorization.k8s.io.
      --authorization-webhook-cache-authorized-ttl duration     The duration to cache 'authorized' responses from the webhook authorizer. (default 10s)
      --authorization-webhook-cache-unauthorized-ttl duration   The duration to cache 'unauthorized' responses from the webhook authorizer. (default 10s)
      --bind-address ip                                         The IP address on which to listen for the --secure-port port. The associated interface(s) must be reachable by the rest of the cluster, and by CLI/web clients. If blank, all interfaces will be used (0.0.0.0 for all IPv4 interfaces and :: for all IPv6 interfaces). (default 0.0.0.0)
      --cert-dir string                                         The directory where the TLS certs are located. If --tls-cert-file and --tls-private-key-file are provided, this flag will be ignored. (default "apiserver.local.config/certificates")
      --client-ca-file string                                   If set, any request presenting a client certificate signed by one of the authorities in the client-ca-file is authenticated with an identity corresponding to the CommonName of the client certificate.
      --contention-profiling                                    Enable lock contention profiling, if profiling is enabled
      --enable-swagger-ui                                       Enables swagger ui on the apiserver at /swagger-ui
  -h, --help                                                    help for this command
      --http2-max-streams-per-connection int                    The limit that the server gives to clients for the maximum number of streams in an HTTP/2 connection. Zero means to use golang's default.
      --kubeconfig string                                       The path to the kubeconfig used to connect to the Kubernetes API server and the Kubelets (defaults to in-cluster config)
      --kubelet-certificate-authority string                    Path to the CA to use to validate the Kubelet's serving certificates.
      --kubelet-insecure-tls                                    Do not verify CA of serving certificates presented by Kubelets.  For testing purposes only.
      --kubelet-port int                                        The port to use to connect to Kubelets. (default 10250)
      --kubelet-preferred-address-types strings                 The priority of node address types to use when determining which address to use to connect to a particular node (default [Hostname,InternalDNS,InternalIP,ExternalDNS,ExternalIP])
      --log-flush-frequency duration                            Maximum number of seconds between log flushes (default 5s)
      --log_backtrace_at traceLocation                          when logging hits line file:N, emit a stack trace (default :0)
      --log_dir string                                          If non-empty, write log files in this directory
      --logtostderr                                             log to standard error instead of files (default true)
      --metric-resolution duration                              The resolution at which metrics-server will retain metrics. (default 1m0s)
      --profiling                                               Enable profiling via web interface host:port/debug/pprof/ (default true)
      --requestheader-allowed-names strings                     List of client certificate common names to allow to provide usernames in headers specified by --requestheader-username-headers. If empty, any client certificate validated by the authorities in --requestheader-client-ca-file is allowed.
      --requestheader-client-ca-file string                     Root certificate bundle to use to verify client certificates on incoming requests before trusting usernames in headers specified by --requestheader-username-headers. WARNING: generally do not depend on authorization being already done for incoming requests.
      --requestheader-extra-headers-prefix strings              List of request header prefixes to inspect. X-Remote-Extra- is suggested. (default [x-remote-extra-])
      --requestheader-group-headers strings                     List of request headers to inspect for groups. X-Remote-Group is suggested. (default [x-remote-group])
      --requestheader-username-headers strings                  List of request headers to inspect for usernames. X-Remote-User is common. (default [x-remote-user])
      --secure-port int                                         The port on which to serve HTTPS with authentication and authorization. If 0, don't serve HTTPS at all. (default 443)
      --stderrthreshold severity                                logs at or above this threshold go to stderr (default 2)
      --tls-cert-file string                                    File containing the default x509 Certificate for HTTPS. (CA cert, if any, concatenated after server cert). If HTTPS serving is enabled, and --tls-cert-file and --tls-private-key-file are not provided, a self-signed certificate and key are generated for the public address and saved to the directory specified by --cert-dir.
      --tls-cipher-suites strings                               Comma-separated list of cipher suites for the server. If omitted, the default Go cipher suites will be use.  Possible values: TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_RSA_WITH_RC4_128_SHA,TLS_RSA_WITH_3DES_EDE_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_GCM_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA,TLS_RSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_RC4_128_SHA
      --tls-min-version string                                  Minimum TLS version supported. Possible values: VersionTLS10, VersionTLS11, VersionTLS12
      --tls-private-key-file string                             File containing the default x509 private key matching --tls-cert-file.
      --tls-sni-cert-key namedCertKey                           A pair of x509 certificate and private key file paths, optionally suffixed with a list of domain patterns which are fully qualified domain names, possibly with prefixed wildcard segments. If no domain patterns are provided, the names of the certificate are extracted. Non-wildcard matches trump over wildcard matches, explicit domain patterns trump over extracted names. For multiple key/certificate pairs, use the --tls-sni-cert-key multiple times. Examples: "example.crt,example.key" or "foo.crt,foo.key:*.foo.com,foo.com". (default [])
  -v, --v Level                                                 log level for V logs
      --vmodule moduleSpec                                      comma-separated list of pattern=N settings for file-filtered logging

复制代码

复制代码

 

3. 应用所有配置文件到系统中

复制代码

复制代码

[root@k8s-master 1.8+]# kubectl apply -f .
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
serviceaccount/metrics-server created
deployment.extensions/metrics-server created
service/metrics-server created
clusterrole.rbac.authorization.k8s.io/system:metrics-server created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created

复制代码

复制代码

过个一两分钟(下载镜像和获取数据都会耗时)检查metrics-server的状态

复制代码

复制代码

[root@k8s-master 1.8+]# kubectl get po -n kube-system
NAME                                 READY   STATUS    RESTARTS   AGE
calico-node-b78m4                    1/1     Running   0          176m
calico-node-r5mlj                    1/1     Running   0          3h6m
calico-node-z5tdh                    1/1     Running   0          176m
coredns-fb8b8dccf-6mgks              1/1     Running   0          3h21m
coredns-fb8b8dccf-cbtlx              1/1     Running   0          3h21m
etcd-k8s-master                      1/1     Running   0          3h20m
kube-apiserver-k8s-master            1/1     Running   0          3h20m
kube-controller-manager-k8s-master   1/1     Running   0          3h20m
kube-proxy-c9xd2                     1/1     Running   0          3h21m
kube-proxy-fp2r2                     1/1     Running   0          176m
kube-proxy-lrsw7                     1/1     Running   0          176m
kube-scheduler-k8s-master            1/1     Running   0          3h20m
metrics-server-7579f696d8-pgcc4      1/1     Running   0          99s
[root@k8s-master 1.8+]# kubectl top node
NAME         CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
k8s-master   179m         8%     1660Mi          43%       
k8s-node1    81m          4%     908Mi           23%       
k8s-node2    78m          3%     1036Mi          26%   

复制代码

复制代码

看的出来,metrics-server已经正常running,并且能够获取节点的信息。

<think>好的,我现在需要帮助用户了解如何在Kubernetes部署和配置metrics-server。根据用户提供的引用内容,我需要先整理相关信息,并确保回答符合系统级指令中的要求,比如使用正确的中文回答、生成相关问题,以及正确引用参考文献。 首先,用户提到了几个引用,其中引用[1]提到了部署metrics-server的命令:kubectl create -f metrics-server/deploy/1.8+/。这可能涉及到直接从官方仓库部署。引用[2]详细说明了Metrics Server的作用,包括它是核心监控数据的聚合器,默认不部署,需要开启API Aggregator,并且依赖kubelet的Summary API。此外,安装前需要确保kube-apiserver的API Aggregator已启用,这点很重要,否则metrics-server可能无法正常工作。 引用[5]提到在部署metrics-server之前需要安装网络插件和存储插件,这可能是因为某些环境需要这些组件才能正常运行metrics-server。不过用户的问题主要集中在metrics-server部署和配置上,所以可能需要重点讲解如何解决常见问题,比如证书验证或网络问题。 接下来,我需要考虑用户可能的安装步骤。根据引用[1],直接使用官方提供的YAML文件部署是最简单的方法,但可能需要修改其中的参数,比如添加--kubelet-insecure-tls来跳过证书验证,尤其是在测试环境中。引用[2]提到需要开启API Aggregator,这可能涉及到修改kube-apiserver的配置,添加相关参数,但如果是通过kubeadm安装的集群,可能已经默认开启,用户需要确认这一点。 然后是关于配置的方面,比如如何调整metrics-server的采集间隔,资源限制,或者配置TLS。引用[4]中提到了创建ServiceAccount和Secret,这可能与权限配置相关,但metrics-server可能需要特定的RBAC权限,所以需要确保部署文件包含正确的ClusterRole和ClusterRoleBinding。 监控部分,部署完成后,用户可能需要使用kubectl top命令来查看资源使用情况,或者通过Kubernetes Dashboard集成,如引用[2]中提到的Dashboard依赖Metrics Server的数据。 常见问题方面,用户可能会遇到metrics-server无法启动,或者无法获取指标的问题。这时候需要检查日志,查看是否有证书错误,或者网络策略是否阻止了metrics-server与kubelet的通信。引用[3]提到集群版本是v1.20.15,可能需要确认metrics-server的版本是否兼容该Kubernetes版本。 最后,生成相关问题时,需要考虑用户可能进一步想了解的内容,比如如何集成到监控系统,如何进行高级配置,或者如何验证metrics-server是否正常工作。 总结回答结构:安装部署步骤、关键配置说明、监控方法、常见问题处理,然后生成相关问题。同时,确保所有引用正确标注,比如开启API Aggregator的部分来自引用[2],部署命令来自引用[1]等。需要注意使用正确的LaTeX格式,比如行内公式用$...$,独立公式用$$...$$,但在这个回答中可能数学公式较少,主要关注代码块和配置示例。</think>### Metrics-Server部署与配置 #### 1. **安装部署** Metrics-ServerKubernetes 集群的核心监控组件,用于聚合资源使用指标(如 CPU、内存)。以下是部署步骤: **步骤 1:启用 API Aggregator** 确保 `kube-apiserver` 已启用 API Aggregator(默认情况下,kubeadm 部署的集群可能已开启)[^2]。检查参数是否包含: ```bash --enable-aggregator-routing=true ``` **步骤 2:部署 Metrics-Server** 使用官方提供的 YAML 文件部署(需适配 Kubernetes 版本): ```bash kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml ``` 若需跳过证书验证(测试环境),在部署文件中添加以下参数到容器命令: ```yaml command: - --kubelet-insecure-tls ``` #### 2. **关键配置说明** - **资源限制**:调整 Metrics-Server 的资源请求和限制,防止资源不足: ```yaml resources: requests: cpu: 100m memory: 200Mi ``` - **采集间隔**:默认采集间隔为 60 秒,可通过 `--metric-resolution` 参数调整。 - **网络策略**:确保 Metrics-Server 可以访问各节点的 `kubelet`(默认端口 10250)。 #### 3. **监控验证** 部署完成后,验证功能是否正常: ```bash kubectl top node # 查看节点资源使用 kubectl top pod -n kube-system # 查看 Pod 资源使用 ``` 若输出指标数据,则说明部署成功。 #### 4. **常见问题处理** - **指标无法获取**:检查 Metrics-Server 日志: ```bash kubectl logs -n kube-system metrics-server-xxxxxx ``` 常见错误包括证书不匹配或网络不通,可通过 `--kubelet-insecure-tls` 或调整网络策略解决。 - **版本兼容性**:确保 Metrics-Server 版本与 Kubernetes 兼容(例如 v1.20.x 集群建议使用 Metrics-Server v0.5+)[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值