Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
K8s Workloads Kubernetes Kubernetes: From Zero to Production Module 3 - Pods and Workloads

Jobs and CronJobs - Batch Processing in Kubernetes

Reviewed & accurate
AI Summary

What You Will Learn

Intermediate

  • What Jobs and CronJobs are
  • When to use them
  • How to create them

A Job runs a Pod until it completes successfully. A CronJob runs Jobs on a schedule (like cron). Perfect for batch processing, backups, and cleanup.

job.yamlyaml
apiVersion: batch/v1
kind: Job
metadata:
  name: db-migration
spec:
  completions: 1          # Run once
  backoffLimit: 3         # Retry 3 times on failure
  template:
    spec:
      containers:
      - name: migrate
        image: my-app:latest
        command: ["python", "migrate.py"]
      restartPolicy: OnFailure
cronjob.yamlyaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: daily-backup
spec:
  schedule: "0 2 * * *"   # Daily at 2 AM
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: backup
            image: backup-tool:latest
            command: ["./backup.sh"]
          restartPolicy: OnFailure
CronJob schedule format
Same as Linux cron: minute hour day month weekday. "0 2 * * *" = 2 AM daily. "*/5 * * * *" = every 5 minutes.

Practical Exercise

Create a Job that prints hello and exits
Apply: kubectl apply -f job.yaml
Check: kubectl get jobs
Check: kubectl logs job/db-migration
Create a CronJob that runs every minute

Key Takeaways

  • Job: runs a Pod until completion.
  • CronJob: schedules Jobs on a cron schedule.
  • Use for: migrations, backups, batch processing, cleanup.
  • backoffLimit: retry count on failure.
  • restartPolicy: OnFailure or Never for Jobs.
Previously: Lesson 17 covered StatefulSets.
Today: You learned: Run batch tasks and scheduled jobs.
Next: Lesson 19 covers health probes.
Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments