简体   繁体   English

K8s Python 从内部访问秘密 Kubernetes

[英]K8s Python Access Secret from inside Kubernetes

My question is the same of this question: 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? but from inside Kubernetes.但从内部 Kubernetes。 I know how to access secrets from the outside with kubernetes python client.我知道如何使用 kubernetes python 客户端从外部访问机密。

But how do I access a secret in python when inside kubernetes?但是,当在 kubernetes 内部时,如何访问 python 中的秘密? I have several python microservices, and they should all access secrets from within kubernetes.我有几个 python 微服务,它们都应该从 kubernetes 中访问机密。 According to the official documentation https://kubernetes.io/docs/tasks/inject-data-application/distribute-credentials-secure/#create-a-pod-that-has-access-to-the-secret-data-through-environment-variables I can create environmental variables.根据官方文档https://kubernetes.io/docs/tasks/inject-data-application/distribute-credentials-secure/#create-a-pod-that-has-access-to-the-secret-data-通过环境变量我可以创建环境变量。 Would these variables then be accessible through import os; os["MY_VAR"]然后这些变量可以通过import os; os["MY_VAR"] import os; os["MY_VAR"] ? import os; os["MY_VAR"]

I think your guess is right, and that if you deploy a pod with the following configuration , then you will be able to access the environment variable SECRET_USERNAME within your pod.我认为您的猜测是正确的,如果您使用以下配置部署 pod,那么您将能够访问 pod 中的环境变量SECRET_USERNAME Then,然后,

import os
username = os.environ["SECRET_USERNAME"]

would allow you to access this value directly in python将允许您直接在 python 中访问此值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM