【问题标题】:K8s Python Access Secret from inside Kubernetes来自 Kubernetes 内部的 K8s Python 访问密钥
【发布时间】:2022-01-03 18:05:44
【问题描述】:

我的问题与这个问题相同:k8s/python: How do I read a secret using the Kubernetes Python client? 但来自 Kubernetes 内部。我知道如何使用 kubernetes python 客户端从外部访问机密。

但是在 kubernetes 中如何访问 python 中的秘密?我有几个 python 微服务,它们都应该从 kubernetes 中访问秘密。 根据官方文档https://kubernetes.io/docs/tasks/inject-data-application/distribute-credentials-secure/#create-a-pod-that-has-access-to-the-secret-data-through-environment-variables我可以创建环境变量。然后可以通过import os; os["MY_VAR"] 访问这些变量吗?

【问题讨论】:

    标签: python kubernetes kubernetes-secrets


    【解决方案1】:

    我认为您的猜测是正确的,如果您使用 following configuration 部署 pod,那么您将能够在您的 pod 中访问环境变量 SECRET_USERNAME。那么,

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

    将允许您直接在 python 中访问此值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-05
      • 2018-05-28
      • 2020-07-23
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-16
      相关资源
      最近更新 更多