Home / How To / Jobs in Kubernetes

Jobs in Kubernetes



A job creates one or more skis to perform a particular operation and ensures that the skis succeed in finishing. When a specified number of pods have been completed, the job is completed. When a job is deleted, the discs it created are deleted. A job can also be used to run multiple pods in parallel.

Click here to learn more about Kubernetes jobs.

In this article we will see the implementation of a simple job, a job with a time limit and a fuck.

Conditions

  1. Kubernetes Cluster with at least 1 working node.
    Click here to learn how to create a Kubernetes cluster. This guide will help you create a Kubernetes cluster with 1 Master and 2 Nodes on AWS Ubuntu 18.04 EC2 instances.

What should we do?

  1. Create jobs

Create jobs

Easy job

Create a definition file for multiple containers with the following contents.

vim simple-job-1.yml
apiVersion: batch/v1
kind: Job
metadata:
  name: simple-job-1
spec:
  template:
    spec:
      containers:
        - name: job-1
          image: busybox
          args:
            - /bin/sh
            - -c
            - date; echo sleeping....; sleep 30s; echo exiting!; date
      restartPolicy: Never

single-job defination

Get a list of existing pods and jobs in the Kubernetes Cluster and create a job with the following commands.

kubectl get pods
kubectl get jobs
kubectl create -f simple-job-1.yml

Create-A-simple jobs

In the screenshot above you can see that there are no skis and jobs in the existing cluster and that a new job has been created.

When you create a job, a pod with the job name is created along with the job.

You can get the pod and the job with the following commands.

kubectl get pods
kubectl get jobs

goat single-job-pod

In the screenshot above you can see that the job is completed after its run.

When you no longer need your job, you can delete it with the following command.

kubectl delete -f simple-job-1.yml
kubectl get pods
kubectl get jobs

delete-the-simple-job

A job with Enforce Time Limit

You can enforce a time limit in the job that ensures that the job must complete the implementation within the specified time limit.

Create a job definition file with the following content.

vim enforce-time-limit-job-2.yml
apiVersion: batch/v1
kind: Job
metadata:
  name: enforce-time-limit-job-2
spec:
  activeDeadlineSeconds: 20
  template:
    spec:
      containers:
        - name: job-2
          image: busybox
          args:
            - /bin/sh
            - -c
            - date; echo some task which should complete in 30 secs....but due to activeDeadlineSeconds=5 will get terminated within 5 secs before its completetion; sleep 30s; echo exiting!; date 
restartPolicy: Never

job definition

Get back a list of skis and jobs in the Kubernetes cluster and create a job with the file above.

kubectl get jobs
kubectl get pods
kubectl create -f enforce-time-limit-job-2.yml

Create-a-job

Once you have created the job if you have checked its details, you can observe that the pod that is created after the job is created ends before it is completed, ie that it does not run for 30 seconds and is completed

Check the reason for its termination, use the following command

kubectl get pods
kubectl  get jobs enforce-time-limit-job-2 -o yaml

job resignation reason

In the screenshot above on the first line, you can see that we have entered ActiveDeadlineSeconds = 20. And we have entered sleep = 30 seconds. This means that our pod would have been live for 30 seconds but it ended within 20 seconds.

To delete the job, you can use the following command.

kubectl delete -f enforce-time-limit-job-2.yml
kubectl get jobs

delete-the-job-to-enforce-time

Cron jobs

To create a cronjob job as an object in Kubernetes, create a file with the following content. This cronjob in Kubernetes is the same as Cronjob in Linux.

vim cronjob-3.yml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cronjob-3
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: cronjob-container
              image: busybox
              args:
                - /bin/sh
                - -c
                - date; echo sleeping for 5 secs....; sleep 5s; echo exiting...;
          restartPolicy: Never

cronjob-defination

The above definition says that the job is created after every minute and will last for 5 seconds.

Use the following commands to get a list of existing jobs and pods and create a cronjob.

kubectl get jobs
kubectl get pods
kubectl create -f cronjob-3.yml

create-a-cronjob

To see that the new job is created after each minute to complete the defined task, use the following command.

kubectl  get jobs -w

observe-cronjob-running-after-every-1 minute

Now you can check the logs where you can see that the job starts after every minute.

kubectl get pods
kubectl logs cronjob-3-1594359720-jfkcl

check-sign-of-subject-pod

In the screenshot above you can see the logs of different skis that were created as part of the job after each minute.

This means that the job triggered after every minute.

When you no longer need the job, you can delete it with the following command.

kubectl delete -f cronjob-3.yml
kubectl get pods
kubectl get jobs

delete-the-cronjob

Conclusion

In this article, we learned how to create a simple job in Kubernetes. We saw how we can enforce the time limit on the job to ensure that the job is not run for too long. In the end, we saw the steps to create a cronjob that is created on its own to perform the task at the specified time. This Cronjob object in Kubernetes is similar to the Cronjob job in Linux.


Source link