image

Job in Kubernetes

A Job in Kubernetes produces one or more Pods and will keep retrying their execution until a certain number of them complete successfully.

The Job keeps track of how many pods have completed successfully.

The job is finished when a certain number of successful completions is met.

When you delete a Job, all of the Pods it produced are deleted as well.

Suspending a Job will result in the deletion of all current Pods until the Job is restarted.

Cronjob

One CronJob object is like one line of a crontab (cron table) file.

It runs a job periodically on a given schedule, written in Cron format.

If your control plane runs the kube-controller-manager in Pods or bare containers, the timezone set for the kube-controller-manager container determines the timezone that the cron job controller uses.

Setting variables such as CRON_TZ or TZ is not officially supported by the Kubernetes project.

CRON_TZ or TZ is an implementation detail of the internal library being used for parsing and calculating the next Job creation time.

Any usage of it is not recommended in a production cluster.

When creating the manifest for a CronJob resource, make sure the name you provide is a valid DNS subdomain name.

The name must be no longer than 52 characters.

This is because the CronJob controller will automatically append 11 characters to the job name provided and there is a constraint that the maximum length of a Job name is no more than 63 characters.

CronJobs are meant for performing regular scheduled actions such as backups, report generation, and so on.

Each of those tasks should be configured to recur indefinitely (for example: once a day / week / month); you can define the point in time within that interval when the job should start.

Cron schedule syntax

 ┌───────────── minute (0 - 59)
 │ ┌───────────── hour (0 - 23)
 │ │ ┌───────────── day of the month (1 - 31)
 │ │ │ ┌───────────── month (1 - 12)
 │ │ │ │ ┌──────────day of the week (0 - 6)
 │ │ │ │ │       (Sunday to Saturday;
 │ │ │ │ │       7 is also Sunday on some systems)
 │ │ │ │ │      OR sun, mon, tue, wed, thu, fri, sat
 │ │ │ │ │
 *  *  *  *  *
Entry Description Equivalent to
@yearly (or @annually) Run once a year at midnight of 1 January 0 0 1 1 *
@monthly Run once a month at midnight of the first day of the month 0 0 1 * *
@weekly Run once a week at midnight on Sunday morning 0 0 * * 0
@daily (or @midnight) Run once a day at midnight 0 0 * * *
@hourly Run once an hour at the beginning of the hour 0 * * * *

For example, the line below states that the task must be started every Friday at midnight, as well as on the 13th of each month at midnight:

0 0 13 * 5

Create Kubernetes cluster with 3 worker nodes.

Master: 1 node

Worker: 2 node

Hint

Solution

Create docker hub account. Docker Hub if you already have one skip this step

Open Play with Kubernetes login with your docker hub account.

Click on start

It will start a 4 hr session

create three instance

click on + ADD NEW INSTANCE three time to add three instances

image

on first instance enter below command, this node will be master node

kubeadm init --apiserver-advertise-address $(hostname -i) --pod-network-cidr 10.5.0.0/16

enter below command on first node

kubectl apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kubeadm-kuberouter.yaml

capture output of kubeadm join XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

you may also use kubeadm token list to find token

use this command on second and third node kubeadm join <IP address of master/first node>:6443 –token –discovery-token-unsafe-skip-ca-verification

image

enter captured command in second and third node

kubeadm join  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

image

image

Check node status, all 3 nodes should be in ready state

image

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

name: pi
image: perl
command: perl -Mbignum=bpi -wle 'print bpi(2000)'

Use Kubectl command

verify with below command

kubectl get jobs

kubectl logs job/pi

kubectl wait --for=condition=complete --timeout=300s job pi
Solution

This command will create job with image perl and name pi

kubectl create job pi  --image=perl -- perl -Mbignum=bpi -wle 'print bpi(2000)'

Once you have checked logs delete the job

kubectl delete job pi

Create a job that executes a command

name: hello
image: busybox
command: 'echo hello;sleep 20;echo world'

Use Kubectl command

verify with below command

kubectl get jobs

kubectl logs job/hello

kubectl describe jobs hello

kubectl logs job/hello
Solution

This command will create job with image busybox and name hello

kubectl create job hello --image=busybox -- /bin/sh -c 'echo hello;sleep 30;echo world'

Once you have checked logs delete the job

kubectl delete job hello

Create a job that executes a command and terminate automatically after 30 seconds

name: hi
image: busybox
command: 'while true; do echo hi; sleep 10;done'

verify with below command

kubectl get jobs

kubectl logs job/hi

kubectl describe jobs hi

kubectl logs job/hi
Solution

This command will create a yaml file for job with image busybox and name hi

kubectl create job hi --image=busybox --dry-run=client -o yaml -- /bin/sh -c 'while true; do echo hi; sleep 10;done' > hi.yaml
vi hi.yaml

use i to change to insert mode

add “activeDeadlineSeconds: 30” under spec as shown below

apiVersion: batch/v1
kind: Job
metadata:
  creationTimestamp: null
  name: hi
spec:
  activeDeadlineSeconds: 30 ##**
  template:
    metadata:
      creationTimestamp: null
    spec:
      containers:
      - command:
        - /bin/sh
        - -c
        - while true; do echo hi; sleep 10;done
        image: busybox
        name: hi
        resources: {}
      restartPolicy: Never
status: {}

Use :wq to save changes and exit from vi and below command to deploy

kubectl apply -f hi.yaml

Once you have checked logs delete the job

kubectl delete job hi

Create a job that executes a command and run 5 times one after another

name: welcome
image: busybox
command: 'do echo welcome; sleep 10;done'

verify with below command

kubectl get jobs

kubectl logs job/welcome

kubectl describe jobs welcome

kubectl logs job/welcome
Solution

