HPA example
Reference The
number of Pods in ReplicationController, Deployment, ReplicaSet or StatefulSet can be automatically scaled according to CPU utilization
# 运行 php-apache 服务器并暴露服务
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-apache
spec:
selector:
matchLabels:
run: php-apache
replicas: 1
template:
metadata:
labels:
run: php-apache
spec:
containers:
- name: php-apache
image: hpa-example
ports:
- containerPort: 80
resources:
limits:
cpu: 500m
requests:
cpu: 200m
---
apiVersion: v1
kind: Service
metadata:
name: php-apache
labels:
run: php-apache
spec:
ports:
- port: 80
selector:
run: php-apache
创建 Horizontal Pod Autoscaler
HPA 将(通过 Deployment)增加或者减少 Pod 副本的数量以保持所有 Pod 的平均 CPU 利用率在 50% 左右(由于每个 Pod 请求 200 毫核的 CPU,这意味着平均 CPU 用量为 100 毫核)
kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
kubectl get hpa
增加负载
启动一个容器,并通过一个循环向 php-apache 服务器发送无限的查询请求
kubectl run -i --tty load-generator --rm --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"
容器内存使用到达上限,开始扩容,Hpa会根据Pod的CPU使用率动态调节Pod的数量
停止负载
终止load-generator
1.
HPA scaling process: collect the recent CPU utilization of all Pods under HPA control (CPU utilization) and
compare the CPU Utilization recorded in the expansion conditions to
adjust the number of instances (must not exceed the maximum/minimum number of instances)
every time 30s to do an automatic expansion judgment
The calculation method of CPU utilization is to use cpu usage (the average of the last minute, which can be directly obtained through metrics) divided by cpu request (here cpu request is the core of cpu usage we made when we created the container Number) to get an average value, which can be understood as: the average percentage of CPU core usage of each Pod.
2. HPA scaling algorithm:
calculation formula: TargetNumOfPods = ceil(sum(CurrentPodsCPUUtilization) / Target)
ceil() means to take the nearest integer greater than or equal to a certain number
and cool down for 3 minutes after each expansion to expand again, and shrink You have to wait 5 minutes later.
When the current Pod Cpu usage rate is close to the target usage rate, expansion or reduction will not be triggered:
Trigger condition: avg(CurrentPodsConsumption) / Target >1.1 or <0.9
# 多项度量指标
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: php-apache
spec:
maxReplicas: 10
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
metrics:
- type: Resource
resource:
name: cpu
target:
averageUtilization: 60
type: Utilization
- type: Resource
resource:
name: memory
target:
averageValue: 50Mi
type: AverageValue