1、概念
HPA :水平自動更新(Horizontal Pod Autoscales),通過檢查pods 的cpu負載通知deployment, 讓其更新pods 數量以對抗增加的請求負載。
2、首先創建一個nginx的deployment nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: web
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx
resources:
limits:
cpu: "0.4"
requests:
cpu: "0.4"
3、應用一下配置文件
kubectl create -f nginx.yaml
4、暴露服務用來測試
kubectl expose deployment web --port=80 --target-port=80 --type=NodePort
5、設置hpa 表示最小一個pods 最多5個pods cpu負載超過80%就觸發擴容
kubectl autoscale deployment web --min=1 --max=5 --cpu-percent=80
6、ab壓測一下
ab -t 600 -n 1000000 -c 1000 http://172.16.0.5:30741/index.html
7、查看hpa
Every 1.0s: kubectl get hpa Wed Oct 20 16:40:15 2021 NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE web Deployment/web 99%/80% 1 5 1 2m9s
8、此時超過80% 查看一下pods 由一個自動變2個了 減輕請求壓力
Every 1.0s: kubectl get pods Wed Oct 20 16:40:25 2021 NAME READY STATUS RESTARTS AGE web-58c75d75f8-6pq79 1/1 Running 0 22m web-58c75d75f8-rplvw 1/1 Running 0 19s
9、ab壓測結束后,hpa的cpu使用會下降到80% 以下 大概過5分鍾(延期保證服務)之后 pods恢復為1個副本 完美!!!
Every 1.0s: kubectl get pods Wed Oct 20 16:47:01 2021 NAME READY STATUS RESTARTS AGE web-58c75d75f8-6pq79 1/1 Running 0 28m web-58c75d75f8-rplvw 0/1 Terminating 0 6m55s
