Kubernetes Cronjob Scheduling Guide

Schedule Cronjobs in Kubernetes

Schedule Cronjobs in Kubernetes

Tasks

  • Create a cronjob named xfusion.

  • Set its schedule to something like */7 * * * *. You can set any schedule for now.

  • Name the container cron-xfusion.

  • Utilize the httpd image with latest tag (specify as httpd:latest).

  • Execute the dummy command echo Welcome to xfusioncorp!.

  • Ensure the restart policy is OnFailure.

Steps

  1. Create a manifest file cronjob.yaml using given details.

     apiVersion: batch/v1
     kind: CronJob
     metadata:
       name: xfusion
     spec:
       schedule: "*/7 * * * *"
       jobTemplate:
         spec:
           template:
             spec:
               restartPolicy: OnFailure
               containers:
                 - name: cron-xfusion
                   image: httpd:latest
                   command: ["echo", "Welcome to xfusioncorp!"]
     cat <<EOF > cronjob.yaml
     apiVersion: batch/v1
     kind: CronJob
     metadata:
       name: xfusion
     spec:
       schedule: "*/7 * * * *"
       jobTemplate:
         spec:
           template:
             spec:
               restartPolicy: OnFailure
               containers:
                 - name: cron-xfusion
                   image: httpd:latest
                   command: ["echo", "Welcome to xfusioncorp!"]
     EOF
  2. Now apply the yaml manifest file.

     k apply -f cronjob.yaml
    • We can also use kubectl apply -f cronjob.yaml too.

  3. Let’s get the result.

      k get cronjob -o wide
  4. Describe the cronjob now.

     k describe cronjob

    #cronjob #k8s #kubernetes #happylearning