How can I run a cron command with environmental variables set by kubernetes secret How can I run a cron command with environmental variables set by kubernetes secret kubernetes kubernetes

How can I run a cron command with environmental variables set by kubernetes secret


Instead of running cron scripts inside the container, you should probably use CronJobs, where you can set env vars from secrets in the same way you do for deployments.


I found the solution after discussion with some guys.

The problem should be docker level not kubernetes level.https://ypereirareis.github.io/blog/2016/02/29/docker-crontab-environment-variables/

Cron process doesn't run as normal login-shell, so it didn't act as our expectation for read environment variables set by docker/k8s.

To solve the problem, we have to make cron process read environment variables first.

STEP1.

Add one line to dump environment variables at run_cron.sh

#!/bin/bash# dump environment variablesprintenv | sed 's/^\(.*\)$/export \1/g' >> /etc/profile.d/rails_env.shcron -f

STEP2.

Make cron read environment variables before executing command.

* * * * * . /etc/profile.d/rails_env.sh; ruby my_job.rb >> /tmp/cron.log 2>&1

or using bash --login option, which would every file under /etc/profile.d

* * * * * /bin/bash -l -c 'ruby my_job.rb >> /tmp/cron.log 2>&1'

Then cron work as expectation!