最近更新时间:2024-09-10 16:07:39
在实际生产环境中,部分业务呈现规律性的高峰和低谷。为了减轻运维人员的工作负担,KCE提供了CronHPA功能,可定期对集群中的工作负载进行扩缩容。同时CronHPA可以和HPA策略组合使用,通过监控弹性指标以应对日常突发高峰业务场景,丰富实际生产场景中弹性伸缩的使用方式。
您已使用金山云容器服务创建一个正常运行的Kubernetes集群,关于如何创建集群,请参见创建集群。
进入集群内,在组件管理中安装CronHPA组件,并确保CronHPA资源运行正常
进入自动伸缩-CronHPA页面,为已有工作负载创建CronHPA规则;deployment-demo在每分钟第0秒将副本数调为3,在每分钟第30秒将副本数调为1
进入CronHPA规则内查看扩缩容记录
CronHPA关键字段说明,参见下方表格
字段 | 说明 | 备注 | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
apiVersion | API版本,固定值为“autoscaling.kce.ksyun.com/v1beta1” | - | ||||||||||||||||||||||||||||
kind | API类型,固定值为“CronHorizontalPodAutoscaler” | - | ||||||||||||||||||||||||||||
metadata.name | CronHPA名称 | - | ||||||||||||||||||||||||||||
metadata,namespace | CronHPA所属命名空间 | CronHPA策略只可用于同一个Namespaec下的工作负载 | ||||||||||||||||||||||||||||
spec.scaleTargetRef | 声明CronHPA策略作用的对象,需配置以下字段:
|
| ||||||||||||||||||||||||||||
spec.excludeDates | excludeDates是日期数组,当遇到符合excludeDates 描述日期时任务将会被跳过 |
| ||||||||||||||||||||||||||||
spec.jobs | CronHPA规则策略,支持在一个规格中设置多个CronHPA策略,需配置以下字段:
注:CronHPA为v1.0.0版本时,runOnce功能无法生效,请将CronHPA升级至最新版本使用 | Cron表达式格式:
|
进入集群内,在组件管理中安装CronHPA组件,并确保CronHPA资源运行正常
# 查看自定义资源是否安装
$ kubectl api-resources | grep cronhpa
cronhorizontalpodautoscalers cronhpa autoscaling.kce.ksyun.com true CronHorizontalPodAutoscaler
# 查看CronHPA controller是否安装
$ kubectl get deploy kubernetes-cronhpa-controller -n kube-system
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-cronhpa-controller 1/1 1 1 4m11s
创建一个Deployment,yaml如下:
apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
name: nginx-deployment-basic
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9 # replace it with your exactly <image_name:tags>
ports:
- containerPort: 80
创建一个CronHPA策略,yaml如下:
apiVersion: autoscaling.kce.ksyun.com/v1beta1
kind: CronHorizontalPodAutoscaler
metadata:
labels:
controller-tools.k8s.io: "1.0"
name: cronhpa-sample
spec:
scaleTargetRef:
apiVersion: apps/v1beta2
kind: Deployment
name: nginx-deployment-basic
excludeDates:
- "* * * 15 11 *" # exclude November 15th
- "* * * * * 5" # exclude every Friday
jobs:
- name: "scale-down"
schedule: "30 */1 * * * *" # 每分钟的第30秒,将deployment/nginx-deployment-basic 副本数调整为 1
targetSize: 1
- name: "scale-up"
schedule: "0 */1 * * * *" # 每分钟的第0秒,将deployment/nginx-deployment-basic 副本数调整为 3
targetSize: 3
查看cronhpa/cronhpa-sample详情
$ kubectl describe cronhpa cronhpa-sample
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Succeed 8m31s cron-horizontal-pod-autoscaler cron hpa job scale-down executed successfully. current replicas:2, desired replicas:1.
Normal Succeed 31s (x8 over 7m31s) cron-horizontal-pod-autoscaler cron hpa job scale-down executed successfully. current replicas:3, desired replicas:1.
Normal Succeed 1s (x9 over 8m1s) cron-horizontal-pod-autoscaler cron hpa job scale-up executed successfully. current replicas:1, desired replicas:3.
当CronHPA和HPA两个策略同时作用于一个Deployment上时,CronHPA不会直接调整Deployment的Pod数量,而是通过基于HPA策略基础上去调整Deployment。在KCE中将CronHPA Yaml文件中spec.scaleTargetRef设置为HPA资源,通过HPA资源中spec.scaleTargetRef的声明找到要操作的Deployment。
CronHPA根据目标实例数(targetReplicas)和replicas(Deployment当前实例数)的较大值来判断是否需要扩缩容及修改HPA的上限。还根据目标实例数(targetReplicas)和HPA配置的较小值来判断是否需要修改HPA的下限。参数说明如下:
targetReplicas:CronHPA设定的目标实例数
minReplicas:HPA中配置的Deployment的最小实例数
maxReplicas:HPA中配置的Deployment的最大实例数
replicas:Deployment实例数
以下是根据不同的场景,列举出CronHPA修改HPA的场景:
场景 | CronHPA (targetReplicas) | Deployment (replicas) | HPA (minReplicas / maxReplicas) | 最终结果 | 操作说明 |
---|---|---|---|---|---|
targetReplicas < minReplicas ≤ replicas ≤ maxReplicas | 4 | 5 | 5/10 | HPA: 4/10 Deployment: 5 | CronHPA目标实例数低于HPA最小实例数(minReplicas)时:
|
targetReplicas = minReplicas ≤ replicas ≤ maxReplicas | 5 | 6 | 5/10 | HPA: 5/10 Deployment: 6 | CronHPA目标实例数等于HPA最小实例数(minReplicas)时:
|
minReplicas < targetReplicas < replicas ≤ maxReplicas | 4 | 5 | 1/10 | HPA: 4/10 Deployment: 5 | CronHPA目标实例数大于HPA最小实例数(minReplicas),小于Deployment实例数(replicas)时:
|
minReplicas < targetReplicas = replicas < maxReplicas | 5 | 5 | 1/10 | HPA: 5/10 Deployment: 5 | CronHPA目标实例数大于HPA最小实例数(minReplicas),等于Deployment实例数(replicas)时:
|
minReplicas ≤ replicas < targetReplicas < maxReplicas | 6 | 5 | 1/10 | HPA: 6/10 Deployment: 6 | CronHPA目标实例数大于Deployment实例数(replicas),小于HPA最大实例数(maxReplicas)时:
|
minReplicas ≤ replicas < targetReplicas = maxReplicas | 10 | 5 | 1/10 | HPA: 10/10 Deployment: 10 | CronHPA目标实例数大于Deployment实例数(replicas),等于HPA最大实例数(maxReplicas)时:
|
minReplicas ≤ replicas ≤ maxReplicas < targetReplicas | 11 | 5 | 5/10 | HPA: 11/11 Deployment: 11 | CronHPA目标实例数大于HPA最大实例数(maxReplicas)时:
|
纯净模式