Pages

Sunday, May 20, 2018

kubernetes - Jobs

In Kubernetes , a Job is a controller object that represents a finite task. Generally if we have a replication controller or replica set , we will have the 
kubernetes engine run a specified number of pods running all the time. They
make sure that pods are up and running at any time.

Jobs differ from the other controller objects is that jobs manage the task as it runs to completion rather than managing an ongoing desired state ) such as total number of running pods ).

The general use case where jobs are used when we need to run some computational jobs or batch oriented tasks. You can use a Job to run independent but related work items in parallel: sending emails, rendering frames, transcoding files, scanning database keys, etc. However, Jobs are not designed for closely-communicating parallel processes such as continuous streams of background processes.

Lets create a simple Job as,
[root@manja17-I13330 kubenetes-config]# cat basic-job.yml
apiVersion: batch/v1
kind: Job
metadata:
 name: testing-service
spec:
 template:
   metadata:
     labels:
       name: testing-service
   spec:
     containers:
     - name: counter
       image: centos:7
       command:
         - "bin/bash"
         - "-c"
         - "for i in 9 8 7 6 5 4 3 2 1 ; do echo $i ; done"

     restartPolicy: Never

This is a simple job manifest file which when created will create a pod with single container called "counter". the job of the counter container is to execute a command with a bash for loop.

create the pod and check the pod status as,
[root@manja17-I13330 kubenetes-config]# kubectl get pods
NAME                     READY STATUS RESTARTS   AGE
testing-service-gfmrz   0/1 Completed 0          31s

The pod is created and status is completed which means counter container has completed the job.

We can also get the jobs available current using "kubectl get jobs". Lets describe the pod to see what it did,
[root@manja17-I13330 kubenetes-config]# kubectl describe job/testing-service
Name:           testing-service
Namespace:      default
Selector:       controller-uid=0e67e17a-5b96-11e8-9494-020055e1ea1d
Labels:         controller-uid=0e67e17a-5b96-11e8-9494-020055e1ea1d
               job-name=testing-service
               name=testing-service
Annotations:    <none>
Parallelism:    1
Completions:    1
Start Time:     Sat, 19 May 2018 14:54:27 -0400
Pods Statuses:  0 Running / 1 Succeeded / 0 Failed
Pod Template:
 Labels:  controller-uid=0e67e17a-5b96-11e8-9494-020055e1ea1d
          job-name=testing-service
          name=testing-service
 Containers:
  counter:
   Image:      centos:7
   Port:       <none>
   Host Port:  <none>
   Command:
     bin/bash
     -c
     for i in 9 8 7 6 5 4 3 2 1 ; do echo $i ; done
   Environment:  <none>
   Mounts:       <none>
 Volumes:        <none>
Events:
 Type    Reason           Age From     Message
 ----     ------           ---- ----     -------
 Normal  SuccessfulCreate  2m job-controller  Created pod: testing-service-gfmrz

We can check the logs of the pod to see what exactly happened as,
[root@manja17-I13330 kubenetes-config]# kubectl logs testing-service-gfmrz
9
8
7
6
5
4
3
2
1

More to come on kubernetes, Happy learning :-)

No comments :

Post a Comment