Skip to main content
In this lesson, we will walk through a series of steps to deploy a pod, create and update a Job, run jobs in parallel, and set up a CronJob in Kubernetes. Follow along to see how each component is implemented. ─────────────────────────────

1. Deploying the Pod

We begin with a pod definition in the file throw-dice-pod.yaml. This pod runs a container using the kodekloud/throw-dice image. The container randomly generates a number between one and six. Here, a six indicates success while any other number signals failure. Our goal is to deploy the pod and review its logs to inspect the generated number. First, list the file and preview its contents:
The contents of throw-dice-pod.yaml are as follows:
Deploy the pod using:
Then, check the pod’s status:
If the pod status shows Error, it means the container generated a number other than six. Verify the result by inspecting the logs:
For example, an output like:
indicates the container generated a four. ─────────────────────────────

2. Creating a Job to Measure Attempts

Next, we will create a Kubernetes Job named throw-dice-job. This Job uses the same pod definition (without extra commands) to determine how many attempts it takes to roll a six. For further details, refer to the official Kubernetes Jobs documentation here. Create a file named throw-dice-job.yaml with the following content:
Deploy the Job:
Check its status with:
You might see an output like:
To review detailed pod events and status, describe the Job:
A sample output might display:
In this example, the Job ran a total of 4 attempts (1 succeeded and 3 failed) until a pod succeeded in generating a six.
─────────────────────────────

3. Updating the Job for Multiple Completions

To modify the Job such that it continues running until it achieves three successful completions, follow these steps:
  1. Delete the current Job:
  2. Update throw-dice-job.yaml to include the completions property. You can also increase the backoffLimit if more attempts are expected:
  3. Apply the updated Job:
Monitor the Job’s status using:
A sample output might be:
This output indicates that the Job took 4 attempts (three successful and one failed) to complete. ─────────────────────────────

4. Running Jobs in Parallel

To run the Job in parallel instead of sequentially, add the parallelism property to the definition. If necessary, delete the existing Job and update throw-dice-job.yaml as follows:
Deploy the updated Job:
You can describe the Job to verify that pods are now running in parallel and that you achieve three successful completions more quickly. ─────────────────────────────

5. Creating a CronJob

The final task is to create a CronJob that executes the job daily at 21:30. Create a file named throw-dice-cronjob.yaml with the following definition:
Ensure that the restart policy is specified as Never (with a capital “N”). Using “never” (all lowercase) will result in an error.
Deploy the CronJob:
A successful deployment might return:
─────────────────────────────

Conclusion

In this lesson, we covered how to:
  • Deploy a pod that performs a dice-rolling operation.
  • Create a Job to measure the number of attempts required for a successful roll.
  • Update the Job to require multiple completions.
  • Configure the Job to run in parallel.
  • Schedule the Job using a CronJob to run daily at a specified time.
These steps provide a comprehensive look at managing Kubernetes Jobs and CronJobs. Enjoy experimenting with these configurations in your Kubernetes environment! For more detailed information on Kubernetes components, visit the following resources:

Watch Video