How to Run Parallel Jobs in Kubernetes Using Completions and Parallelism
The DevOps team needs to process a batch of short-running tasks in Kubernetes. To reduce the total processing time, multiple tasks should run concurrently while limiting the amount of parallel work.
Your task is to create a Kubernetes Job named parallel-job using the busybox image.
Configure the Job so that:
- A total of 6 successful completions are required.
- No more than 2 Pods run concurrently.
- Each Pod prints
Data Processedand then remains active for 30 seconds before completing.
Monitor the Job and its Pods while it is running.
Jobs, refer to the official Kubernetes documentation.Create the Job Manifest
Use an imperative command to generate the Job manifest:
kubectl create job parallel-job \
--image=busybox \
--dry-run=client -o yaml \
-- /bin/sh -c 'echo "Data Processed"; sleep 30' > parallel-job.yamlEdit parallel-job.yaml and add completions and parallelism under spec:
Understanding completions and parallelism
completions: 6means the Job must successfully complete 6 Pods in total.parallelism: 2means Kubernetes can run up to 2 Pods at the same time.
With these settings, Kubernetes runs two Pods concurrently and continues creating new Pods until all 6 completions are successful.
Apply the Job
Apply the manifest to create the Job. The expected output confirms that parallel-job was created successfully.
$ kubectl apply -f parallel-job.yaml
job.batch/parallel-job createCheck the Pods
After creating the Job, check its Pods to verify that the Job is running as expected.
$ kubectl get po
NAME READY STATUS RESTARTS AGE
parallel-job-8nsnk 1/1 Running 0 15s
parallel-job-zjpqq 1/1 Running 0 15sTo observe the Pods while the Job is running:
$ kubectl get pods -w
NAME READY STATUS RESTARTS AGE
parallel-job-8nsnk 0/1 Completed 0 54s
parallel-job-9l7mp 1/1 Running 0 18s
parallel-job-cd7zc 1/1 Running 0 18s
parallel-job-zjpqq 0/1 Completed 0 54s
parallel-job-cd7zc 0/1 Completed 0 33s
parallel-job-9l7mp 0/1 Completed 0 34s
parallel-job-cd7zc 0/1 Completed 0 34s
parallel-job-cd7zc 0/1 Completed 0 34s
parallel-job-9l7mp 0/1 Completed 0 35s
parallel-job-9l7mp 0/1 Completed 0 35s
parallel-job-pjjvk 0/1 Pending 0 0s
parallel-job-lxfxn 0/1 Pending 0 0s
parallel-job-pjjvk 0/1 Pending 0 0s
parallel-job-lxfxn 0/1 Pending 0 0s
parallel-job-cd7zc 0/1 Completed 0 35s
parallel-job-9l7mp 0/1 Completed 0 35s
parallel-job-lxfxn 0/1 ContainerCreating 0 0s
parallel-job-pjjvk 0/1 ContainerCreating 0 0s
parallel-job-pjjvk 0/1 ContainerCreating 0 0s
parallel-job-lxfxn 0/1 ContainerCreating 0 0s
parallel-job-pjjvk 0/1 ContainerCreating 0 0s
parallel-job-lxfxn 0/1 ContainerCreating 0 0s
parallel-job-pjjvk 1/1 Running 0 3s
parallel-job-lxfxn 1/1 Running 0 3sBecause each Pod sleeps for 30 seconds, you should be able to observe up to 2 active Pods at the same time.
You can also check the Job's status:
$ kubectl get job parallel-job
NAME STATUS COMPLETIONS DURATION AGE
parallel-job Complete 6/6 107s 2m8sThe COMPLETIONS column should eventually reach:6/6
Check Pod Logs
List the Pods created by the Job using the job-name label, then check the logs of one of the Pods.
kubectl get pods -l job-name=parallel-jobThe logs should show Data Processed, confirming that the Job executed the required command successfully.
$ kubectl logs parallel-job-pjjvk
Data ProcessedImportant: parallelism controls the maximum number of Pods running concurrently. It does not mean the Job creates only 2 Pods. The Job will create enough Pods to achieve the configured 6 successful completions.CKA Exam Tips
These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.
1. Remember completions vs parallelism
completions defines the total number of successful executions, while parallelism defines the maximum number of executions that can run at the same time. For this task, use completions: 6 and parallelism: 2.
2. Generate Job YAML Quickly
You can quickly generate the basic Job YAML using kubectl create job with --dry-run=client. Then add the required completions and parallelism values to the generated manifest.
3. Use the Job Label to Find Its Pods
Instead of searching through all Pods, use the Job's label selector to list only the Pods created by parallel-job:
kubectl get pods -l job-name=parallel-job