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

相关推荐
wljy141 分钟前
二、静态库的制作和使用
linux·c语言·开发语言·c++
阿里云云原生1 小时前
阿里云可观测 2026 年 4 月产品动态
云原生
mzhan0171 小时前
Linux: coredump产生对程序退出的影响
linux·运维·服务器
光电笑映1 小时前
从环境变量到进程虚拟地址空间——Linux 内存管理的底层脉络
linux·服务器·c++·c
源远流长jerry2 小时前
Linux 网络收包机制:从网卡到 Socket 的完整路径
linux·运维·服务器·网络·网络协议·tcp/ip
qq_364371722 小时前
基于 Docker 容器化环境配置
运维·docker·容器
塔克拉玛攻城狮2 小时前
详解cni插件cilium篇一:它为什么这么快?它还有哪些高级功能?
kubernetes·cilium
阿里云云原生2 小时前
Skills Registry 公测开启:为企业打造私有的 Skill 管理中心
云原生
吃胖点儿3 小时前
DevOps与自动化原理
云原生
GentleDevin3 小时前
Docker 运维常用命令大全
docker·容器·运维命令