本文已参与好文召集令活动,点击查看:后端、大前端双赛道投稿,2万元奖池等你挑战
1 VPA 简介
VPA 全称 Vertical Pod Autoscaler,即垂直 Pod 自动扩缩容,它根据容器资源使用率自动设置 CPU 和 内存 的requests,从而允许在节点上进行适当的调度,以便为每个 Pod 提供适当的资源。
它既可以缩小过度请求资源的容器,也可以根据其使用情况随时提升资源不足的容量。
PS: VPA不会改变Pod的资源limits值。
废话不多说,直接上图,看VPA工作流程
2 部署metrics-server
2.1 下载部署清单文件
1 | bash复制代码[root@VM-10-48-centos ~]# wget https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.7/components.yaml |
2.2 修改components.yaml文件
- 修改了镜像地址为:
scofield/metrics-server:v0.3.7
- 修改了metrics-server启动参数args
1 | yaml复制代码- name: metrics-server |
2.3 执行部署
1 | bash复制代码[root@VM-10-48-centos ~]# kubectl apply -f components.yaml |
2.4 验证
1 | bash复制代码[root@VM-10-48-centos ~]# kubectl get po -n kube-system | grep metrics-server |
3 部署vertical-pod-autoscaler
3.1 克隆autoscaler项目
1 | bash复制代码[root@VM-10-48-centos ~]# git clone https://github.com/kubernetes/autoscaler.git |
3.2 修改部署文件
1 | bash复制代码[root@VM-10-48-centos ~]# cd autoscaler/vertical-pod-autoscaler/deploy |
3.3 部署
1 | bash复制代码[root@VM-10-48-centos ~]# cd autoscaler/vertical-pod-autoscaler |
这里如果出现错误:ERROR: Failed to create CA certificate for self-signing. If the error is “unknown option -addext”, update your openssl version or deploy VPA from the vpa-release-0.8 branch
需要升级openssl
的版本解决
1 | bash复制代码[root@VM-10-48-centos ~]# yum install gcc gcc-c++ -y |
再次执行vertical-pod-autoscaler/pkg/admission-controller/gencerts.sh
3.4 查看结果
可以看到metrics-server和vpa都已经正常运行了
1 | bash复制代码[root@VM-10-48-centos ~]# kubectl get po -n kube-system | grep -E "metrics-server|vpa" |
4 示例
4.1 updateMode: Off
1 首先我们部署一个nginx
服务,部署到namespace: vpa
中
1 | yaml复制代码apiVersion: apps/v1 |
看下结果,正常运行了2个pod
1 | bash复制代码[root@VM-10-48-centos ~]# kubectl get po -n vpa |
2 创建一个NodePort类型的service
1 | bash复制代码[root@VM-10-48-centos ~]# cat svc.yaml |
3 创建VPA
这里先使用updateMode: "Off"
模式,这种模式仅获取资源推荐,但不更新Pod
1 | bash复制代码[root@VM-10-48-centos ~]# cat nginx-vpa-demo.yaml |
4 查看部署结果
1 | bash复制代码[root@VM-10-48-centos ~]# kubectl get vpa -n vpa |
5 使用describe查看vpa详情,主要关注Container Recommendations
1 | bash复制代码[root@VM-10-48-centos ~]# kubectl describe vpa nginx-vpa -n vpa |
其中
1 | json复制代码Lower Bound: 下限值 |
6 现在对nginx
进行压测
执行压测命令
1 | text复制代码[root@VM-10-48-centos ~]# ab -c 100 -n 10000000 http://10.1.2.16:30895/ |
7 几分钟后再观察VPA Recommendation变化
1 | bash复制代码[root@VM-10-48-centos ~]# kubectl describe vpa -n vpa nginx-vpa | tail -n 20 |
从输出信息可以看出,VPA对Pod给出了推荐值:Cpu: 1643m
,因为我们这里设置了updateMode: "Off"
,所以不会更新Pod
4.2 updateMode: Auto
1 把updateMode: “Auto”,看看VPA会有什么动作
这里把resources改为:memory: 50Mi,cpu: 100m
1 | bash复制代码[root@VM-10-48-centos ~]# kubectl get po -n vpa |
2 再次部署vpa,这里VPA部署文件nginx-vpa-demo.yaml
只改了updateMode: "Auto"
1 | text复制代码[root@k8s-node001 examples]# cat nginx-vpa-demo.yaml |
3 再次压测
1 | bash复制代码[root@VM-10-48-centos ~]# ab -c 100 -n 10000000 http://10.1.2.16:30895/ |
4 几分钟后,使用describe
查看vpa详情,同样只关注Container Recommendations
1 | bash复制代码[root@VM-10-48-centos ~]# kubectl describe vpa nginx-vpa -n vpa | tail -n 20 |
Target变成了Cpu:1643m ,Memory:262144k
5、来看下event事件
1 | bash复制代码[root@VM-10-48-centos ~]# kubectl get event -n vpa |
从输出信息可以了解到,vpa执行了EvictedByVPA,自动停掉了nginx,然后使用 VPA推荐的资源启动了新的nginx
,我们查看下nginx的pod可以得到确认
1 | bash复制代码[root@VM-10-48-centos ~]# kubectl describe po -n vpa nginx-5594c66dc6-d8d6h |
看重点Requests:cpu: 1643m,memory: 262144k
再回头看看部署文件
1 | yaml复制代码requests: |
现在可以知道VPA做了哪些事了吧。当然,随着服务的负载的变化,VPA的推荐之也会不断变化。当目前运行的pod的资源达不到VPA的推荐值,就会执行pod驱逐,重新部署新的足够资源的服务。
4.3 VPA使用限制
- 不能与HPA(Horizontal Pod Autoscaler )一起使用
- Pod比如使用副本控制器,例如属于Deployment或者StatefulSet
4.4 VPA有啥好处
- Pod 资源用其所需,所以集群节点使用效率高。
- Pod 会被安排到具有适当可用资源的节点上。
- 不必运行基准测试任务来确定 CPU 和内存请求的合适值。
- VPA 可以随时调整 CPU 和内存请求,无需人为操作,因此可以减少维护时间。
本文转载自: 掘金