<p>I'm using kubernetes secret as my environment variable
(<a href="http://kubernetes.io/docs/user-guide/secrets/#using-secrets-as-environment-variables" rel="nofollow noreferrer">http://kubernetes.io/docs/user-guide/secrets/#using-secrets-as-environment-variables</a>).</p>

<p>I've check whether env vars set correctly by </p>

<pre><code>kubectl exec -it my_pod bash
</code></pre>

<p>1.</p>

<pre><code>echo $RAILS_ENV #=&gt; staging
</code></pre>

<p>2.</p>

<pre><code>bin/rails c;
puts ENV['RAILS_ENV'] #=&gt; staging
</code></pre>

<p>It works fine for my rails application and bash command, but doesn't work when executing cron process.</p>

<p>I've read some post to understand cron process (e.g. <a href="https://unix.stackexchange.com/questions/27289/how-can-i-run-a-cron-command-with-existing-environmental-variables">https://unix.stackexchange.com/questions/27289/how-can-i-run-a-cron-command-with-existing-environmental-variables</a>) and know that I should source .profile or .bashrc before executing cron command like.</p>

<pre><code>$ crontab -l

* * * * * . $HOME/.profile; /bin/bash -l -c 'echo rails_env=$RAILS_ENV &gt;&gt; /tmp/cron_test.log'

* * * * * . $HOME/.bashrc; /bin/bash -l -c 'echo rails_env=$RAILS_ENV &gt;&gt; /tmp/cron_test.log'
</code></pre>

<p>but both doesn't work for me ˊˋ</p>

<p>result</p>

<pre><code>rails_env=
</code></pre>

<p>Any help will be appreciated!</p>

<p>===</p>

<p><strong>[update: add kubernetes yaml config]</strong></p>

<p>Both environment variables assign by kubernetes config and kubernetes secret can read in bash and rails application, but doesn't work in cron process.</p>

<p>deployment.yaml</p>

<pre><code>spec:
  containers:
  - name: my_rails
  ...
    command:
    - bash
    args:
    - /tmp/run_cron.sh
  ...
    env:
      - name: RAILS_ENV
        value: staging
      - name: MYSQL_PASSWORD
        valueFrom:
          secretKeyRef:
            name: rails-secret
            key: MYSQL_PASSWORD
</code></pre>

<p>secret.yaml</p>

<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: rails-secret
type: Opaque
data:
  MYSQL_PASSWORD: ENCODED_PASSWORD
</code></pre>

<p>I found the solution after discussion with some guys.</p>

<p>The problem should be docker level not kubernetes level.
<a href="https://ypereirareis.github.io/blog/2016/02/29/docker-crontab-environment-variables/" rel="nofollow noreferrer">https://ypereirareis.github.io/blog/2016/02/29/docker-crontab-environment-variables/</a></p>

<p>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.</p>

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

<p>STEP1.</p>

<p>Add one line to dump environment variables at run_cron.sh</p>

<pre><code>#!/bin/bash

# dump environment variables
printenv | sed 's/^\(.*\)$/export \1/g' &gt;&gt; /etc/profile.d/rails_env.sh

cron -f
</code></pre>

<p>STEP2.</p>

<p>Make cron read environment variables before executing command.</p>

<pre><code>* * * * * . /etc/profile.d/rails_env.sh; ruby my_job.rb &gt;&gt; /tmp/cron.log 2&gt;&amp;1
</code></pre>

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

<pre><code>* * * * * /bin/bash -l -c 'ruby my_job.rb &gt;&gt; /tmp/cron.log 2&gt;&amp;1'
</code></pre>

<p>Then cron work as expectation!</p>
