/    /  Kubernetes – Jobs

Kubernetes Jobs

 

Pods are the main purpose of a job, and they ensure that the specified number of pods are successful. When the specified number of pods is successful, the job is considered completed.

Creating a Job

Create a job with this command

apiVersion: v1
kind: Job ------------------------> 1
metadata:
   name: py
   spec:
   template:
      metadata
      name: py -------> 2
      spec:
         containers:
            - name: py ------------------------> 3
            image: python----------> 4
            command: ["python", "SUCCESS"]
            restartPocliy: Never --------> 5

The above code defines −

  • kind: Job → This tells kubectl that we’re creating a job type pod with the yaml file.
  • Name:py → This is the name of the template we’re using.
  • name: py → The name py has been given under container specs so that we can identify the pod that’s going to be created.
  • Image: python → This is the image we’re pulling to create the container.
  • restartPolicy: Never →If this condition is set as never, then the image will not restart if the container is killed or if it is false.

The job will be created using the following command with yaml and saved as  py.yaml.

$ kubectl create –f py.yaml

You can check the status of a job by running the following command.

$ kubectl describe jobs/py

You can check the status of a job by running the following command.

Scheduled Job

In Kubernetes, scheduled jobs are launched in Kubernetes clusters using Cronetes.

  • Schedule a job to run a pod at a specific time.
  • It gets a parodic job that runs automatically.

In order to schedule the job, we will use the same YAML we used to create the job.

apiVersion: v1
kind: Job
metadata:
   name: py
spec:
   schedule: h/25 * * * * ? -------------------> 1
   template:
      metadata
         name: py
      spec:
         containers:
         - name: py
         image: python
         args:
/bin/sh -------> 2
-c
ps –eaf ------------> 3
restartPocliy: OnFailure

The above code defines −

  • schedule: h/25 * * * * ? → Schedule the job to run every 25 minutes.
  • /bin/sh: Enters the container with /bin/sh
  • ps –eaf → Runs ps -eaf on the machine and lists all processes running.

The scheduled job concept is useful when we want to run a set of tasks at a specified time and then complete them.