Jobs & CronJobs in Kubernetes

What Are Kubernetes Jobs?

Kubernetes Jobs are used to run a **one-time** task in a cluster. Unlike Deployments, Jobs ensure that the task **completes successfully** and doesn't restart automatically.

✅ Use Cases for Kubernetes Jobs:

  • ⚡ **Database Migrations** – Running a one-time migration script.
  • 🔄 **Batch Processing** – Processing files, logs, or reports.
  • 🛠️ **Cleanup Tasks** – Deleting old logs or expired data.

Example: Running a One-Time Job

The following Kubernetes **Job** runs a simple echo command and exits.

apiVersion: batch/v1
kind: Job
metadata:
  name: example-job
spec:
  template:
    spec:
      containers:
      - name: my-job
        image: busybox
        command: ["echo", "Hello from Kubernetes Job!"]
      restartPolicy: Never

📌 **Explanation:**

  • 🔹 The container runs the `echo` command.
  • 🔹 Once completed, the job does not restart.
  • 🔹 `restartPolicy: Never` ensures the job does not restart if it fails.

What Are Kubernetes CronJobs?

A **CronJob** in Kubernetes is used for tasks that need to run at scheduled intervals, just like Linux **cron jobs**.

✅ Use Cases for Kubernetes CronJobs:

  • 📅 **Automated Database Backups** – Run backups every night.
  • 🔍 **Log Cleanup** – Delete old logs every 7 days.
  • 📢 **Sending Email Reports** – Generate and send reports at scheduled times.

Example: Running a Scheduled Task

The following **CronJob** runs every minute and prints a message:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: example-cronjob
spec:
  schedule: "*/1 * * * *" # Runs every 1 minute
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: my-cronjob
            image: busybox
            command: ["echo", "Hello from Kubernetes CronJob!"]
          restartPolicy: Never

📌 Understanding the `schedule` Field:

Schedule Meaning
"*/5 * * * *" Runs every **5 minutes**
"0 12 * * *" Runs at **12 PM daily**
"0 0 * * 1" Runs every **Monday at midnight**

Checking Job & CronJob Logs

To verify if a job or cron job executed successfully:

# Get all jobs
kubectl get jobs

# Get all cronjobs
kubectl get cronjobs

# Check logs of a specific job
kubectl logs job/example-job

Deleting Jobs & CronJobs

To remove a Job or CronJob manually:

# Delete a job
kubectl delete job example-job

# Delete a cron job
kubectl delete cronjob example-cronjob

Best Practices for Jobs & CronJobs

  • ✅ Use **Jobs** for tasks that must run only once and **CronJobs** for recurring tasks.
  • ✅ Set **restartPolicy: Never** unless the job needs to restart on failure.
  • ✅ Use **backoffLimit** to control retry attempts.
  • ✅ Monitor job execution with `kubectl logs` and `kubectl get jobs`.

Conclusion

✅ Kubernetes **Jobs** help execute **one-time tasks**, while **CronJobs** automate **scheduled jobs** using a **cron schedule**.

🎯 Next, we will explore **RBAC (Role-Based Access Control) in Kubernetes**, which ensures secure access control within clusters.