【问题标题】:k8s/python: How do I read a secret using the Kubernetes Python client?k8s/python:如何使用 Kubernetes Python 客户端读取密钥?
【发布时间】:2019-08-05 04:11:16
【问题描述】:

我想做这个问题的反面:

How to create secrets using Kubernetes Python client?

即:

如何通过 kubernetes-python API 从 kubernetes 集群中读取现有密钥?

用例是:我想从 jupyter notebook(也在我的集群中运行)对 mongodb(在我的集群中运行)进行身份验证,但出于显而易见的原因,我不想将 mongodb 身份验证密码保存在 jupyter notebook 中。

谢谢!

【问题讨论】:

标签: python mongodb kubernetes jupyter-notebook


【解决方案1】:
  1. 为python安装Kubernetes client
  2. 现在您可以提取秘密了。例如秘密名称 - mysql-pass,命名空间 - default
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
secret = v1.read_namespaced_secret("mysql-pass", "default")
print(secret)
  1. 如果您需要从密钥中提取解码后的密码
from kubernetes import client, config
import base64
import sys    
config.load_kube_config()
v1 = client.CoreV1Api()
sec = str(v1.read_namespaced_secret("mysql-pass", "default").data)
pas = base64.b64decode(sec.strip().split()[1].translate(None, '}\''))
print(pas)

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    如果你使用 kubernetes 客户端 api,它会给你一个 dict 数据类型的响应,你可能不需要做 spiting 等,你可以这样说,

    from kubernetes import client, config
    import base64
    config.load_kube_config()
    v1 = client.CoreV1Api()
    sec = v1.read_namespaced_secret("default-token-rsbq7", "default").data
    cert = base64.b64decode(sec["ca.crt"])
    print(cert)
    

    【讨论】:

      猜你喜欢
      • 2023-02-16
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多