This command will create a yaml file for job with image busybox and name welcome

kubectl create job welcome --image=busybox --dry-run=client -o yaml -- /bin/sh -c 'do echo welcome; sleep 10;done' > welcome.yaml
vi welcome.yaml

use i to change to insert mode

add “completions: 5 " under spec as shown below

apiVersion: batch/v1
kind: Job
metadata:
  creationTimestamp: null
  name: welcome
spec:
  completions: 5  ##**
  template:
    metadata:
      creationTimestamp: null
    spec:
      containers:
      - command:
        - /bin/sh
        - -c
        - do echo welcome; sleep 10;done
        image: busybox
        name: hi
        resources: {}
      restartPolicy: Never
status: {}

Use :wq to save changes and exit from vi and below command to deploy

kubectl apply -f welcome.yaml

Once you have checked logs delete the job

kubectl delete job welcome

Create a job that executes a command and run 5 times in parallel

name: greetings
image: busybox
command: 'do echo greetings; sleep 10;done'

verify with below command

kubectl get jobs

kubectl logs job/greetings

kubectl describe jobs greetings

kubectl logs job/greetings
Solution

This command will create a yaml file for job with image busybox and name greetings

kubectl create job greetings --image=busybox --dry-run=client -o yaml -- /bin/sh -c 'do echo greetings; sleep 10;done' > greetings.yaml
vi greetings.yaml

use i to change to insert mode

add “parallelism: 5” under spec as shown below

apiVersion: batch/v1
kind: Job
metadata:
  creationTimestamp: null
  name: greetings
spec:
  activeDeadlineSeconds: 30 ##**
  parallelism: 5  ##**
  template:
    metadata:
      creationTimestamp: null
    spec:
      containers:
      - command:
        - /bin/sh
        - -c
        - do echo greetings; sleep 10;done
        image: busybox
        name: hi
        resources: {}
      restartPolicy: Never
status: {}

Use :wq to save changes and exit from vi and below command to deploy

kubectl apply -f greetings.yaml

Once you have checked logs delete the job

kubectl delete job greetings

Create a cronjob that executes a command and run every minute

name: hello
image: busybox
command: 'date; echo Hello from the Kubernetes cluster'

Use Kubectl command

verify with below command

kubectl get cronjob

kubectl get pods

kubectl logs <pods from previous command>

kubectl describe cronjob hello

Solution

This command will create job with image busybox and name hello every minute

kubectl create cronjob hello --image=busybox --schedule="* * * * *" -- /bin/sh -c 'date; echo Hello from the Kubernetes cluster'

Once you have checked logs delete the job

kubectl delete cronjob hello

Create a cronjob that executes a command and run every minute and terminate it if it takes more than 15 seconds to start after scheduled time

name: welcome
image: busybox
command: 'date; echo Welcome to the Kubernetes cluster'

Use Kubectl command

verify with below command

kubectl get cronjob

kubectl get pods

kubectl logs <pods from previous command>

kubectl describe cronjob welcome

Solution

This command will create a yaml file for cronjob with image busybox and name welcome

kubectl create cronjob welcome --image=busybox --restart=Never --dry-run=client --schedule="* * * * *" -o yaml -- /bin/sh -c 'date; echo Welcome to the Kubernetes cluster' > welcome.yaml
vi welcome.yaml

use i to change to insert mode

add “startingDeadlineSeconds: 15” under spec as shown below

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  creationTimestamp: null
  name: welcome
spec:
  startingDeadlineSeconds: 15 ##**
  jobTemplate:
    metadata:
      creationTimestamp: null
      name: time-limited-job
    spec:
      template:
        metadata:
          creationTimestamp: null
        spec:
          containers:
          - command:
            - /bin/sh
            - -c
            - date; echo Welcome to the Kubernetes cluster
            image: busybox
            name: time-limited-job
            resources: {}
          restartPolicy: Never
  schedule: '* * * * *'
status: {}

Use :wq to save changes and exit from vi and below command to deploy

kubectl apply -f welcome.yaml

Once you have checked logs delete the job

kubectl delete cronjob hello

Create a cronjob that executes a command and run every minute and terminate it if it start but takes more than 10 seconds to complete

name: greetings
image: busybox
command: 'date; echo Greetings from the Kubernetes cluster'

Use Kubectl command

verify with below command

kubectl get cronjob

kubectl get pods

kubectl logs pods <pods from previous command>

kubectl describe cronjob greetings

Solution

This command will create a yaml file for job with image busybox and name greetings

kubectl create cronjob greetings --image=busybox --restart=Never --dry-run=client --schedule="* * * * *" -o yaml -- /bin/sh -c 'date; echo Greetings from the Kubernetes cluster' > greetings.yaml
vi greetings.yaml

use i to change to insert mode

add “activeDeadlineSeconds: 10” under spec as shown below

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  creationTimestamp: null
  name: greetings
spec:
  activeDeadlineSeconds: 10 ##**
  jobTemplate:
    metadata:
      creationTimestamp: null
      name: time-limited-job
    spec:
      template:
        metadata:
          creationTimestamp: null
        spec:
          containers:
          - command:
            - /bin/sh
            - -c
            - date; echo Greetings from the Kubernetes cluster
            image: busybox
            name: time-limited-job
            resources: {}
          restartPolicy: Never
  schedule: '* * * * *'
status: {}

Use :wq to save changes and exit from vi and below command to deploy

kubectl apply -f greetgins.yaml

Once you have checked logs delete the job

kubectl delete cronjob hello

Delete all three nodes and close session

  1. click on first node and then DELETE
  2. Repeat same for second and third node
  3. click close session

image

Congratulations you have completed this lab

Let us know if you still have any question