How to Run Automated Tasks in Kubernetes with a cronjob
How to Run Automated Tasks in Kubernetes with a cronjob
You can use CronJobs to run jobs on a time-based schedule. These automated jobs run like Cron tasks on a Linux or UNIX system.
Cron jobs are useful for creating periodic and recurring tasks, like running backups . Cron jobs can also schedule individual tasks for a specific time, such as if you want to schedule a job for a lean time.
Prerequisites:
1. You need an active Kubernetes Cluster.
2. You need Kubernetes cluster at version >= 1.8 (for CronJob)
Let's Start:
master01 $ kubectl cluster-info
Kubernetes master is running at https://172.17.0.11:6443
Alertmanager is running at https://172.17.0.11:6443/api/v1/namespaces/kube-system/services/alertmanager/proxy
KubeDNS is running at https://172.17.0.11:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
kube-state-metrics is running at https://172.17.0.11:6443/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy
NodeExporter is running at https://172.17.0.11:6443/api/v1/namespaces/kube-system/services/node-exporter:metrics/proxy
Prometheus is running at https://172.17.0.11:6443/api/v1/namespaces/kube-system/services/prometheus/proxy
Kubernetes master is running at https://172.17.0.11:6443
Alertmanager is running at https://172.17.0.11:6443/api/v1/namespaces/kube-system/services/alertmanager/proxy
KubeDNS is running at https://172.17.0.11:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
kube-state-metrics is running at https://172.17.0.11:6443/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy
NodeExporter is running at https://172.17.0.11:6443/api/v1/namespaces/kube-system/services/node-exporter:metrics/proxy
Prometheus is running at https://172.17.0.11:6443/api/v1/namespaces/kube-system/services/prometheus/proxy
[root@master01 tmp]# kubectl version
Client Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.0", GitCommit:"0ed33881dc4355495f623c6f22e7dd0b7632b7c0", GitTreeState:"clean", BuildDate:"2018-09-27T17:05:32Z", GoVersion:"go1.10.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.0", GitCommit:"0ed33881dc4355495f623c6f22e7dd0b7632b7c0", GitTreeState:"clean", BuildDate:"2018-09-27T16:55:41Z", GoVersion:"go1.10.4", Compiler:"gc", Platform:"linux/amd64"}
[root@master01 tmp]#
Client Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.0", GitCommit:"0ed33881dc4355495f623c6f22e7dd0b7632b7c0", GitTreeState:"clean", BuildDate:"2018-09-27T17:05:32Z", GoVersion:"go1.10.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.0", GitCommit:"0ed33881dc4355495f623c6f22e7dd0b7632b7c0", GitTreeState:"clean", BuildDate:"2018-09-27T16:55:41Z", GoVersion:"go1.10.4", Compiler:"gc", Platform:"linux/amd64"}
[root@master01 tmp]#
We need a config file. This example cron job config .spec file prints the current time and a hello message every minute:
Run the example CronJob by using this command:
kubectl create -f https://k8s.io/examples/application/job/cronjob.yaml
[root@master01 tmp]# kubectl create -f https://k8s.io/examples/application/job/cronjob.yaml
cronjob.batch/hello created
You have new mail in /var/spool/mail/root
[root@master01 tmp]#
[root@master01 tmp]# kubectl create -f https://k8s.io/examples/application/job/cronjob.yaml
cronjob.batch/hello created
You have new mail in /var/spool/mail/root
[root@master01 tmp]#
After creating the cron job, get its status using this command:
[root@master01 tmp]# kubectl get cronjob hello
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 1 53s 2m9s
You have new mail in /var/spool/mail/root
[root@master01 tmp]#
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 1 53s 2m9s
You have new mail in /var/spool/mail/root
[root@master01 tmp]#
We can watch the cronjob is getting created.
[root@master01 tmp]# kubectl get jobs --watch
NAME COMPLETIONS DURATION AGE
hello-1569253200 1/1 116s 2m26s
hello-1569253260 1/1 8s 85s
hello-1569253320 0/1 24s 24s
hello-1569253380 0/1 0s
hello-1569253380 0/1 0s 0s
hello-1569253320 1/1 65s 65s
hello-1569253380 1/1 5s 5s
hello-1569253200 1/1 116s 3m13s
hello-1569253200 1/1 116s 3m13s
hello-1569253200 1/1 116s 3m13s
NAME COMPLETIONS DURATION AGE
hello-1569253200 1/1 116s 2m26s
hello-1569253260 1/1 8s 85s
hello-1569253320 0/1 24s 24s
hello-1569253380 0/1 0s
hello-1569253380 0/1 0s 0s
hello-1569253320 1/1 65s 65s
hello-1569253380 1/1 5s 5s
hello-1569253200 1/1 116s 3m13s
hello-1569253200 1/1 116s 3m13s
hello-1569253200 1/1 116s 3m13s
Now, find the pods that the last scheduled job created and view the standard output of one of the pods.
[root@master01 tmp]# kubectl logs hello-1569253440-8kbsz
Mon Sep 23 15:45:01 UTC 2019
Hello from the Kubernetes cluster
[root@master01 tmp]#
Interesting Articles on Kubernetes:Mon Sep 23 15:45:01 UTC 2019
Hello from the Kubernetes cluster
[root@master01 tmp]#
Kubernetes : Kubernetes Node Management, Maintenance, Delete
How to add a New Worker Node to a existing kubernetes Cluster
MinIO Client Installation and Quickstart
PLEG is not healthy: Kubernetes Worker Node is in "NotReady" state
Backup MySQL databases in Kubernetes
How to Run Automated Tasks in Kubernetes with a cronjob
How to Completely remove Kubernetes
Delete the Cronjob when you do not need it:
[root@master01 tmp]# kubectl delete cronjob hello
cronjob.batch "hello" deleted
[root@master01 tmp]#
cronjob.batch "hello" deleted
[root@master01 tmp]#
No comments