CronJob
Kubernetes CronJob
A Kubernetes resource that runs a Job periodically. The schedule is defined with cron syntax.
Kubernetes CronJob
A Kubernetes resource that runs a Job periodically. The schedule is defined with cron syntax.
A CronJob is a workload resource in Kubernetes that automates recurring tasks. It defines a schedule using the same syntax as Linux crontab (minute, hour, day of month, month, day of week), creating a Job and running a Pod at the specified time.
Before CronJob was introduced, you had to rely on external schedulers (Jenkins, Airflow) or the host's crontab. Since CronJob became a stable (GA) feature in Kubernetes 1.21, scheduling and execution can be managed together inside the cluster.
A CronJob internally uses the Job controller. When the scheduled time arrives, the CronJob controller creates a new Job object, and the Job controller creates the Pod for that Job to perform the task. After the task completes, the Pod terminates, and previous Jobs are cleaned up according to the history management policy.
In practice, CronJob is used for a variety of batch tasks such as database backups, log cleanup, report generation, cache refreshes, and certificate renewals. The concurrencyPolicy and startingDeadlineSeconds settings let you finely control duplicate execution and delayed handling.
# 기본 CronJob 예제: 매일 새벽 2시 DB 백업
apiVersion: batch/v1
kind: CronJob
metadata:
name: db-backup
namespace: production
spec:
schedule: "0 2 * * *" # 매일 02:00 UTC
timeZone: "Asia/Seoul" # K8s 1.27+ 타임존 지원
jobTemplate:
spec:
template:
spec:
containers:
- name: backup
image: postgres:15
command:
- /bin/sh
- -c
- |
pg_dump -h $DB_HOST -U $DB_USER $DB_NAME | \
gzip > /backup/db-$(date +%Y%m%d).sql.gz
env:
- name: DB_HOST
valueFrom:
secretKeyRef:
name: db-credentials
key: host
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: db-credentials
key: password
volumeMounts:
- name: backup-volume
mountPath: /backup
restartPolicy: OnFailure
volumes:
- name: backup-volume
persistentVolumeClaim:
claimName: backup-pvc
"If we switch the DB backup to a CronJob, we can remove the Jenkins dependency. Let's set concurrencyPolicy to Forbid so that a new Job is skipped if the previous backup is still running, and also add Slack notification integration for failures."
"To explain the difference between a CronJob and a Job: a Job is a batch task that runs once, while a CronJob repeatedly creates Jobs according to a schedule. If you set startingDeadlineSeconds on a CronJob, you can control how far back missed schedules are made up for during controller downtime."
"I checked why the CronJob didn't run, and startingDeadlineSeconds was set too short. It exceeded the deadline during the master node rolling update and was skipped. I'll increase it to 300 seconds or more and run the missed one with a manual Job."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
The default is the timezone of kube-controller-manager (usually UTC). "0 9 * * *" runs at 9:00 UTC (18:00 Korean time), not 9:00 Korean time. In Kubernetes 1.27+, specify spec.timeZone explicitly.
The default value of successfulJobsHistoryLimit is 3, but if you change it to unlimited, completed Jobs and Pods keep piling up and increase the load on etcd. Be sure to set an appropriate value.
In production environments, it is recommended to use ttlSecondsAfterFinished to automatically clean up completed Jobs, activeDeadlineSeconds to prevent infinite execution, and PrometheusRule to set up Job failure alerts.