هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-report
namespace: shop
spec:
schedule: "15 2 * * *"
timeZone: "Asia/Riyadh"
concurrencyPolicy: Forbid
startingDeadlineSeconds: 600
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 5
jobTemplate:
spec:
backoffLimit: 2
activeDeadlineSeconds: 3600
template:
spec:
restartPolicy: Never
containers:
- name: report
image: registry.example.com/shop/jobs:1.4.2
args: ["python", "-m", "jobs.nightly_report"]
envFrom:
- secretRef: { name: db-cred }Step by step
- Schedule — the same 5 fields as cron.
timeZone(Kubernetes 1.27+) avoids UTC surprises. - The safety settings:
| Setting | Why |
|---|---|
concurrencyPolicy: Forbid | Never two runs at the same time (a slow run skips the next). |
startingDeadlineSeconds | Skip a run that could not start within this time (no burst of late runs). |
backoffLimit: 2 | Retry a failed Pod twice, then mark the Job failed. |
activeDeadlineSeconds | Kill a run that hangs. |
restartPolicy: Never | A failed Pod stays for its logs (a new Pod is used for the retry). |
*JobsHistoryLimit | Keep some old Jobs for logs, delete the rest. |
- Test it now — create a Job from the CronJob (quick answer) instead of waiting for 02:15.
- See the runs:
kubectl get cronjob nightly-report -n shop
NAME SCHEDULE TIMEZONE SUSPEND ACTIVE LAST SCHEDULE AGE
nightly-report 15 2 * * * Asia/Riyadh False 0 6h 9d
kubectl get jobs -n shop --sort-by=.metadata.creationTimestamp | tail -5
kubectl logs -n shop job/nightly-report-29384715- Pause during maintenance:
kubectl patch cronjob nightly-report -n shop -p '{"spec":{"suspend":true}}'- Alert on failures — a failed Job is easy to miss. With Prometheus/kube-state-metrics alert on
kube_job_status_failed > 0, or let the job ping a dead-man's switch (Uptime Kuma push monitor) when it succeeds.
Check that it worked
The manual test Job completes (COMPLETIONS 1/1), its log shows the work, LAST SCHEDULE updates each night, and old Jobs are cleaned up by the history limits.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Runs at the wrong hour | Schedule is UTC without timeZone. | Set timeZone, or convert the time. |
| Two runs at once, data broken | concurrencyPolicy: Allow (default). | Forbid (or Replace). |
Cannot determine if job needs to be started: too many missed start times | Controller was down / suspended long. | startingDeadlineSeconds; it recovers by itself. |
| Job never finishes | The process does not exit, or waits for input. | activeDeadlineSeconds; make the command exit. |
| No logs of a failed run | Pods deleted (history 0, or OnFailure restarts). | Keep failedJobsHistoryLimit; restartPolicy: Never. |
| Nobody noticed it failed for weeks | No alert. | Alert on failed Jobs or a missing success ping. |
Go further
- Command pages: kubectl, crontab.
- Next how-tos: Schedule a job with cron, Create a systemd timer, Get the logs of a crashed Pod.
- Learn it: CKA — Other workloads (Jobs, CronJobs).
1Which setting stops two runs of a CronJob from running at the same time?
concurrencyPolicy: Forbid.