<p>The example has:</p>

<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: usernamekey
      - name: SECRET_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: passwordkey
  restartPolicy: Never
</code></pre>

<p>the above from:</p>

<p><a href="https://kubernetes.io/docs/concepts/configuration/secret/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/configuration/secret/</a></p>

<p>I've created a secret like this:</p>

<pre><code>kubectl --namespace=mycustomnamespace create secret generic mysecret --from-literal=passwordkey="abc123" --from-literal=usernamekey="mememe"
</code></pre>

<p>I understand that the above secrets exist under the namespace.</p>

<p>But if I try this:</p>

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

<p>(note that I added a namespace declaration under metadata)</p>

<p>I get this error:</p>

<blockquote>
  <p>Error validating data:
  [ValidationError(Pod.spec.containers[1].env[2].valueFrom.secretKeyRef):
  unknown field "namespace" in io.k8s.api.core.v1.SecretKeySelector,
  ValidationError(Pod.spec.containers[1].env[6].valueFrom.secretKeyRef):
  unknown field "namespace" in io.k8s.api.core.v1.SecretKeySelector];</p>
</blockquote>

<p>If I take out the namespace(s) (under the secretKeyRef(s))....the pod fails..with</p>

<blockquote>
  <p>Warning  Failed     2s (x8 over 1m)  kubelet, minikube  Error:
  secret "mysecret" not found</p>
</blockquote>

<p>Yes, my secrets are in the namespace:</p>

<pre><code>kubectl get secrets --namespace mycustomnamespace

NAME                  TYPE                                  DATA      AGE
default-token-55bzp   kubernetes.io/service-account-token   3         10m
mysecret              Opaque                                2         10m
</code></pre>

<p>APPEND : (resolution)</p>

<p>It was an error on my part.  Check my comment under Vasily's answer.</p>

<p>But basically, the magic-sauce is that the below yml....</p>

<pre><code>metadata:
  name: secret-env-pod
  namespace: mycustomnamespace
</code></pre>

<p>the above yml should "drive" the namespaces (aka, set the scope of the namespace) for the rest of the configuration (yml) ....  </p>

<p>(if you are a future reader of this question, double and triple check that you have everything under the correct namespace.  ALL of your normal "get" statements need to use -n (aka --namespace) as a part.</p>

<p>example</p>

<pre><code>kubectl get pods
</code></pre>

<p>the above will only get pods under "default".</p>

<p>you have to do</p>

<pre><code>kubectl get pods --namespace mycustomnamespace
</code></pre>

granadaCoder
<p>Simply remove <code>namespace: mycustomnamespace</code> from pod secretKeyRef definitions. </p>

<p>Also your secret create command should be like that:</p>

<pre><code>kubectl --namespace=mycustomnamespace create secret generic mysecret --from-literal=passwordkey="abc123" --from-literal=usernamekey="mememe"
</code></pre>
