justmeandopensource / kubernetes

Kubernetes playground
1.68k stars 2.96k forks source link

Doubt regarding the CronJob #111

Closed basavrajdevpuje closed 2 years ago

basavrajdevpuje commented 2 years ago

Hello,

I have recently deployed the Matomo App (Web analytics app) along with the Galera-MariaDB cluster on Kubernetes. The Matomo app can also be installed on local systems (Windows or Linux).

On the local system, the cron job for the Matomo app can be set. Please refer to the link: https://matomo.org/faq/on-premise/how-to-set-up-auto-archiving-of-your-reports/

I want to accomplish the same thing with Kubernetes. However, I'm confused about "How to set a cron job for the running pod?"

Is it possible to set cronjobs for the running pods? Or could you please suggest an alternative solution?

Thanks in advance.

ak2766 commented 2 years ago

Yes, cronjobs in Kubernetes aren't exactly like cronjobs on a server. I'm thinking you might need to read up on sidecar containers. But having never used them myself, that's as far as my recommendation goes.

My tool of choice in Linux is a BASH script so I tend to tackle all manner of problems in kubernetes with scripts. To that end, I'd get the Dockerfile used to build Matomo and add a script just before going to the Entrypoint. Say you want to run a cleanup process every hour, I'd do this:

CMD [ "while true; do cleanup; sleep 3600; done" ]

NOTE: This is untested code but I hope you get the gist.

basavrajdevpuje commented 2 years ago

@ak2766 Thank you for your reply. It really helped me to get closer to the answer. I used command: [] (i.e. CMD [] as you mentioned) in my CRON job to get it done.

I also got to know that we cannot create a cron job for a running pod. But we can reuse the PVC used by the running pod by setting a POD AFFINITY rule in the CRON JOB which matches the label of the same pod.

justmeandopensource commented 2 years ago

@ak2766 thanks for helping.