k8s配置HPA完成自动扩缩容
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