Steps to Create a Kubernetes Job
Prerequsites:
- k8s cluster should be up and running
Here, for the demo purpose i am using Minikube.
Follow the steps mentioned below:
Step 1: Create a YAML file
nano <filename.yaml>
#nano job1.yaml
Step 2: In the below we have created a job which runs a pod based on the alpine image, and inside the container the job prints the number from 1 to 9 and then it terminates, and we have specified the restartPolicy: Never which means the pod will not be restarted if it exits.
apiVersion: batch/v1
kind: Job
metadata:
name: k8s-job
spec:
template:
metadata:
name: k8s-job
spec:
containers:
- name: job-demo
image: alpine:latest
command:
- "bin/sh"
- "-c"
- "for i in 1 2 3 4 5 6 7 8 9 ; do echo $i ; done"
restartPolicy: Never
Step 3: Create the yaml file using:
kubectl apply -f job1.yaml
Step 4: Check the state of the pods in the cluster
kubectl get pod
After executing the above command, the status coloumn will show that its completed. You can also use describe command to see a detailed information about the pod
kubectl describe pod <podname>
The Containers section of the output will lists the container state as Terminated due to the completion of Operation
Step 5: You can also check the job by –
kubectl get job
and the completion coloumn will show that the job is completed sucessfully.
How To Create Kubernetes Job/Cron Job
Kubernetes Jobs and CronJobs are essential tools for managing workloads within your Kubernetes cluster. Jobs enable you to execute one-time tasks, while CronJobs automates repetitive tasks based on a defined schedule. This comprehensive guide will walk you through creating and configuring both Jobs and CronJobs, empowering you to streamline your Kubernetes workflows.