Create Cronjobs in Kubernetes

Ticker

6/recent/ticker-posts

Create Cronjobs in Kubernetes

 Question There are some jobs/tasks that need to be run regularly on different schedules. Currently the Nautilus DevOps team is working on developing some scripts that will be executed on different schedules, but for the time being the team is creating some cron jobs in Kubernetes cluster with some dummy commands (which will be replaced by original scripts later). Create a cronjob as per details given below:

Create a cronjob named nautilus.

Set schedule to */2 * * * *.

Container name should be cron-nautilus.

Use nginx image with latest tag only and remember to mention tag i.e nginx:latest.

Run a dummy command echo Welcome to xfusioncorp.

Ensure restart policy is OnFailure.

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.

Please Note:-  Perform the below commands based on your question server,  user name & other details that might differ. So please read the task carefully before executing it. All the Best 👍

Solution:  

1.  Kindly check the existing pod & cronjob

thor@jump_host /$ kubectl get pod

No resources found in default namespace.

thor@jump_host /$

thor@jump_host /$ kubectl get cronjob

No resources found in default namespace.

thor@jump_host /$

2.  Create a YAML  file with all the parameters,  Kindly do the changes as per task

you can copy from GitLab    https://gitlab.com/nb-tech-support/devops.git

    Refer Below Video for more clarity )

thor@jump_host /$ vi /tmp/cron.yml

thor@jump_host /$

thor@jump_host /$ cat /tmp/cron.yml

apiVersion: batch/v1beta1

kind: CronJob

metadata:

  name: nautilus

spec:

  schedule: "*/2 * * * *"

  jobTemplate:

    spec:

      template:

        spec:

          containers:

            - name: cron-nautilus

              image: nginx:latest

              command:

                - /bin/sh

                - -c

                - echo Welcome to xfusioncorp

          restartPolicy: OnFailure

thor@jump_host /$

3. Run the below command to create a pod 

thor@jump_host /$ kubectl create -f /tmp/cron.yml

cronjob.batch/nautilus created

thor@jump_host /$

4.  Wait for the pod to get completed status & check Cronjob

thor@jump_host /$ kubectl get cronjob

NAME       SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE

nautilus   */2 * * * *   False     0        <none>          8s

thor@jump_host /$

thor@jump_host /$ kubectl get pod

NAME                        READY   STATUS      RESTARTS   AGE

nautilus-1627054320-f6xl6   0/1     Completed   0          40s

thor@jump_host /$

5.  Validate the task by running the below command 

thor@jump_host /$ kubectl logs nautilus-1627054320-f6xl6

Welcome to xfusioncorp

thor@jump_host /$

6.  Click on Finish & Confirm to complete the task successfully

Happy Learning!!!!

 Apart from this if you need more clarity,  I have made a  tutorial video on this, 

please go through and share your comments. Like and share the knowledge



Post a Comment

0 Comments

Latest Posts

Troubleshoot and Create Ansible Playbook Level 1 Ansible KodeKloud Engineer Task