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

73 views Asked by At

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: "* 9-18 * * 1-5" #every minute, 9h00 to 18h59, every week day
  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: "* 19-23,0-8 * * 1-5" #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: <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: "* * * * 6,0" #weekends
  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
  • 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

There are 1 answers

1
Ray John Navarro On BEST ANSWER

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:

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.

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.

Here is a guide for Kubernetes CronJob that can prove helpful for your use case.[1]

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