1.VPA和HPA的区别
VPA 和 HPA 之间的区别在于它们的扩展方式。HPA 通过添加或删除pod进行扩展,pod内的资源请求不会发生改变,实现水平扩展容量;VPA 通过增加或减少现有 Pod 容器内的 CPU 和内存资源来进行扩展,pod的数量不会发生改变,实现垂直扩展容量。
2.VPA组件部署
VPA不像HPA那样默认来源于集群,VPA有其独立的项目,因此需要手动部署
部署VPA要求集群版本>=1.25
首先把VPA项目拉下来:
git clone https://github.com/kubernetes/autoscaler.git
切换到autoscaler/vertical-pod-autoscaler/hack,执行vpa-up.sh,如果集群中已有想要重新部署的话先执行vpa-down.sh卸载;下图为部署后状态。
如果国内环境无法拉取镜像,可以选择以下镜像:
#拉取镜像
docker pull giantswarm/vpa-admission-controller:0.14.0
docker pull giantswarm/vpa-recommender:0.14.0
docker pull giantswarm/vpa-updater:0.14.0
拉取镜像后,注意切换当git对应版本分支,修改autoscaler/vertical-pod-autoscaler/deploy/下的部署配置。
3.VPA demo测试
可以选择项目自带的demo进行测试,cd到autoscaler/vertical-pod-autoscaler/examples,部署示例:
#hamster.yaml
apiVersion: "autoscaling.k8s.io/v1"
kind: VerticalPodAutoscaler
metadata:
name: hamster-vpa
spec:
# recommenders field can be unset when using the default recommender.
# When using an alternative recommender, the alternative recommender's name
# can be specified as the following in a list.
# recommenders:
# - name: 'alternative'
targetRef:
apiVersion: "apps/v1"
kind: Deployment
name: hamster
resourcePolicy:
containerPolicies:
- containerName: '*'
minAllowed:
cpu: 100m
memory: 50Mi
maxAllowed:
cpu: 1
memory: 500Mi
controlledResources: ["cpu", "memory"]
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hamster
spec:
selector:
matchLabels:
app: hamster
replicas: 2
template:
metadata:
labels:
app: hamster
spec:
securityContext:
runAsNonRoot: true
runAsUser: 65534 # nobody
containers:
- name: hamster
image: gcmirrors/ubuntu-slim:0.1 #镜像拉不下来的用这个
resources:
requests:
cpu: 100m
memory: 50Mi
command: ["/bin/sh"]
args:
- "-c"
- "while true; do timeout 0.5s yes >/dev/null; sleep 0.5s; done"
部署成功后初始是按照yaml中设置的资源来的:
稍等一会vpa给出recommendation后pod会重建,重建后pod的资源值会变为vpa给出的推荐值: