DEV Community

Cover image for 8.Schedule Cronjobs in Kubernetes
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

8.Schedule Cronjobs in Kubernetes

Lab Information

The Nautilus 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 */8 * * * *. 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 xfusioncorp!.

Ensure the restart policy is OnFailure.
Enter fullscreen mode Exit fullscreen mode

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

Lab Solutions

Step 1: Create the YAML configuration file

First, let's create a YAML file that defines our cronjob:

cat > devops-cronjob.yaml <<EOF
apiVersion: batch/v1
kind: CronJob
metadata:
  name: devops
spec:
  schedule: "*/8 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: cron-devops
            image: nginx:latest
            command: ["/bin/sh", "-c"]
            args: ["echo Welcome to xfusioncorp!"]
          restartPolicy: OnFailure
EOF
Enter fullscreen mode Exit fullscreen mode

Explanation of what we just created:

apiVersion: batch/v1 - Uses the batch API for cronjobs

kind: CronJob - Specifies this is a cronjob resource

name: devops - Names the cronjob "devops"

schedule: "*/8 * * * *" - Runs every 8 minutes

name: cron-devops - Container name as required

image: nginx:latest - Uses nginx:latest image

command/args - Executes the echo command

restartPolicy: OnFailure - Sets the restart policy

Step 2: Apply the YAML file to create the cronjob

Now, let's create the cronjob in the Kubernetes cluster:

kubectl apply -f devops-cronjob.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the cronjob was created

Check that the cronjob exists:

kubectl get cronjob devops
Enter fullscreen mode Exit fullscreen mode

Step 4: Check the cronjob details (optional)

To see more detailed information about the cronjob:

kubectl describe cronjob devops
Enter fullscreen mode Exit fullscreen mode

Step 5: Monitor for job execution

Since the cronjob runs every 8 minutes, wait a few minutes and then check if any jobs have been created:

kubectl get jobs
Enter fullscreen mode Exit fullscreen mode

Step 6: Check the logs of a completed job

When you see jobs in the output from Step 5, get the pods associated with those jobs:

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Look for pods with names starting with "devops-" and then check their logs:

kubectl logs devops-29346092-fr6tk
Enter fullscreen mode Exit fullscreen mode

Step 7: Verify the pod details (optional)

To confirm the pod used the correct container name and image:

kubectl describe pod <pod-name>
Enter fullscreen mode Exit fullscreen mode

Look for:

Containers: section showing cron-devops

Image: showing nginx:latest

Restart Policy: showing OnFailure

Top comments (0)