<p>My question is the same of this question: <a href="https://stackoverflow.com/questions/55159582/k8s-python-how-do-i-read-a-secret-using-the-kubernetes-python-client">k8s/python: How do I read a secret using the Kubernetes Python client?</a> but from inside Kubernetes. I know how to access secrets from the outside with kubernetes python client.</p>
<p>But how do I access a secret in python when inside kubernetes? I have several python microservices, and they should all access secrets from within kubernetes.
According to the official documentation <a href="https://kubernetes.io/docs/tasks/inject-data-application/distribute-credentials-secure/#create-a-pod-that-has-access-to-the-secret-data-through-environment-variables" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/inject-data-application/distribute-credentials-secure/#create-a-pod-that-has-access-to-the-secret-data-through-environment-variables</a> I can create environmental variables. Would these variables then be accessible through <code>import os; os[&quot;MY_VAR&quot;]</code> ?</p>

<p>I think your guess is right, and that if you deploy a pod with the <a href="https://kubernetes.io/docs/tasks/inject-data-application/distribute-credentials-secure/#define-a-container-environment-variable-with-data-from-a-single-secret" rel="nofollow noreferrer">following configuration</a>, then you will be able to access the environment variable <code>SECRET_USERNAME</code> within your pod. Then,</p>
<pre class="lang-py prettyprint-override"><code>import os
username = os.environ[&quot;SECRET_USERNAME&quot;]
</code></pre>
<p>would allow you to access this value directly in python</p>
