Configuring a Kubernetes Job with Multiple Pods
To configure a Kubernetes Job with multiple Pods, you'll need to specify the parallelism
and completions
settings in the Job's YAML configuration.
Here's an example YAML file for a Job that runs a simple "hello world" script in multiple Pods:
apiVersion: batch/v1
kind: Job
metadata:
name: hello-world-job
spec:
parallelism: 3
completions: 6
template:
spec:
containers:
- name: hello-world
image: busybox
command: ["/bin/sh", "-c", "echo 'Hello, LabEx!' && sleep 10"]
restartPolicy: OnFailure
In this example, the parallelism
is set to 3, which means that up to 3 Pods will be launched in parallel to execute the task. The completions
is set to 6, which means that the Job will be considered complete once 6 successful task completions have been achieved.
The Job's Pod template specifies a container that runs a simple "echo" command and then sleeps for 10 seconds. The restartPolicy
is set to OnFailure
, which means that Kubernetes will automatically retry the task if a Pod fails.
You can deploy this Job to your Kubernetes cluster using the following command:
kubectl apply -f hello-world-job.yaml
Once the Job is deployed, you can use the following commands to monitor its progress:
## View the status of the Job
kubectl get jobs
## View the Pods that have been launched for the Job
kubectl get pods -l job-name=hello-world-job
You can also view the logs of the Pods to see the output of the "hello world" script:
kubectl logs -l job-name=hello-world-job
By configuring the parallelism
and completions
settings, you can control how many Pods are launched in parallel and how many successful task completions are required for the Job to be considered complete. This allows you to scale your batch processing workloads and ensure that they are executed efficiently and reliably.