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.
More on Write-a-Kubernetes-manifest tasks
Write a Deployment and Service for an Nginx app with 3 replicas, resources and probes.BeginnerAdd an HPA that scales the Deployment between 3 and 15 pods at 70% CPU.BeginnerFix this broken Deployment YAML.BeginnerWrite a ConfigMap and Secret and inject them as environment variables and as a mounted file.IntermediateWrite an Ingress that routes `/api` to the api service and `/` to the web service with TLS.IntermediateWrite a pod spec with a hardened securityContext.Intermediate