引用链接:https://mp.weixin.qq.com/s/mRwvZBVfWPYvE7hERbGMTA
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
https://github.com/kubernetes-sigs/custom-metrics-apiserver
https://kubernetes.io/zh/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
在1.18版本,hpa进行了功能的增强,增加了扩缩容灵敏度的控制,增加了一个字段behavior。
咱们来看下定义:
behavior
behavior configures the scaling behavior of the target in both Up and Down
directions (scaleUp and scaleDown fields respectively). If not set, the
default HPAScalingRules for scale up and scale down are used.
使用的例子:
behavior:
scaleDown:
policies:
- type: Pods
value: 4
periodSeconds: 60
- type: Percent
value: 10
periodSeconds: 60
periodSeconds表示该策略必须满足的过去时间长度。第一个策略(Pods)允许在一分钟内最多缩减4个副本。
第二个策略(百分比)允许一分钟内最多缩减10%的当前副本。 由于默认情况下已选择允许最大更改量的策略,因此仅当Pod副本数大于40时才使用第二个策略。
如果副本数为40个或更少,则将应用第一个策略。例如,如果有80个副本,并且目标必须缩小到10个副本,则在第一步期间将减少8个副本。在下一个迭代中,当副本数为72时,有10%的Pod是7.2,但是四舍五入为8。
在自动定标器控制器的每个循环上,将根据数量重新计算要更改的Pod的数量。当前副本。当副本数降到40以下时,将应用第一个策略(Pods),并且一次将减少4个副本。 可以通过为扩展方向指定selectPolicy字段来更改策略选择。通过将值设置为Min,可以选择允许副本数量最小变化的策略。将值设置为“禁用”将完全禁用该方向的缩放。
Kubernetes v1.20 [alpha]又引入了新的特性
type: ContainerResource
containerResource:
name: cpu
container: application
target:
type: Utilization
averageUtilization: 60
可以配置containerresource的资源
腾讯云的HPA功能:
https://mp.weixin.qq.com/s/mRwvZBVfWPYvE7hERbGMTA
腾讯云定义了很多剧哦metrics的指标可以使用,包括GPU,DISK IO等,为扩缩容提供了丰富的根据值。
此处使用腾讯云的例子介绍一下新字段,behavior
以下的配置文件表示扩容,按照当前的pod数量的9倍扩容实例数,即总副本数为当前的10倍,范围仍在min和max的区间,此处没有配置时间,表示使用默认触发,会根据controller-manager中配置的5分钟进行观察,此功能的引进还是很有用的。毕竟随着应用的不同,所需要的反应时间也是不同的。
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: web
spec:
minReplicas: 1
maxReplicas: 1000
metrics:
- pods:
metric:
name: custom_metrics
target:
averageValue: "80"
type: AverageValue
type: Pods
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
behavior: # 这里是重点
scaleUp:
policies:
- type: percent
value: 900%
腾讯云提供的功能非常完善了,对于自建集群使用keda更够更快速的实现对应的功能。
Keda文档介绍的很详细,
比如:
Pollinginterval:
检查每个触发器的时间间隔。默认情况下,KEDA将每30秒检查每个ScaledObject上的每个触发源.
Cooldownperiod:
上一次触发后等待的时间段报告为活动,然后再将资源缩放回零。默认情况下,该时间为5分钟(300秒)。
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: {scaled-object-name}
spec:
scaleTargetRef:
apiVersion: {api-version-of-target-resource} # Optional. Default: apps/v1
kind: {kind-of-target-resource} # Optional. Default: Deployment
name: {name-of-target-resource} # Mandatory. Must be in the same namespace as the ScaledObject
envSourceContainerName: {container-name} # Optional. Default: .spec.template.spec.containers[0]
pollingInterval: 30 # Optional. Default: 30 seconds
cooldownPeriod: 300 # Optional. Default: 300 seconds
minReplicaCount: 0 # Optional. Default: 0
maxReplicaCount: 100 # Optional. Default: 100
advanced: # Optional. Section to specify advanced options
restoreToOriginalReplicaCount: true/false # Optional. Default: false
horizontalPodAutoscalerConfig: # Optional. Section to specify HPA related options
behavior: # Optional. Use to modify HPA's scaling behavior
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 100
periodSeconds: 15
triggers:
并支持多种触发器:
以上是创建了一个cpu的触发器,scaledobject,进行扩容
还支持了许多metrics,比如prometheus,influxdb等
rancher也推送了公众号,大体上是和keda的逻辑相同的。
下一步是查看如何实践keda,(代码解读)