Introduction
In Kubernetes, jobs and cronjobs are used to run tasks that are not part of a long-running application or service. Jobs are used for one-off tasks, while cronjobs are used for tasks that need to be run on a regular schedule.
In this lab, we will learn how to run pods with jobs and cronjobs in Kubernetes. We will start with a simple example and gradually build up to more complex examples.
Start the Minikube Cluster
Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.
Navigate to your working directory:
Open the terminal and navigate to the default project folder:
cd /home/labex/projectStart Minikube:
Start Minikube to initialize a Kubernetes cluster:
minikube start- This command sets up a single-node Kubernetes cluster on your local machine.
- Minikube may take a few minutes to start depending on your system's performance.
Verify Minikube is running:
Check the status of the Minikube cluster:
minikube status- Look for components like
kubeletandapiserverlisted asRunning. - If the cluster is not running, rerun
minikube start.
- Look for components like
If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.
Run a Pod with a Job
The first step is to create a pod that runs a job. In this example, we will create a pod that runs a command to print "Hello, World!" to the console.
Create a file named job.yamlin /home/labex/project/ with the following contents:
apiVersion: batch/v1
kind: Job
metadata:
name: hello-job
spec:
template:
spec:
containers:
- name: hello
image: busybox
command: ["sh", "-c", 'echo "Hello, World!"']
restartPolicy: Never
backoffLimit: 4
In this file, we define a job named hello-job that runs a single container named hello. The container runs the busybox image and executes a command to print "Hello, World!" to the console.
To create the job, run the following command:
kubectl apply -f job.yaml
You can check the status of the job by running the following command:
kubectl get jobs
Once the job is completed, you can view the logs of the pod by running the following command:
kubectl logs <POD_NAME>
Replace <POD_NAME> with the name of the pod that ran the job,and you can get the POD_NAME with the kubectl get pods |grep hello-job command.
Congratulations, you have successfully run a pod with a job in Kubernetes!
Run a Job with Multiple Pods
In some cases, you may need to run a job with multiple pods for better performance. In this example, we will create a job that runs multiple pods to download files from a remote server.
Create a file named multi-pod-job.yaml in /home/labex/project/ with the following contents:
apiVersion: batch/v1
kind: Job
metadata:
name: download-job
spec:
completions: 3
parallelism: 2
template:
spec:
containers:
- name: downloader
image: curlimages/curl
command: ["curl", "-o", "/data/file", "http://example.com/file"]
volumeMounts:
- name: data-volume
mountPath: /data
restartPolicy: Never
volumes:
- name: data-volume
emptyDir: {}
backoffLimit: 4
In this file, we define a job named download-job that runs multiple pods with the curlimages/curl image. Each pod downloads a file from http://example.com/file and saves it to a shared volume named data-volume.
To create the job, run the following command:
kubectl apply -f multi-pod-job.yaml
You can check the status of the job by running the following command:
kubectl get jobs
Once the job is completed, you can view the logs of the pod by running the following command:
kubectl logs <POD_NAME>
Replace <POD_NAME> with the name of any pod that ran the job. You can see the download log of the file,and you can get the POD_NAME with the kubectl get pod |grep download-job command.
Congratulations, you have successfully run a job with multiple pods in Kubernetes!
Run a Cronjob
In addition to one-off jobs, Kubernetes also supports cronjobs for running tasks on a regular schedule. In this example, we will create a cronjob that runs a command every minute.
Create a file named cronjob.yaml in /home/labex/project/ with the following contents:
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello-cronjob
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
command: ["sh", "-c", 'echo "Hello, World!"']
restartPolicy: Never
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 3
In this file, we define a cronjob named hello-cronjob that runs a command every minute. The command is the same as the one we used in the first example to print "Hello, World!" to the console.
To create the cronjob, run the following command:
kubectl apply -f cronjob.yaml
You can check the status of the cronjob by running the following command:
kubectl get cronjobs
Once the cronjob is running, you can view the logs of the pod by running the following command:
kubectl logs -f <POD_NAME>
Replace <POD_NAME> with the name of any pod that was created by the cronjob,and you can get the POD_NAME with the kubectl get pod |grep hello-cronjob command.
Congratulations, you have successfully run a cronjob in Kubernetes!
Summary
In this lab, we learned how to run pods with jobs and cronjobs in Kubernetes. We started with a simple example of a pod that runs a job, and gradually built up to more complex examples of jobs with multiple pods and cronjobs that run on a regular schedule. With this knowledge, you can run various tasks that are not part of a long-running application or service in Kubernetes.


