部署
使用yaml部署服务
apiVersion: apps/v1
kind: Deployment
metadata:name: php-apachenamespace: tools
spec:replicas: 1selector:matchLabels:app: php-apachetemplate:metadata:labels:app: php-apachespec:containers:- name: php-apacheimage: registry.cn-beijing.aliyuncs.com/google_registry/hpa-exampleports:- containerPort: 80resources:limits:cpu: 200m #CPU利用率赫兹最大200Mrequests: cpu: 200m
给刚刚新建的服务开启hpa设置
yaml部署hpa
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:namespace: toolsname: php-apache
spec:maxReplicas: 5minReplicas: 1scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: php-apachetargetCPUUtilizationPercentage: 10
这里hpa的含义为,当超过服务cpu的10%的时候,进行扩容,最小存在节点数为1个,最多5个
测试
起一个测试容器
apiVersion: apps/v1
kind: Deployment
metadata:name: busyboxnamespace: tools
spec:selector:matchLabels:app: busyboxreplicas: 1template:metadata:labels:app: busyboxspec:containers:- name: busyboximage: busybox:1.32imagePullPolicy: IfNotPresentargs:- /bin/sh- -c- sleep 3600imagePullSecrets:- name: default-secret
进入容器控制台,执行测试命令:
while true; do wget -q -O- 容器组ip:80; done
观察
查看hpa,发现cpu飙升后,很快达到阈值,然后就达到了hpa的最大副本数5个
都是刚起来的容器;
当停止测试脚本后,过一段时间,大概10分钟以内,容器会自动缩容至1个
踩坑点
参考:k8s——HPA的三个误区与避坑指南