Saturday, 4 October 2025

Create a CronJob in Kubernetes with nginx:latest (Scheduled Task)

Our course you can check :-   Udemy course 


Ques:-

DevOps team is setting up recurring tasks on different schedules. Currently, they're developing scripts to be executed periodically. To kickstart the process, they're creating cron jobs in the Kubernetes cluster with placeholder commands. Follow the instructions below:

Create a cronjob named devops.

Set Its schedule to something like */6 * * * *. You can set any schedule for now.

Name the container cron-devops.

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

Execute the dummy command echo Welcome to Learning!.

Ensure the restart policy is OnFailure.


Ans:-


raj@jumphost ~$ vi job.ymal
raj@jumphost ~$ cat job.ymal 

apiVersion: batch/v1
kind: CronJob
metadata:
  name: devops
spec:
  schedule: "*/6 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: cron-devops
            image: nginx:latest
            command: ["echo", "Welcome to Learning!"]
          restartPolicy: OnFailure

raj@jumphost ~$ ls
job.ymal
raj@jumphost ~$ kubectl apply -f job.ymal 
cronjob.batch/devops created
raj@jumphost ~$ kubectl get job
No resources found in default namespace.
raj@jumphost ~$ kubectl get cronjob
NAME     SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
devops   */6 * * * *   False     0        <none>          31s
raj@jumphost ~$ kubectl get pod
No resources found in default namespace.
raj@jumphost ~$ 


This will:

  • Create a CronJob named devops
  • Run every 6 minutes (*/6 * * * *)
  • Use the nginx:latest image
  • Execute the dummy command echo Welcome to Learning!
  • Restart the job only on failure

No comments:

Post a Comment