24-k8s的附件组件-Metrics-server组件与hpa资源pod水平伸缩

一、概述

Metrics-Server组件目的:获取集群中pod、节点等负载信息;

hpa资源目的:通过metrics-server获取的pod负载信息,自动伸缩创建pod;

参考链接:

资源指标管道 | Kubernetes

https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/metrics-server

GitHub - kubernetes-sigs/metrics-server: Scalable and efficient source of container resource metrics for Kubernetes built-in autoscaling pipelines.

二、安装部署Metrics-Server组件

就是给k8s集群安装top命令的意思;

1,下载Metrics-Server资源清单

· 第一种方式:github下载

root@k8s231 metricsserver\]# wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/high-availability-1.21+.yaml

· 第二种方式:本地上传安装包

百度云盘;

链接:https://pan.baidu.com/s/1axn44_AsbHQxIMw9nuNVMw?pwd=jtqb

提取码:jtqb

2,编辑Metrics-Server的资源清单

root@k8s231 metricsserver\]# vim high-availability-1.21+.yaml spec: affinity: podAntiAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchLabels: k8s-app: metrics-server namespaces: - kube-system topologyKey: kubernetes.io/hostname containers: - args: #启动允许使用不安全的证书 - --kubelet-insecure-tls - --cert-dir=/tmp - --secure-port=10250 - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname - --kubelet-use-node-status-port - --metric-resolution=15s #image: registry.k8s.io/metrics-server/metrics-server:v0.7.0 image: registry.aliyuncs.com/google_containers/metrics-server:v0.6.3

3,创建Metrics-Server资源

root@k8s231 metricsserver\]# kubectl apply -f high-availability-1.21+.yaml

4,验证Metrics-Server是否成功安装

· 查看pod

root@k8s231 metricsserver\]# kubectl get pods -A

· 使用top命令测试是否管用

查节点的top值

root@k8s231 metricsserver\]# kubectl top node

查看pod的top值

root@k8s231 metricsserver\]# kubectl top pods -A

三、hpa资源实现pod水平伸缩(自动扩缩容)

1,当资源使用超一定的范围,会自动扩容,但是扩容数量不会超过最大pod数量;

2,扩容时无延迟,只要监控资源使用超过阔值,则会直接创建pod;

3,当资源使用率恢复到阔值以下时,需要等待一段时间才会释放,大概时5分钟;

1,编辑deployment资源

root@k8s231 hpa\]# cat deploy.yaml apiVersion: apps/v1 kind: Deployment metadata: name: dm-hpa spec: replicas: 1 selector: matchLabels: k8s: xinjizhiwa template: metadata: labels: k8s: xinjizhiwa spec: containers: - name: c1 image: centos:7 command: - tail - -f - /etc/hosts resources: requests: cpu: "50m" limits: cpu: "150m"

2,编写hpa资源清单

root@k8s231 hpa\]# cat hpa.yaml apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata: name: hpa-tools spec: #指定pod最大的数量是多少(自动扩容的上限) maxReplicas: 10 #指定pod最小的pod数量是多少(自动缩容的下限) minReplicas: 2 #弹性伸缩引用的目标是谁? scaleTargetRef: #目标资源的api apiVersion: "apps/v1" #目标资源的类型kind kind: Deployment #目标资源的名称metadata-name是什么 name: dm-hpa #使用cpu阈值(使用到达多少,开始扩容、缩容) #95% targetCPUUtilizationPercentage: 95

3,创建hpa和deploy资源

root@k8s231 hpa\]# kubectl apply -f .

4,查看hpa资源

root@k8s231 hpa\]# kubectl get hpa -o wide

到这里,就已经实现了自动扩缩容的pod副本了;

至此,咱们的metrics-server组件和hpa资源,就学习完毕了;

四、压测测试

1,进入pod,安装stress工具

· 进入pod容器

root@k8s231 hpa\]# kubectl exec dm-hpa-5bb4dd448d-ks2rt -it -- sh

· 安装aili源和epel源

sh-4.2# yum -y install wget

sh-4.2# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

sh-4.2# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

· 安装压测工具

sh-4.2# yum -y install stress

2,开始使用命令压测pod

sh-4.2# stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 20m

3,查看hpa资源的负载情况

root@k8s231 \~\]# kubectl get hpa -o wide

可以看到:

1,我们创建的deploy资源只有一个副本;

2,我们创建的hpa资源之后,设置最小值是2,最大值是10 ;

3,我们在查看pod,可以看见,pod变成了2个;

4,我们进入容器,开始压测,将负载压测到超过95%;

5,再次查看pod,发现变成了3个,自动创建了一个;

6,关闭压测,5分钟后,pod有回归到了2个;

7,至此,hpa的pod自动伸缩,测试完毕;

相关推荐
张火火isgudi3 小时前
fedora43 安装 nvidia 驱动以及开启视频编解码硬件加速
linux·运维·视频编解码·nvidia
IT19955 小时前
Qt笔记-使用SSH2进行远程连接linux服务器并上传文件
linux·服务器·笔记
XXYBMOOO5 小时前
内核驱动开发与用户级驱动开发:深度对比与应用场景解析
linux·c++·驱动开发·嵌入式硬件·fpga开发·硬件工程
叽里咕噜怪6 小时前
docker-compose 编排ruoy实践
运维·docker·容器
lengjingzju6 小时前
一网打尽Linux IPC(三):System V IPC
linux·服务器·c语言
大聪明-PLUS6 小时前
如何编写你的第一个 Linux 内核模块
linux·嵌入式·arm·smarc
知识分享小能手6 小时前
Ubuntu入门学习教程,从入门到精通,Ubuntu 22.04文件压缩与解压缩知识点详解(12)
linux·学习·ubuntu
用户6135411460167 小时前
Krb5-libs-1.18.2-5.ky10.x86_64.rpm 安装失败怎么办?附详细步骤
linux
zhougl9968 小时前
Vuex 模块命名冲突:问题解析与完整解决方案
linux·服务器·apache
一世琉璃白_Y8 小时前
Ubuntu(VMware)虚拟机网络异常排查与解决方案
linux·网络·ubuntu