使用 Kubernetes CronJob 进行基于时间的扩展:如何避免部署覆盖 minReplicas

Time-based scaling with Kubernetes CronJob: How to avoid deployments overriding minReplicas

我有一个 Horizo​​ntalPodAutoscalar 可以根据 CPU 缩放我的 pods。这里的minReplicas设置为5:

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: myapp-web
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: myapp-web
  minReplicas: 5 
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 50

然后我添加了 Cron 作业以根据一天中的时间扩展 up/down 我的水平 pod 自动缩放器:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: production
  name: cron-runner
rules:
- apiGroups: ["autoscaling"]
  resources: ["horizontalpodautoscalers"]
  verbs: ["patch", "get"]

---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: cron-runner
  namespace: production
subjects:
- kind: ServiceAccount
  name: sa-cron-runner
  namespace: production
roleRef:
  kind: Role
  name: cron-runner
  apiGroup: rbac.authorization.k8s.io

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: sa-cron-runner
  namespace: production
---

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: django-scale-up-job
  namespace: production
spec:
  schedule: "56 11 * * 1-6"
  successfulJobsHistoryLimit: 0 # Remove after successful completion
  failedJobsHistoryLimit: 1 # Retain failed so that we see it
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: sa-cron-runner
          containers:
          - name: django-scale-up-job
            image: bitnami/kubectl:latest
            command:
            - /bin/sh
            - -c
            - kubectl patch hpa myapp-web --patch '{"spec":{"minReplicas":8}}'
          restartPolicy: OnFailure
----
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: django-scale-down-job
  namespace: production
spec:
  schedule: "30 20 * * 1-6"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 0 # Remove after successful completion
  failedJobsHistoryLimit: 1 # Retain failed so that we see it
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: sa-cron-runner
          containers:
          - name: django-scale-down-job
            image: bitnami/kubectl:latest
            command:
            - /bin/sh
            - -c
            - kubectl patch hpa myapp-web --patch '{"spec":{"minReplicas":5}}'
          restartPolicy: OnFailure

这真的很好用,除了现在当我部署它时用 Horizo​​ntalPodAutoscaler 规范中的 minReplicas 覆盖这个 minReplicas 值(在我的例子中,它设置为 5)

我正在使用 kubectl apply -f ~/autoscale.yaml

部署我的 HPA

有没有办法处理这种情况?我是否需要创建某种共享逻辑,以便我的部署脚本可以计算出 minReplicas 的值应该是多少?或者有更简单的处理方法吗?

我想你也可以考虑以下两种方案:


使用 helm 通过查找功能管理应用程序的生命周期:

此解决方案背后的主要思想是在尝试使用 helm install/[create/recreate 之前查询特定集群资源(此处 HPA)的状态=13=] 命令。

我的意思是在每次升级应用程序堆栈之前检查当前 minReplicas 值。


HPA 资源与应用程序清单文件分开管理

在这里你可以把这个任务交给一个专门的HPA操作员,它可以与你的CronJobs共存,根据特定的时间表调整minReplicas: