有没有更简单的cron作业来仅在工作日的工作时间保持集群运行?

huangapple go评论51阅读模式
英文:

Is there a simpler cronjob for keeping a cluster up only during workday hours?

问题

Context: 我对CronJobs相对新,我需要在非工作时间(晚上7点/19:00之后和早上9点/9:00之前)以及周末(星期六和星期天,全天)期间“关闭”GKE集群。

我按照这个教程进行操作,一切都设置得非常好,最后我编辑了scheduled-autoscaler-example.yaml文件以包含以下CronJob表达式:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: scale-up
spec:
  schedule: "0 9-18 * * 1-5" #每分钟,从9:00到18:59,每个工作日
  successfulJobsHistoryLimit: 0
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: custom-metric-exporter
            image: <image...>
            command:
              - /export
              - --name=scheduled_autoscaler_example
              - --value=10
          restartPolicy: OnFailure
      backoffLimit: 1
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: scale-down
spec:
  schedule: "0 19-23,0-8 * * 1-5" #每分钟,从19:00到23:59,然后从00:00到08:59,每个工作日
  successfulJobsHistoryLimit: 0
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: custom-metric-exporter
            image: <image...>
            command:
              - /export
              - --name=scheduled_autoscaler_example
              - --value=0
          restartPolicy: OnFailure
      backoffLimit: 1
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: scale-down-wknd
spec:
  schedule: "0 0 * * 6,0" #周末
  successfulJobsHistoryLimit: 0
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: custom-metric-exporter
            image: <image...>
            command:
              - /export
              - --name=scheduled_autoscaler_example
              - --value=0
          restartPolicy: OnFailure
      backoffLimit: 1
  • 一个用于在工作时间内扩展 - 从早上9点到晚上7点;
  • 一个用于在一天结束时缩小规模 - 从晚上7点到第二天早上9点;
  • 还有一个用于在周末缩小规模。

问题: 假设这两个CronJob都能按预期工作,是否有更好的表达式或更简单的设置上下调整的时间间隔的方法?

英文:

Context: I'm relatively new to CronJobs and I have to "turn off" a GKE cluster during off hours (after 7pm/19h00 and until 9am/9h00) and during weekends (saturday and sunday, all day).

I followed this tutorial, got everything set up prety nice and lastly I edited the scheduled-autoscaler-example.yaml file to have these cronjob expressions:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: scale-up
spec:
  schedule: &quot;* 9-18 * * 1-5&quot; #every minute, 9h00 to 18h59, every week day
  successfulJobsHistoryLimit: 0
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: custom-metric-exporter
            image: &lt;image...&gt;
            command:
              - /export
              - --name=scheduled_autoscaler_example
              - --value=10
          restartPolicy: OnFailure
      backoffLimit: 1
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: scale-down
spec:
  schedule: &quot;* 19-23,0-8 * * 1-5&quot; #every minute, 19h00 to 23h59 then 00 to 8h59, every week day
  successfulJobsHistoryLimit: 0
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: custom-metric-exporter
            image: &lt;image...&gt;
            command:
              - /export
              - --name=scheduled_autoscaler_example
              - --value=0
          restartPolicy: OnFailure
      backoffLimit: 1
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: scale-down-wknd
spec:
  schedule: &quot;* * * * 6,0&quot; #weekends
  successfulJobsHistoryLimit: 0
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: custom-metric-exporter
            image: &lt;image...&gt;
            command:
              - /export
              - --name=scheduled_autoscaler_example
              - --value=0
          restartPolicy: OnFailure
      backoffLimit: 1
  • One for scaling up during worktimes - from 9am to 7pm;
  • One for scaling down at the end of the day - from 7pm to 9am the next morning;
  • And one for scaling down during weekends.

The question: Assuming both cronjobs are gonna work as intended, is there a better expression or a simpler way of setting up the hour intervals for both the scaling up and down?

答案1

得分: 1

你的CronJob配置清晰而简洁。对于每个计划使用单独的作业是一种良好的实践,可以避免混淆,并在出现问题时更容易进行隔离。以防你正在寻找其他简化或澄清计划表达式的方法,以下是一些考虑点:

范围与步骤:"/10"语法表示"每10分钟",因此"/30 * * *"表示作业将每30分钟运行。你可以使用类似的方法来定义具有步骤的范围。

时区:Kubernetes CronJob默认使用UTC作为时区。根据你的需求,你可能需要调整计划以考虑时区差异。

这是一个对于你的用例可能有帮助的Kubernetes CronJob指南1

1 https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/

英文:

Your CronJob configuration is clear and concise. Using separate jobs for each schedule is a good practice to avoid confusion and ease of isolation when having an issue. Just in case you're looking for other ways to simplify or clarify the schedule expressions, here are some points to consider:<br><br>Ranges with Steps: The "/10" syntax means "every 10", so "/30 * * * *" means the job will run every 30 minutes. You could use a similar approach to define ranges with steps.<br><br>Time Zone: Kubernetes CronJob uses UTC as the default timezone. Depending on your needs, you might need to adjust your schedule to take into account the time zone differences.<br><br>Here is a guide for Kubernetes CronJob that can prove helpful for your use case.[1]<br><br>[1] <https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/><br>

huangapple
  • 本文由 发表于 2023年7月13日 23:33:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定