1. Deploying the Pod
We begin with a pod definition in the file throw-dice-pod.yaml. This pod runs a container using thekodekloud/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:
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: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:-
Delete the current Job:
-
Update throw-dice-job.yaml to include the
completionsproperty. You can also increase thebackoffLimitif more attempts are expected: -
Apply the updated Job:
4. Running Jobs in Parallel
To run the Job in parallel instead of sequentially, add theparallelism property to the definition. If necessary, delete the existing Job and update throw-dice-job.yaml as follows:
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.
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.