<p>I create secret redis-secret with following command.</p>
<pre><code>kubectl create secret generic redis-secret --from-literal=password=0123456
</code></pre>
<p>After that I create pod <strong>secrets-via-file</strong>, using the <strong>redis</strong> image, which mounts secret name <strong>redis-secret</strong> at /secrets.</p>
<pre><code>kubectl run secret-via-file --image=redis --dry-run=client -o yaml &gt; pod.yaml
</code></pre>
<p>I edited the create <strong>pod.yaml</strong> file.</p>
<pre><code>apiVersion: v1
    kind: Pod
metadata:
  labels:
    run: secret-via-file
  name: secret-via-file
spec:
  containers:
  - image: redis
    name: secret-via-file
    volumeMounts:
    - name: redis-secret
      mountPath: /secrets
  volumes:
  - name: redis-secret
    secret:
      secretName: redis-secret
</code></pre>
<p>I created second pod name <strong>secret-via-env</strong>, using the <strong>redis</strong> image, which exports <strong>password</strong> as <strong>PASSWORD</strong>.</p>
<pre><code>kubectl run secret-via-env --image=redis --dry-run=client -o yaml &gt; pod2.yaml
</code></pre>
<p>I edited the <strong>pod2.yaml</strong> file. </p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  labels:
    run: secrets-via-env
  name: secrets-via-env
spec:
  containers:
  - image: redis
    name: secrets-via-env
    env:
      - name: PASSWORD
        valueFrom:
          secretKeyRef:
            name: redis-secret
            key: password
</code></pre>
<p>I connect to the pod <strong>secrets-via-env</strong> with following command.</p>
<pre><code>kubectl exec -it secret-via-file -- redis-cli
</code></pre>
<p>I try to verify if the secret is mounted to the pods. In the second pod I want to use the variable PASSWORD to retrieve the assigned value(0123456). I used the command below but it does not work.</p>
<pre><code>SECRET GET PASSWORD 
</code></pre>

O.Man
<p>tried as below. i see the PASSWORD secret is getting listed as env inside pod</p>
<pre><code># create secret
kubectl create secret generic redis-secret --from-literal=password=0123456

# create pod
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: secrets-via-env
  name: secrets-via-env
spec:
  containers:
  - image: redis
    name: secrets-via-env
    env:
    - name: PASSWORD
      valueFrom:
        secretKeyRef:
          name: redis-secret
          key: password

# check PASSWORD secret
master $ kubectl exec -it secrets-via-env sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
# echo $PASSWORD
0123456
</code></pre>
<pre><code># from first pod
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: secret-via-file
  name: secret-via-file
spec:
  containers:
  - image: redis
    name: secret-via-file
    volumeMounts:
    - name: redis-secret
      mountPath: /secrets
  volumes:
  - name: redis-secret
    secret:
      secretName: redis-secret

controlplane $ kubectl exec -it secret-via-file sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
# ls -l /secrets
total 0
lrwxrwxrwx 1 root root 15 Jul 22 09:45 password -&gt; ..data/password
# cat /secrets/password
0123456#
</code></pre>
