<p>How to attach a secret to running pod?</p>

<p>I have a pod running and i want to attach a secret.</p>

<p>I don't want to terminate the running pod instance.</p>

<p>I know pod are meant to run as stateless.</p>

prashant
<p>According to the <a href="https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets" rel="nofollow noreferrer">documentation</a>:</p>

<blockquote>
  <p>Secrets can be mounted as data volumes or be exposed as environment variables to be used by a container in a pod.</p>
  
  <p>This is an example of a pod that mounts a secret in a volume:</p>
</blockquote>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: redis
    volumeMounts:
    - name: foo
      mountPath: "/etc/foo"
      readOnly: true
  volumes:
  - name: foo
    secret:
      secretName: mysecret
</code></pre>

<blockquote>
  <p><strong>Mounted Secrets are updated automatically</strong></p>
  
  <p>When a secret being already consumed in a volume is updated, projected keys are eventually updated as well. Kubelet is checking whether the mounted secret is fresh on every periodic sync. However, it is using its local cache for getting the current value of the Secret. The type of the cache is configurable using the (ConfigMapAndSecretChangeDetectionStrategy field in KubeletConfiguration struct). It can be either propagated via watch (default), ttl-based, or simply redirecting all requests to directly kube-apiserver. As a result, the total delay from the moment when the Secret is updated to the moment when new keys are projected to the Pod can be as long as kubelet sync period + cache propagation delay, where cache propagation delay depends on the chosen cache type (it equals to watch propagation delay, ttl of cache, or zero corespondingly).</p>
  
  <p>Note: A container using a Secret as a subPath volume mount will not receive Secret updates.</p>
  
  <p>This is an example of a pod that uses secrets from environment variables:</p>
</blockquote>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: secret-env-pod
spec:
  containers:
  - name: mycontainer
    image: redis
    env:
      - name: SECRET_USERNAME
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: username
      - name: SECRET_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: password
  restartPolicy: Never
</code></pre>

<p>For both cases you need to change pod specification. You can do it by editing Pod or Deployment with kubectl edit:</p>

<pre><code>$ kubectl edit pod &lt;pod_name&gt; -n &lt;namespace_name&gt;
$ kubectl edit deployment &lt;deployment_name&gt; -n &lt;namespace_name&gt;
</code></pre>

<p>Alternatively you can make changes in YAML file and apply it:</p>

<pre><code>$ vi MyPod.yaml
$ kubectl apply -f MyPod.yaml
</code></pre>

<p>The most important thing you need to know, if you change the Pod specification, your Pod will be restarted to apply changes. In case of Deployment rolling update will happen. In most cases it is okay. If you need to save state of your application, the best way is to store valuable information using Volumes.</p>

<p>If you still want to add secrets without Pod restarts, you can use shared storage like NFS. When you change the content of NFS volume that already mounted into the Pod, the changes will be visible inside the pod instantly.
In certain cases you can exec shell inside the pod and mount NFS volume manually.</p>

<p>Alternatively, you can export the content of the secret to the file using the <a href="https://github.com/ashleyschuett/kubernetes-secret-decode" rel="nofollow noreferrer">ksd</a> program<br>
(or <code>base64 -d</code>) to decode base64 encoded values in the Secret:</p>

<pre><code>kubectl get secret mysecret -o yaml | ksd &gt; filename.yaml
</code></pre>

<p>and <a href="https://medium.com/@nnilesh7756/copy-directories-and-files-to-and-from-kubernetes-container-pod-19612fa74660" rel="nofollow noreferrer">copy</a> it to the pod using the following command:</p>

<pre><code>kubectl cp filename.yaml &lt;some-namespace&gt;/&lt;some-pod&gt;:/tmp/secret.yaml
</code></pre>
