There are multiple ways to run a Pod.
a. Directly run the Pod
b. Run the Pod using ReplicationSet or ReplicationController.
c. Run the Pob via Deployment
d. Run the Pod using a Job
You can even run a Pod using Cron Job. CronJobs are used to initialize and start a Pod at specified time.
When running a CronJob, a Job will get scheduled, this job will start a Pod.
For example, if you want to send email to all your application subscribers on every day morning 5AM, you can create a Cron Job, that spin up a Job at 5 AM, which in turn run a Pod, once the application in the Pod send emails, it terminates successfully. Next day at 5AM, another Job will spin up by cron job and same procedure repeats.
cronJobDemo1.yml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: cron-job-to-collect-log-files
labels:
app: wakeup-emails
author: krishna
serviceType: terminal-app
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: collect-log-files
image: busybox
command: ["/bin/sh"]
args: ["-c", "echo 'Collected all the log files'; sleep 10;"]
restartPolicy: OnFailure
Let’s create a CronJob using above definition file.
$kubectl create -f cronJobDemo1.yml cronjob.batch/cron-job-to-collect-log-files created
Let’s confirm that the cronjob, job and a Pod is created.
$kubectl get cronjobs.batch
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
cron-job-to-collect-log-files */1 * * * * False 1 10s 31s
$
$kubectl get pods
NAME READY STATUS RESTARTS AGE
cron-job-to-collect-log-files-1591510980-qbfx9 1/1 Running 0 11s
$
$kubectl get jobs
NAME COMPLETIONS DURATION AGE
cron-job-to-collect-log-files-1591510980 0/1 14s 14s
Wait for 10 seconds and query job, pods and you can confirm that the Pod exited successfully and Job is in completed state.
$kubectl get pods
NAME READY STATUS RESTARTS AGE
cron-job-to-collect-log-files-1591510980-qbfx9 0/1 Completed 0 26s
$
$kubectl get jobs
NAME COMPLETIONS DURATION AGE
cron-job-to-collect-log-files-1591510980 1/1 17s 29s
Wait for some time (more than a minute) and query jobs and pods, you can confirm new Job is created for every one minute and Pod started to collect log files.
$kubectl get jobs
NAME COMPLETIONS DURATION AGE
cron-job-to-collect-log-files-1591510980 1/1 17s 2m56s
cron-job-to-collect-log-files-1591511040 1/1 17s 116s
cron-job-to-collect-log-files-1591511100 1/1 17s 56s
$
$kubectl get pods
NAME READY STATUS RESTARTS AGE
cron-job-to-collect-log-files-1591510980-qbfx9 0/1 Completed 0 3m
cron-job-to-collect-log-files-1591511040-knn2q 0/1 Completed 0 2m
cron-job-to-collect-log-files-1591511100-fvsct 0/1 Completed 0 60s
Let’s stop this cron job by executing below command.
$kubectl delete cronjob.batch cron-job-to-collect-log-files
cronjob.batch "cron-job-to-collect-log-files" deleted
Once you delete a CRON job, all the jobs created by this Cron Job and all the Pods created will get deleted.
$kubectl get cronjob.batch
No resources found in default namespace.
$
$kubectl get jobs
No resources found in default namespace.
$
$kubectl get pods
No resources found in default namespace.
You can refer my below post to know more about CRON expressions.
https://self-learning-java-tutorial.blogspot.com/2014/10/cron-triggers.html
No comments:
Post a Comment