HPA Pod自动扩缩容
一、简介
我们可以通过手工执行 kubectl scale
命令和在Dashboard
上操作实现pod
的扩缩容,Kubernetes
系统也能够根据Pod
当前负载的变化情况自动的进行扩缩容。Kubernetes
提供了一个资源对象:Horizontal Pod Autoscaling
(Pod水平自动伸缩),简称 HPA
。HPA
通过监控分析 RC
或者 Deployment
控制的所有 Pod
的负载变化情况确定是否需要调整 Pod
的副本数量,这是HPA
的基本原理。
HPA
控制器通过 Metrics Server的API获取指标数据,基于用户定义的扩缩容规则进行计算,得到目标Pod的副本数量。当目标Pod副本数量与当前副本数量不同时,HPA
控制器就向Pod副本控制器(Deployment、RC或RS)发起scale操作,调整Pod的副本数量,完成扩缩容操作。
二、部署操作
2.1 hpa-nginx.yaml
--- apiVersion: v1 kind: Service metadata: name: hpa-nginx labels: app: hpa-nginx spec: selector: app: hpa-nginx ports: - port: 80 protocol: TCP targetPort: 80 --- apiVersion: apps/v1 kind: Deployment metadata: name: hpa-nginx labels: app: hpa-nginx spec: selector: matchLabels: app: hpa-nginx revisionHistoryLimit: 10 template: metadata: name: hpa-nginx labels: app: hpa-nginx spec: containers: - name: hpa-nginx image: nginx:1.18.0 ports: - containerPort: 80 resources: limits: cpu: 0.2 memory: 100Mi requests: cpu: 0.1 memory: 10Mi # resources 字段需要添加,否则hpa无法获取目标pod的资源利用指标,会显示unknow
# kubectl get pods -l app=hpa-nginx NAME READY STATUS RESTARTS AGE hpa-nginx-5dc8fcf8c8-wl9jr 1/1 Running 0 10m # kubectl get svc -l app=hpa-nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hpa-nginx ClusterIP 10.100.92.206 <none> 80/TCP 11m
2.2 创建一个HPA
# kubectl autoscale deployment hpa-nginx --cpu-percent=8 --min=1 --max=10 # kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE hpa-nginx Deployment/hpa-nginx 0%/10% 1 10 1 13h
此命令创建一个关联资源hpa-nginx 的 HPA
,最小的Pod副本数量为1,最大为10。HPA
会根据设定的cpu使用率(8%)动态的增加或减少Pod的数量。
2.3 测试HPA自动扩缩容
hpa-verify.yaml
apiVersion: v1 kind: Pod metadata: name: hpa-busybox labels: app: hpa-busybox spec: containers: - name: hpa-busybox image: registry.cn-hangzhou.aliyuncs.com/google_containers/busybox imagePullPolicy: IfNotPresent command: - sleep - "600"
kubectl exec -it hpa-busybox-d9c949d7-mpplg -- sh / # while true; do wget -q -O- http://hpa-nginx;done
查看Pod资源使用情况和hpa扩缩容情况
# hpa情况 # kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE hpa-nginx Deployment/hpa-nginx 86%/10% 1 10 9 14h # Pod资源情况 # kubectl top pod hpa-nginx-57fcdcdc46-ww5s6 NAME CPU(cores) MEMORY(bytes) hpa-nginx-57fcdcdc46-ww5s6 86m 1Mi # hpa扩缩容情况 # kubectl get pod -l app=hpa-nginx NAME READY STATUS RESTARTS AGE hpa-nginx-57fcdcdc46-5frjt 1/1 Running 0 43s hpa-nginx-57fcdcdc46-856lm 1/1 Running 0 28s hpa-nginx-57fcdcdc46-9fm5x 1/1 Running 0 28s hpa-nginx-57fcdcdc46-bq48j 1/1 Running 0 43s hpa-nginx-57fcdcdc46-gx48s 1/1 Running 0 28s hpa-nginx-57fcdcdc46-h7kv4 1/1 Running 0 28s hpa-nginx-57fcdcdc46-pthrz 1/1 Running 0 13s hpa-nginx-57fcdcdc46-rwz7k 1/1 Running 0 43s hpa-nginx-57fcdcdc46-ww5s6 1/1 Running 0 14h
将测试访问停掉之后,过段时间就会自动缩容
# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE hpa-nginx Deployment/hpa-nginx 0%/10% 1 10 1 14h
定义申明式yaml文件来创建hpa
apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata: name: hpa-nginx spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: hpa-nginx minReplicas: 1 maxReplicas: 10 targetCPUUtilizationPercentage: 50