Kubernetes Job Examples-Create Kubernetes Jobs-DecodingDevOps

Kubernetes Job Examples-Create Kubernetes Jobs-DecodingDevOps

kubernetes Jobs

Sometimes we want to perform a small task rather than continuously running, this is when the kubernetes jobs come into the picture. A kubernetes job runs until it completes the task successfully. Jobs are useful only when you want to perform a certain operation only once. It can happen that the job fails without completing a task, in such cases the kubernetes job creates a new pod based on the template defined in the specification of the job YAML file.

Example of kubernetes Jobs

Here’s an example of YAML file used to create a job.

kubernetes job example yaml

Job.yaml

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
  backoffLimit: 4

It computes π to 2000 places and prints it out. It takes around 10s to complete.

kubectl apply -f Job.yaml

Creating your own YAML files

As deployment and other YAML files, there are few things which are compulsory which are: apiVersion, kind, and metadata.

We specify specs that are used to define the specification of the container. Its similar to what we define in the deployments file.

Inside specs, we write a template which is the wire frame of the container.

We specify the container name and image we can also override other things such as commands and etc.

Parallel Jobs

Parallel Jobs:

  • Only one pod is started when the job starts.
  • Job completes when the pod is completed successfully.

Parallel Jobs with fixed completion count:

  • specify a non-zero value in spec.completions
  • The job represents the overall task, and it’s completed when there is one successful pod for each value in range 1  to spec.completions

Parallel Jobs with work queues:

  • do not specify .spec.completions, default to .spec.parallelism.
  • Pods should inter-communicate in order to know what each pod should work on.
  • Each pod is capable of identifying whether the job is completed or not.
  • When any pod from the job is terminated with the status success no new pods will be scheduled.
  • Only when any one pod is terminated with success and all pods terminated with success, the job is said to be completed.
  • Once a pod is terminated with success all pods should start terminating.

We can use cronjobs to create jobs that run on a specific time.

  • kubernetes jobs example
  • kubernetes job example yaml
  • jobs in kubernetes
  • create job in kubernetes
  • kubernetes job template
  • kubernetes create job