In Kubernetes, a Job is used when you want Pods to run to completion rather than run continuously like Deployments or ReplicaSets. Typical use cases include batch processing, ETL, log analysis, or report generation.
In this guide, we’ll implement a simple Kubernetes Job in GKE to understand how it works step by step.
🔹 Step 01: Introduction
We’ll cover the following:
- Review a sample Kubernetes Job YAML file
- Deploy and verify the Job
- Create a Job imperatively using kubectl
- Clean up
🔹 Step 02: job1.yaml
Here’s our first Job definition:
apiVersion: batch/v1
kind: Job
metadata:
# Unique key of the Job instance
name: job1
spec:
template:
metadata:
name: job1
spec:
containers:
- name: job1
image: alpine
command: ['sh', '-c', 'echo Kubernetes Jobs Demo ; sleep 30']
# Do not restart containers after they exit
restartPolicy: Never
👉 This job runs an alpine container, prints "Kubernetes Jobs Demo", and then sleeps for 30 seconds before completing.
🔹 Step 03: Deploy Kubernetes Manifests
Apply the Job YAML to your GKE cluster:
# Deploy Kubernetes Manifests
kubectl apply -f job1.yaml
# OR
kubectl create -f job1.yaml
Verify the Job
# List Jobs
kubectl get jobs
# Describe Job
kubectl describe job job1
# List Pods created by the Job
kubectl get pods
👉 Observation:
- The Pod runs for around 30 seconds.
- Once complete, its status changes to Completed.
# Check Pod details
kubectl describe pod <POD-NAME>
🔹 Step 04: Delete the Job
Once tested, you can clean up the Job:
kubectl delete job job1
This removes the Job and all its Pods.
🔹 Step 05: Create a Job Imperatively
Instead of writing YAML, you can create a Job directly using kubectl:
# Create Job using kubectl imperative command
kubectl create job job2 --image=alpine -- sh -c 'echo Kubernetes Jobs Basics Demo ; sleep 30'
# List Jobs
kubectl get jobs
# List Pods
kubectl get pods
When you’re done:
# Delete the Job
kubectl delete job job2
🎯 Conclusion
- A Kubernetes Job runs Pods until completion and ensures tasks finish successfully.
- Useful for batch jobs, ETL processes, log analysis, or parallel data tasks.
- You can create Jobs via YAML manifests or imperative kubectl commands.
Top comments (0)