DEV Community

Cover image for Part-94: 🚀Kubernetes Jobs in GKE – Run Tasks Until They Finish
Latchu@DevOps
Latchu@DevOps

Posted on

Part-94: 🚀Kubernetes Jobs in GKE – Run Tasks Until They Finish

In Kubernetes, we usually think about Deployments and ReplicaSets for long-running apps like websites or APIs. But sometimes, you don’t want something to run forever — instead, you need it to start → do its job → finish. That’s where Kubernetes Jobs come in. ✅


🔹 What is a Job?

A Job in Kubernetes runs one or more Pods until they successfully complete.

  • Once the task is done, the Pod stops.
  • The Job tracks the Pods and makes sure they finish.
  • If a Pod fails, the Job can retry it.
  • When the required number of completions is reached, the Job is marked as complete.

If you delete the Job, Kubernetes also deletes all the Pods it created.


🔹 Why use Jobs?

Jobs are perfect when you need one-time or short-lived tasks instead of services that run forever.

âś… Use cases:

  • Batch Processing → ETL jobs, log analysis, data cleanup, report generation
  • Parallel Processing → breaking down big tasks like image processing or data analysis across multiple Pods

🔹 Example Job Manifest

Here’s a simple example of a Kubernetes Job that runs a container, prints "Hello Kubernetes", and then exits:

apiVersion: batch/v1
kind: Job
metadata:
  name: hello-job
spec:
  template:
    spec:
      containers:
      - name: hello
        image: busybox
        command: ["echo", "Hello Kubernetes"]
      restartPolicy: Never
  backoffLimit: 2
Enter fullscreen mode Exit fullscreen mode

👉 Run it:

kubectl apply -f hello-job.yaml
kubectl get jobs
kubectl get pods
kubectl logs <pod-name>
Enter fullscreen mode Exit fullscreen mode

🔹 Job Lifecycle

  1. You create a Job.
  2. Kubernetes creates Pods to run it.
  3. Each Pod runs until it finishes.
  4. The Job is marked complete when success criteria are met.
  5. If you suspend a Job → Kubernetes deletes active Pods. Resume it → Pods start again.

🔹 Key Points to Remember

  • restartPolicy must be Never or OnFailure.
  • Jobs can be scaled to run multiple Pods in parallel.
  • For repeating jobs on a schedule → use a CronJob instead.

âś… In short:

Kubernetes Jobs = best way to run one-time tasks reliably inside your cluster. They handle retries, failures, and cleanup automatically.

Top comments (0)