Kubernetes

Kubernetes CronJob

Scheduled backup job — runs on a cron schedule inside the cluster

📋Configuration Files

cronjob-backup.yml

yaml
1apiVersion: batch/v1
2kind: CronJob
3metadata:
4 name: db-backup
5spec:
6 schedule: "0 2 * * *"
7 jobTemplate:
8 spec:
9 template:
10 spec:
11 restartPolicy: OnFailure
12 containers:
13 - name: backup
14 image: postgres:16-alpine
15 command:
16 - /bin/sh
17 - -c
18 - pg_dump -h postgres -U app appdb > /backup/dump.sql
19 volumeMounts:
20 - name: backup
21 mountPath: /backup
22 volumes:
23 - name: backup
24 emptyDir: {}
📄

Step 01

Apply CronJob

(01)Deploy to cluster

Linux
1kubectl apply -f .
2kubectl get all
3kubectl get pods -w

(02)Remove

Linux
1kubectl delete -f .