Schedule Mailing on K8s CronJob

Onur Vatan
4 min readMay 19, 2020

If you work on cronjobs firstly you should learn that the Cron time string format. Cronjob is a job scheduler and it is using that time format to decide when it starts a new job.

There is a table for explaining the meaning of every column.
| | | | |
| | | | |
| | | | + — — Day of the Week (range: 1–7, 1 standing for Monday)
| | | + — — — Month of the Year (range: 1–12)
| | + — — — — Day of the Month (range: 1–31)
| + — — — — — Hour (range: 0–23)
+ — — — — — — Minute (range: 0–59)

At 22:00 on every day-of-week from Monday through Friday.
“At 22:00 on every day-of-week from Monday through Friday.” https://crontab.guru/#0_22_*_*_1-5

Also, you can check that very useful website: https://crontab.guru. But, this article not about just a Cron time string format. Because of this, we may go to the next chapter.

List of what we need for that example

I have created a .net core web API with a simple MailSenderController. I’ve used the MailKit library and added codes like below and published my API to AWS Lambda.

Install-Package MailKit -Version 2.5.0

After publishing our Mailsender Lambda function is ready.

Now, we have a lambda function URL for email sending. I just created a shell script for calling that lambda function.

And there is a post body in JSON format. I’ve created that file like that.

Now we are ready to dockerize our code. There is a Dockerfile.

After that preparation, we are ready to build and push to docker hub.

docker build -t onurvatan/mailsender:latest .docker push onurvatan/mailsender:latest

The last chapter is to deploy your cronjob to K8s cluster

We have our docker proxy and need to create a YAML file for apply to the K8s cluster. There is a YAML file that I created for that operation.

If you care to schedule is */1 * * * * this means is every minute your cronjob will create a job for calling your lambda URL via your docker container. Of course, change that schedule in real. After a job is complete, that job won’t process again. Your cronjob will create a new one as your specified schedule. Now apply that YAML and see the result.

kubectl apply -f mailsenderjob.yaml

If you call get cronjobs command.

kubectl get cronjobs

Results seem like that.

NAME          SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
mail-sender */1 * * * * False 0 <none> 7s

The last schedule is <none> because of your cronjob is waiting for the 1st minute. When time‘s up. You may see that.

NAME          SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
mail-sender */1 * * * * False 1 2s 45s

This is not 60s because your cronjob is waiting for the next minute on the time. And now if you check the jobs.

kubectl get jobs

After waiting for almost 2 minutes. You may see the result like that.

NAME                         COMPLETIONS   DURATION   AGE
mail-sender-1589849040 1/1 94s 94s
mail-sender-1589849100 1/1 34s 34s

That means your mail may be sent. Let’s check the mailbox. If there is a problem you should check your job logs or get the pod of your job.

Well done. Your cronjob is working. There is a lot of area for using those cronjobs. Truncate logs, perform a backup, move data between DBs, etc.

We have come to end of another article. Thanks for reading. Bye!

--

--