Horizontal Pod Autoscaler(HPA)
环境
- 必须安装metrics-server或其他自定义metrics-server
- 必须配置requests参数
- 不能扩容无法缩放的对象,比如DaemonSet
接口类型
- HPA v1为稳定版自动水平伸缩,只支持CPU指标
- V2为beta版本,分为v2beta1(支持CPU、内存和自定义指标)
- v2beta2(支持CPU、内存、自定义指标Custom和额外指标ExternalMetrics)
$ kubectl get apiservices |grep autoscal
v1.autoscaling Local True 88d
v2beta1.autoscaling Local True 88d
v2beta2.autoscaling Local True
创建HPA deployment
[root@k8s-master01 ~]$ kubectl create deployment hpa-nginx --image=registry.cn-beijing.aliyuncs.com/dotbalo/nginx --dry-run=client -oyaml > hpa-nginx.yaml
[root@k8s-master01 ~]$ vim hpa-nginx.yaml
##container下设置pod资源
spec:
containers:
- image: registry.cn-beijing.aliyuncs.com/dotbalo/nginx
name: nginx
resources:
requests:
cpu: 10m
[root@k8s-master01 ~]$ kubectl create -f hpa-nginx.yaml
deployment.apps/hpa-nginx created
[root@k8s-master01 ~]$ kubectl top po
NAME CPU(cores) MEMORY(bytes)
busybox 0m 0Mi
hpa-nginx-58ddb65c8d-5cs9h 1m 6Mi
创建HPA对象
最小副本1,最大副本10,cpu超过10%自动扩容,小于10%自动缩容
[root@k8s-master01 ~]$ kubectl autoscale deployment hpa-nginx --cpu-percent=10 --min=1 --max=10
horizontalpodautoscaler.autoscaling/hpa-nginx autoscaled
[root@k8s-master01 ~]$ kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-nginx Deployment/hpa-nginx <unknown>/10% 1 10 0 6s
#等待一会设置成功TARGETS状态改变
[root@k8s-master01 ~]$ kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-nginx Deployment/hpa-nginx 0%/10% 1 10 1 8m24s
模拟触发自动扩缩容
暴露服务端口:
$ kubectl expose deployment hpa-nginx --port=80
设置循环请求:
$ while true; do wget -q -O- http:// 172.25.244.241 > /dev/null; done
#此时cpu飙升,触发扩容
[root@k8s-master01 ~]$ kubectl top po
NAME CPU(cores) MEMORY(bytes)
busybox 0m 0Mi
hpa-nginx-58ddb65c8d-5cs9h 78m 6Mi
[root@k8s-master01 ~]$ kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-nginx Deployment/hpa-nginx 79%/10% 1 10 10 15m
[root@k8s-master01 ~]$ kubectl get po
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 110 88d
hpa-nginx-58ddb65c8d-4pp59 1/1 Running 0 11s
hpa-nginx-58ddb65c8d-4znxs 0/1 ContainerCreating 0 26s
hpa-nginx-58ddb65c8d-5cs9h 1/1 Running 0 17m
hpa-nginx-58ddb65c8d-627f4 1/1 Running 0 11s
hpa-nginx-58ddb65c8d-97kbf 0/1 ContainerCreating 0 11s
hpa-nginx-58ddb65c8d-cjblq 0/1 ContainerCreating 0 11s
hpa-nginx-58ddb65c8d-ds758 1/1 Running 0 26s
hpa-nginx-58ddb65c8d-t44gh 1/1 Running 0 26s
#停止循环,自动缩容
[root@k8s-master01 ~]$ kubectl get po -l app=hpa-nginx -w
NAME READY STATUS RESTARTS AGE
hpa-nginx-58ddb65c8d-4pp59 1/1 Running 0 6m33s
hpa-nginx-58ddb65c8d-4znxs 1/1 Running 0 6m48s
hpa-nginx-58ddb65c8d-5cs9h 1/1 Running 0 23m
hpa-nginx-58ddb65c8d-627f4 1/1 Running 0 6m33s
hpa-nginx-58ddb65c8d-97kbf 1/1 Running 0 6m33s
hpa-nginx-58ddb65c8d-cjblq 1/1 Running 0 6m33s
hpa-nginx-58ddb65c8d-ds758 1/1 Running 0 6m48s
hpa-nginx-58ddb65c8d-p7dmv 1/1 Running 0 6m17s
hpa-nginx-58ddb65c8d-pkdf4 1/1 Running 0 6m17s
hpa-nginx-58ddb65c8d-t44gh 1/1 Running 0 6m48s
hpa-nginx-58ddb65c8d-97kbf 1/1 Terminating 0 9m40s
hpa-nginx-58ddb65c8d-cjblq 1/1 Terminating 0 9m40s
hpa-nginx-58ddb65c8d-ds758 1/1 Terminating 0 9m55s
hpa-nginx-58ddb65c8d-pkdf4 1/1 Terminating 0 9m24s
hpa-nginx-58ddb65c8d-4pp59 1/1 Terminating 0 9m40s
hpa-nginx-58ddb65c8d-627f4 1/1 Terminating 0 9m40s
[root@k8s-master01 ~]$ kubectl get po
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 110 88d
hpa-nginx-58ddb65c8d-5cs9h 1/1 Running 0 29m