<p>How can I have Kubernates automatically restart a container which purposefully exits in order to get new data from environment variables?</p>

<p>I have a container running on a Kubernates cluster which operates in the following fashion:</p>

<ul>
<li>Container starts, polls for work</li>
<li>If it receives a task, it does some work</li>
<li>It polls for work again, until ...</li>
<li>.. the container has been running for over a certain period of time, after which it exits instead of polling for more work.</li>
</ul>

<p>It needs to be continually restarted, as it uses environment variables which are populated by Kubernates secrets which are periodically refreshed by another process. </p>

<p>I've tried a Deployment, but it doesn't seem like the right fit as I get <code>CrashLoopBackOff</code> status, which means the worker is scheduled less and less often.</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-fonky-worker
  labels:
    app: my-fonky-worker

spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-fonky-worker
  template:
    metadata:
      labels:
        app: my-fonky-worker
    spec:
      containers:
      - name: my-fonky-worker-container
        image: my-fonky-worker:latest
        env:
          - name: NOTSOSECRETSTUFF
            value: cats_are_great
          - name: SECRETSTUFF
            valueFrom:
              secretKeyRef:
                name: secret-name
                key: secret-key
</code></pre>

<p>I've also tried a CronJob, but that seems a bit hacky as it could mean that the container is left in the stopped state for several seconds.</p>

<p>As @Josh said you need to exit with <strong>exit 0</strong> else it will be treated as a failed container! Here is the <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#example-states" rel="nofollow noreferrer">reference</a> <br/> According to the first example there "Pod is running and has one Container. Container exits with success." if your <strong>restartPolicy</strong> is set to Always (which is default by the way) then the container will restart although the Pod status shows running but if you log the pod then you can see the restart of the container.</p>

<blockquote>
  <p>It needs to be continually restarted, as it uses environment variables which are populated by Kubernates secrets which are periodically refreshed by another process.</p>
</blockquote>

<p>I would take a different approach to this. I would mount the config map as explained <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#add-configmap-data-to-a-specific-path-in-the-volume" rel="nofollow noreferrer">here</a> this will automatically refresh the Mounted config maps data <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#mounted-configmaps-are-updated-automatically" rel="nofollow noreferrer">Ref</a>. Note: please take care of the " <strong>kubelet sync period</strong> (1 minute by default) + <strong>ttl of ConfigMaps cache</strong> (1 minute by default) in kubelet" to manage the refresh rate of configmap data in the Pod. </p>
