Skip to content
Advertisement

Openshift 3 CronJob inside running container

If I understand it correctly, you can configure Openshift to run cronjobs which will run a job periodically. Also a job seems to run a new container each time the cronjob fires.

I need a way to run a command each minute, but inside a running container/pod/deployment instead of creating a new container each time.
We use Openshift to deploy our php 7 / Laravel application. This framework uses an inbuilt scheduler. To make it work, you have to trigger the framework each minute with “php artisan run”.

As far as I understand, one should not alter the container image (php+apache) to include a cron service. But how should I do it instead?

Advertisement

Answer

I have a somewhat unconventional solution to this. OpenShift/Kubernetes has the concept of “Application Health Checks”. This is basically something that the platform runs every so often (as defined) and can either make an HTTP call, TCP connection, or perform an action within the container. The side effect to this is that the command run in the container fails, your pod will be restarted. This is generally used to check if an application is no longer responsive and to allow the platform to kill it off and restart.

But, it can totally be bastardized to do exactly what you need, and doesn’t require using the CronJobs, which are currently only available in Tech Preview.

You should be able to run the following, which will patch your current DeploymentConfig to include a livenessProbe that will run php artisan run every minute:

oc patch dc/<deployment-name> -p '{"spec":{"template":{"spec":{"containers":[{"name":"<deployment-name>","livenessProbe":{"exec":{"command":["/bin/bash","-c","php","artisan","run","||","true"]},"initialDelaySeconds":60,"periodSeconds":60,"timeoutSeconds":120}}]}}}}'

Note that you’ll need to provide the deployment-name twice in the above command.

This will tell OpenShift to run /bin/bash -c php artisan run || true 60 seconds after the pod is started, continue to run every 60 seconds, and consider it a failure if the command takes longer than 120 seconds to run. If the command times out more than 3 times, the pod will be considered unhealthy and restarted (|| true ensures that even if the command fails, it’ll won’t kill the pod).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement