What is Job object in Kubernetes?
Kubernetes Job types |
There are 3 types of jobs
non-parallel jobs [single pod jobs - unless it fails. creates replacement pod when pod goes down]
parallel jobs with a fixed completion count
parallel jobs with task queue
##Example type 1: hundred-fibonaccis.yml
In the Job description you can observe the attributes such as parallelism, Pod Statuses.
On the other terminal which is running in parallel, Observe the pod status change from Running to Completed:
##Example type 2
Now let's Describe the job counter :
--- apiVersion: batch/v1 kind: Job metadata: name: fibo-100 spec: template: spec: containers: - name: fib-container image: truek8s/hundred-fibonaccis:1.0 restartPolicy: OnFailure backoffLimit: 3Create the Job:
kubectl create -f hundred-fibonaccis.ymlNow let's describe the job:
kubectl describe job fibo-100
Describing a Job in Kubernetes |
On the other terminal which is running in parallel, Observe the pod status change from Running to Completed:
kubectl get po -wPod still exists to get the logs
kubectl logs [podname] -- [container]
We can see the Fibonacci number series printed out from the container logs.
Fibonacci series printed from kubctl logs command |
--- apiVersion: batch/v1 kind: Job metadata: name: counter spec: template: metadata: name: count-pod-template spec: containers: - name: count-container image: alpine command: - "/bin/sh" - "-c" - "for i in 100 50 10 5 1; do echo $i; done" restartPolicy: NeverTo create the counter-job use the following command :
kubectl create -f counter-job.yamlCheck Job list:
kubectl get jobsCheck the Pod:
kubectl get poCheck the log:
kubectl logs count-pod-xxx
Counter Job execution output using kubectl logs |
kubectl describe jobs counterYou can observer the Start Time and Pod Statuses from the above command ##cleanup delete job:
kubectl delete jobs counter-jobno need to delete pod, When a Job deleted automatically removes the corresponding all its pods.
Controlling Job Completions
In some situations you need to run the same job multiple times, we need to define completions: number it under Job-> Specifications (spec section)--- apiVersion: batch/v1 kind: Job metadata: name: hello-job spec: completions: 2 template: spec: containers: - name: busybox-container image: busybox command: ["echo", "hello Kubernetes job!!!"] restartPolicy: NeverObserve the number of the Completions on the
watch kubectl get all kubectl get pods kubectl delete job hello-job kubectl get podsOnce Job is deleted all its relavant resource will be cleaned up automaticall.
Parallelism
In some project there will be need to run multiple pods running in parallel--- apiVersion: batch/v1 kind: Job metadata: name: hello-job spec: parallelism: 2 template: spec: containers: - name: busybox-container image: busybox command: ["echo", "hello Kubernetes job!!!"] restartPolicy: NeverObserve the number of the Completions on the How backoffLimit works on Job? Let's do simple exeriment and understand this 'backoffLimit' attribute.
--- apiVersion: batch/v1 kind: Job metadata: name: hello-job spec: parallelism: 2 backoffLiit: 4 template: spec: containers: - name: busybox-container image: busybox command: ["ech0o", "hello Kubernetes job!!!"] restartPolicy: NeverObserve command mistyped purposefully, which will fail to create new Pod.
Working with CronJob
Job that works like a crontab in Linux systems. Any task that needs to be executed based on the scheduled time then we can use this Kubernetes Object.
--- apiVersion: batch/v1beta1 kind: CronJob metadata: name: hello-cron spec: schedule: "* * * * *" jobTemplate: spec: template: spec: containers: - name: busybox-container image: busybox command: ["echo", "Namste Kubernetes Cronjob!!!"] startPolicy: OnFailureCreating
kubectl create -f cronjob.yamlOpen in new terminal and run the following command to watch continuously :
watch kubectl get allCheck the pods section in the above output
No comments:
Post a Comment