首页 > 其他分享 > k8s 部署 metrics-server

k8s 部署 metrics-server

时间:2023-02-28 11:33:50浏览次数:46  
标签:kubectl server metrics io apply k8s

k8s 提供了 top 命令可用于统计资源使用情况,它包含有 node 和 pod 两个⼦命令,分别显⽰ node 节点和 Pod 对象的资源使⽤信息。

kubectl top 命令依赖于 metrics 接口。k8s 系统默认未安装该接口,需要单独部署:

[root@k8s-master k8s-install]# kubectl top pod
error: Metrics API not available

安装过程

一、下载部署文件

下载 metrics 接口的部署文件components.yaml

​https://github.com/kubernetes-sigs/metrics-server​

 wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

二、修改配置

修改镜像地址

将部署文件中镜像地址修改为国内的地址。大概在部署文件的第 140 行。

原配置是:

image: k8s.gcr.io/metrics-server/metrics-server:v0.6.1

修改后的配置是:

image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.1

可使用如下命令实现修改:

sed -i 's/k8s.gcr.io\/metrics-server/registry.cn-hangzhou.aliyuncs.com\/google_containers/g' components.yaml

禁用证书

kubelet 证书需要由集群证书颁发机构签名(或者通过向 Metrics Server 传递参数 --kubelet-insecure-tls 来禁用证书验证)。

由于是测试环境,我们选择使用参数​​禁用证书验证,生产环境不推荐这样做!!!​

在大概 139 行的位置追加参数:

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

apply 部署文件:

[root@k8s-master k8s-install]# kubectl apply -f components.yaml
Warning: resource serviceaccounts/metrics-server is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
serviceaccount/metrics-server configured
Warning: resource clusterroles/system:aggregated-metrics-reader is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader configured
Warning: resource clusterroles/system:metrics-server is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
clusterrole.rbac.authorization.k8s.io/system:metrics-server configured
Warning: resource rolebindings/metrics-server-auth-reader is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader configured
Warning: resource clusterrolebindings/metrics-server:system:auth-delegator is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator configured
Warning: resource clusterrolebindings/system:metrics-server is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server configured
Warning: resource services/metrics-server is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
service/metrics-server configured
Warning: resource deployments/metrics-server is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
deployment.apps/metrics-server configured
Warning: resource apiservices/v1beta1.metrics.k8s.io is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io configured

metrics pod 已经正常运行:

[root@k8s-master k8s-install]# kubectl get pod -A | grep metrics
kube-system metrics-server-fd9598766-8zphn 1/1 Running 0

三、部署 metrics 接口

[root@k8s-master k8s-install]# kubectl create -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

查看该 metric pod 的运行情况:

[root@k8s-master k8s-install]# kubectl get pods --all-namespaces | grep metrics
kube-system metrics-server-6ffc8966f5-84hbb 0/1 Running 0 2m23s

四、验证

kubectl top 命令成功:

[root@k8s-master k8s-install]# kubectl top pod
NAME CPU(cores) MEMORY(bytes)
front-end-59bc6df748-699vb 0m 3Mi
front-end-59bc6df748-r7pkr 0m 3Mi
kucc4 1m 2Mi
legacy-app 1m 1Mi
my-demo-nginx-998bbf8f5-9t9pw 0m 0Mi
my-demo-nginx-998bbf8f5-lfgvw 0m 0Mi
my-demo-nginx-998bbf8f5-nfn7r 1m 0Mi
nginx-kusc00401 0m 3Mi
[root@k8s-master k8s-install]#
[root@k8s-master k8s-install]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
k8s-master 232m 5% 1708Mi 46%
k8s-slave1 29m 1% 594Mi 34%
k8s-slave2 25m 1% 556Mi 32%

五、报错解决

​kubectl top nodes​​ 报错 ​​Error from server (ServiceUnavailable): the server is currently unable to handle the request (get nodes.metrics.k8s.io)​

1、添加hostNetwork=true

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:
hostNetwork: true # 添加这个

2、重启kubeapi pod和 kubelet

kubectl delete pod  kube-apiserver-k8s-master -n kube-system
systemctl restart kubelet



标签:kubectl,server,metrics,io,apply,k8s
From: https://blog.51cto.com/landandan/6090628

相关文章

  • K8S 1.20 弃用 Docker 评估之 Docker CLI 的替代产品
    背景2020年12月初,Kubernetes在其最新的Changelog中宣布,自Kubernetes1.20之后将弃用Docker作为容器运行时。弃用Docker带来的,可能是一系列的改变,包括不限于:......
  • k8s中的PV和PVC
    前言容器磁盘上的文件的生命周期是短暂的,这就使得在容器中运行重要应用时会出现一些问题。首先,当容器崩溃时,kubelet会重启它,但是容器中的文件将丢失——容器以干净的状态......
  • K8s:渐进式入门服务网格 Istio (一)
    写在前面分享一些Istio的学习笔记博文内容涉及:istio下载安装一个Demo运行什么是istio,服务网格等概念介绍istio架构组成,应用场景等理解不足小伙伴帮忙指......
  • K8s学习(二)Kubernetest的资源管理及五大资源介绍
    前言本文是k8s学习系列文章,前后串起来是一个完整的课程(学习笔记),本文记录k8s的资源管理方式及五大类资源的具体管理命令,看完本文基本上就能实现k8s的基本操作,可以独立部署小......
  • k8sdeploy配置文件示例
    apiVersion:extensions/v1beta1kind:Deploymentmetadata:name:[k8s服务名]namespace:defaultlabels:k8s-app:[k8s服务名]spec:replicas:1te......
  • k8s service clusterip
    创建PODkubectlcreatedeploymentpod-clusterip--image=nginx--replicas=2deployment.apps/pod-clusteripcreated 创建Servicekubectlcreateserviceclust......
  • K8SPod进阶资源限制以及探针 (云原生)
    一、Pod进阶1、资源限制当定义Pod时可以选择性地为每个容器设定所需要的资源数量。最常见的可设定资源是CPU和内存大小,以及其他类型的资源。当为Pod中的容器指......
  • K8S集群+负载均衡层+防火墙 实例
    实验拓扑图:实验要求:(1)Kubernetes区域可采用Kubeadm方式进行安装。(2)要求在Kubernetes环境中,通过yaml文件的方式,创建2个NginxPod分别放置在两个不同的节点上,Pod使用......
  • k8s 单master
    准备3台Centos8服务器192.168.31.81master192.168.31.214node1192.168.31.206node2分别修改hostnamehostnamectlset-hostnamemaster.k8s.localh......
  • SQL SERVER 生僻字查询问题和关键字COLLATE
       先说问题,生僻字查询的问题,有的时候我们的数据里包含一些生僻字,在查询用Like模糊匹配的时候,发现有的查询不准确,测试数据如下:1--测试数据2ifnotobject_id(N'......