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自动伸缩,测试完毕;

相关推荐
Sapphire~3 小时前
Linux-07 ubuntu 的 chrome 启动不了
linux·chrome·ubuntu
伤不起bb3 小时前
NoSQL 之 Redis 配置与优化
linux·运维·数据库·redis·nosql
广东数字化转型3 小时前
nginx怎么使用nginx-rtmp-module模块实现直播间功能
linux·运维·nginx
啵啵学习3 小时前
Linux 里 su 和 sudo 命令这两个有什么不一样?
linux·运维·服务器·单片机·ubuntu·centos·嵌入式
半桔4 小时前
【Linux手册】冯诺依曼体系结构
linux·缓存·职场和发展·系统架构
网硕互联的小客服4 小时前
如何利用Elastic Stack(ELK)进行安全日志分析
linux·服务器·网络·安全
云道轩5 小时前
llm-d:面向Kubernetes的高性能分布式LLM推理框架
分布式·容器·kubernetes
冰橙子id5 小时前
linux——磁盘和文件系统管理
linux·运维·服务器
无聊的小坏坏6 小时前
环境变量深度解析:从配置到内核的全链路指南
linux·bash
才鲸嵌入式6 小时前
01 Ubuntu20.04下编译QEMU8.2.4,交叉编译32位ARM程序,运行ARM程序的方法
linux·c语言·单片机·嵌入式·arm·qemu·虚拟机