Detailed Explanation of Pod Controllers – Job

“Learning k8s from Scratch”

Introduction to Job

Job is primarily responsible forbatch processing (processing a specified number of tasks at once)temporaryone-time (each task runs only once and then ends)tasks. The characteristics of Job are as follows:

* When the pod created by Job successfully finishes, Job will record the number of successfully finished pods.

* When the number of successfully finished pods reaches the specified amount, Job will complete execution.

Detailed Explanation of Pod Controllers - Job

Resource manifest file for Job:

 apiVersion:batch/v1  # Version kind:Job   # Type metadata:  # Metadata   name:    # Job name   namespace:  # Namespace   labels:   # Labels     controller:job spec:    # Description details   completions:1   # Specifies the number of successful pod runs required for the job, default value: 1   parallelism:1   # Specifies the number of pods to run concurrently, default value: 1   activeDeadlineSeconds:30 # Specifies the time limit for the job to run, if it exceeds this time and has not finished, the system will attempt to terminate it   backoffLimit:6  # Specifies the number of retries after job failure, default: 6   manualSelector:true  # Whether to use selector to choose pods, default: false   selector:   # Selector, through which to specify which pods this controller manages     matchLabels:  # Labels matching rules       app:counterpod      matchExpressions:   # Expressions matching rules       -{key:app,operator:In,values:[counterpod]}   template:  # Template, when the number of replicas is insufficient, it will create pod replicas based on the template below     metadata:       labels:         app:counterpod     spec:       restartPolicy:Never# Restart policy can only be set to Never or OnFailure        containers:       -name:counter         image:busybox:1.34.1         command:["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1;do echo $i; sleep 2; done"]

Explanation of restart policy settings::

* If set toOnFailure, then the job will restart the container when the pod fails, rather than creating a new pod, and the failed count remains unchanged.

* If set toNever, then the job will create a new pod when the existing pod fails, and the failed pod will not disappear, nor will it restart, and the failed count increases by 1.

* If set toAlways, it means to always restart, which means the job task will be executed repeatedly, of course, this is incorrect, so it cannot be set toAlways.

Creating pc-job.yaml:

 cat > pc-job.yaml <<EOF  apiVersion:batch/v1 kind:Job    metadata:   name:pc-job   namespace:dev  spec:   manualSelector:true    selector:     matchLabels:       app:counter-pod   template:     metadata:       labels:         app:counter-pod     spec:       restartPolicy:Never        containers:       - name:counter         image:busybox:1.34.1         command:["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i; sleep 3; done"] EOF# Open 2 shells#1. Observe job statuskubectl get job -n dev -o wide -wNAME   COMPLETIONS DURATION AGE  CONTAINERS IMAGES          SELECTORpc-job  0/1         7s       7s   counter    busybox:1.34.1  app=counter-podpc-job  1/1         37s      37s  counter    busybox:1.34.1  app=counter-pod# Create jobkubectl create -f pc-job.yaml# Delete jobkubectl delete -f pc-job.yaml
# Modify pcjob.yaml, adjust the total number of pods and parallelism by setting the following two options under spec:#completions:6# Specifies the job needs to successfully run Pods a total of 6 times #parallelism:3# Specifies the job to run Pods concurrently as 3cat > pc-job.yaml <<  EOF  apiVersion:batch/v1 kind:Job      metadata:   name:pc-job   namespace:dev  spec:   manualSelector:true    completions:6   parallelism:3   selector:     matchLabels:       app:counter-pod   template:     metadata:       labels:         app:counter-pod     spec:       restartPolicy:Never        containers:       - name:counter         image:busybox:1.34.1         command:["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i; sleep 3; done"] EOF# Open 2 shells#1. Observe job statuskubectl get job -n dev -o wide -wNAME   COMPLETIONS DURATION AGE  CONTAINERS IMAGES          SELECTORpc-job  0/6        0s       0s   counter    busybox:1.34.1  app=counter-podpc-job  0/6        0s       0s   counter    busybox:1.34.1  app=counter-podpc-job  1/6        29s      29s  counter    busybox:1.34.1  app=counter-podpc-job  2/6        29s      29s  counter    busybox:1.34.1  app=counter-podpc-job  3/6        29s      29s  counter    busybox:1.34.1  app=counter-podpc-job  4/6        58s      58s  counter    busybox:1.34.1  app=counter-podpc-job  5/6        58s      58s  counter    busybox:1.34.1  app=counter-podpc-job  6/6        58s      58s  counter    busybox:1.34.1  app=counter-pod#2. Observe pod status, at this point, you will find that the job will run 3 pods each time, executing a total of 6 pods kubectl get pods -n dev -wNAME          READY  STATUS             RESTARTS  AGEpc-job-nc82v  0/1    Pending            0         0spc-job-nc82v  0/1    Pending            0         0spc-job-v7bft  0/1    Pending            0         0spc-job-9kwrt  0/1    Pending            0         0spc-job-v7bft  0/1    Pending            0         0spc-job-9kwrt  0/1    Pending            0         0spc-job-nc82v  0/1    ContainerCreating  0         0spc-job-v7bft  0/1    ContainerCreating  0         0spc-job-9kwrt  0/1    ContainerCreating  0         0spc-job-v7bft  0/1    ContainerCreating  0         1spc-job-nc82v  0/1    ContainerCreating  0         1spc-job-9kwrt  0/1    ContainerCreating  0         1spc-job-v7bft  1/1    Running            0         1spc-job-9kwrt  1/1    Running            0         2spc-job-nc82v  1/1    Running            0         2spc-job-v7bft  0/1    Completed          0         29spc-job-x47lm  0/1    Pending            0         0spc-job-x47lm  0/1    Pending            0         0spc-job-x47lm  0/1    ContainerCreating  0         0spc-job-9kwrt  0/1    Completed          0         29spc-job-s8w5m  0/1    Pending            0         0spc-job-s8w5m  0/1    Pending            0         0spc-job-nc82v  0/1    Completed          0         29spc-job-hxnj5  0/1    Pending            0         0spc-job-hxnj5  0/1    Pending            0         0spc-job-s8w5m  0/1    ContainerCreating  0         0spc-job-hxnj5  0/1    ContainerCreating  0         0spc-job-x47lm  0/1    ContainerCreating  0         0spc-job-s8w5m  0/1    ContainerCreating  0         1spc-job-hxnj5  0/1    ContainerCreating  0         1spc-job-x47lm  1/1    Running            0         2spc-job-hxnj5  1/1    Running            0         2spc-job-s8w5m  1/1    Running            0         2spc-job-x47lm  0/1    Completed          0         29spc-job-hxnj5  0/1    Completed          0         29spc-job-s8w5m  0/1    Completed          0         29s# Create jobkubectl create -f pc-job.yaml# Delete jobkubectl delete -f pc-job.yaml

Leave a Comment