Create Cronjobs in Kubernetes
Task :
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 datacenter.
Set schedule to */10 * * * *.
Container name should be cron-datacenter.
Use httpd image with latest tag only and remember to mention tag i.e httpd: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.
Solution :
thor@jump_host /$ cd /home/thor/ thor@jump_host ~$ vi cron.yml apiVersion: batch/v1beta1 kind: CronJob metadata: name: datacenter spec: schedule: "*/10 * * * *" jobTemplate: spec: template: spec: containers: - name: cron-datacenter image: httpd:latest command: - /bin/sh - -c - echo Welcome to xfusioncorp restartPolicy: OnFailure thor@jump_host ~$ kubectl apply -f cron.yml cronjob.batch/datacenter created thor@jump_host ~$ kubectl get cronjob NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE datacenter */10 * * * * False 0 <none> 21s
wait for 10 minutes
thor@jump_host ~$ kubectl get pod NAME READY STATUS RESTARTS AGE datacenter-1619272200-pvr86 0/1 Completed 0 4m47s