DEV Community

Cover image for Part-95: 🚀Implementing Kubernetes Job Basics in Google Kubernetes Engine (GKE)
Latchu@DevOps
Latchu@DevOps

Posted on

Part-95: 🚀Implementing Kubernetes Job Basics in Google Kubernetes Engine (GKE)

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:

  1. Review a sample Kubernetes Job YAML file
  2. Deploy and verify the Job
  3. Create a Job imperatively using kubectl
  4. 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
Enter fullscreen mode Exit fullscreen mode

👉 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
Enter fullscreen mode Exit fullscreen mode

Verify the Job

# List Jobs
kubectl get jobs

# Describe Job
kubectl describe job job1

# List Pods created by the Job
kubectl get pods
Enter fullscreen mode Exit fullscreen mode

j1

j2

👉 Observation:

  1. The Pod runs for around 30 seconds.
  2. Once complete, its status changes to Completed.
# Check Pod details
kubectl describe pod <POD-NAME>
Enter fullscreen mode Exit fullscreen mode

🔹 Step 04: Delete the Job

Once tested, you can clean up the Job:

kubectl delete job job1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

When you’re done:

# Delete the Job
kubectl delete job job2
Enter fullscreen mode Exit fullscreen mode

j3


🎯 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)