Interview Prep/Real-World Interview/Write-a-Kubernetes-manifest tasks

Write a CronJob that runs a backup every night at 02:00 and keeps limited history.

IntermediateHands-onWrite-a-Kubernetes-manifest tasks
Answer
apiVersion: batch/v1
kind: CronJob
metadata:
  name: db-backup
spec:
  schedule: "0 2 * * *"
  timeZone: "Asia/Kolkata"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 3
  startingDeadlineSeconds: 600
  jobTemplate:
    spec:
      backoffLimit: 2
      activeDeadlineSeconds: 3600
      template:
        spec:
          restartPolicy: OnFailure
          containers:
            - name: backup
              image: myrepo/pg-backup:1.2
              envFrom: [{ secretRef: { name: db-backup-creds } }]

Key trick → concurrencyPolicy: Forbid stops a slow backup from overlapping the next run. timeZone (stable since Kubernetes 1.27) avoids UTC confusion. Job pods need restartPolicy set to OnFailure or Never.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Kubernetes-manifest tasks