Kubernetes - Jobs đ
Description đ
Jobs are a way to run a pod to completion. A job creates one or more pods and ensures that a specified number of them successfully terminate. As pods successfully complete, the job tracks the successful completions. When a specified number of successful completions is reached, the job itself is complete. Deleting a job will clean up the pods it created.
Examples of jobs đ§âđŧ
- performing a one-time task such as running a script or setting up a work directory for a
deployment - computations for a finite amount of data, such as analyzing a large data set or rendering a frames of a movie
- and many other use cases where you want to run a
podto completion and then stop.
Basic Commands đ
-
get
jobskubectl get jobs -
output
joblogskubectl logs <job-pod-name>
Creating a Job đ ī¸
By default Kubernetes will restart a pod if it fails. This is not a desirable action for a job.
-
specsection of apodthat will not be resetspec: # default is Always # restartPolicy: Always # set to Never to run a pod to completion restartPolicy: Never -
sample
jobdefinition:apiVersion: batch/v1 kind: Job metadata: name: math-add-job spec: # to run multiple of the same job at the same time set completions to a number greater than 1 # by default the pods are made one after the other completions: 3 template: spec: containers: - name: math-add image: ubuntu command: ["expr", "77", "+", "5"] restartPolicy: Never -
to run
jobsinparallelsetspec.parallelismto a number greater than 1apiVersion: batch/v1 kind: Job metadata: name: math-add-job spec: completions: 3 # to run jobs in parallel set parallelism to a number greater than 1 parallelism: 3 template: spec: containers: - name: math-add image: ubuntu command: ["expr", "77", "+", "5"] restartPolicy: Never
Examples đ§Š
-
sample
jobdefinitionapiVersion: batch/v1 kind: Job metadata: name: throw-dice-job spec: completions: 1 template: metadata: name: throw-dice-pod spec: containers: - image: kodekloud/throw-dice name: throw-dice restartPolicy: Never -
updated
jobdefinition to increasespec.backoffLimitfrom default of 6 to 10apiVersion: batch/v1 kind: Job metadata: name: throw-dice-job spec: completions: 1 template: metadata: name: throw-dice-pod spec: containers: - image: kodekloud/throw-dice name: throw-dice restartPolicy: Never backoffLimit: 10 -
updated
jobto run as many times required for threecompletionsapiVersion: batch/v1 kind: Job metadata: name: throw-dice-job spec: completions: 3 template: metadata: name: throw-dice-pod spec: containers: - image: kodekloud/throw-dice name: throw-dice restartPolicy: Neversample outputđ kubernetes-zero-to-pro â kc get pods NAME READY STATUS RESTARTS AGE throw-dice-job-2hf2r 0/1 Completed 0 119s throw-dice-job-4zt5w 0/1 Completed 0 96s throw-dice-job-8mljc 0/1 Completed 0 2m23s throw-dice-job-k4m2g 0/1 Error 0 2m13s throw-dice-job-lwfpl 0/1 Error 0 2m17s throw-dice-job-qbmzl 0/1 Error 0 110s throw-dice-job-rjz54 0/1 Error 0 114s throw-dice-pod 0/1 Error 0 38mnotice the time it took to run the
joband the number ofpodsthat were created -
optimize the
jobto run threejobsinparallelapiVersion: batch/v1 kind: Job metadata: name: throw-dice-job spec: completions: 3 parallelism: 3 template: metadata: name: throw-dice-pod spec: containers: - image: kodekloud/throw-dice name: throw-dice restartPolicy: Neversample outputđ kubernetes-zero-to-pro â kc get pods NAME READY STATUS RESTARTS AGE throw-dice-job-6ct7x 0/1 Completed 0 59s throw-dice-job-6jf9n 0/1 Completed 0 59s throw-dice-job-p47j7 0/1 Completed 0 51s throw-dice-job-vjcjw 0/1 Error 0 59s throw-dice-job-zfhq6 0/1 Error 0 55s