python - k8s/ python : How do I read a secret using the Kubernetes Python client?

标签 python mongodb kubernetes jupyter-notebook

我想做这个问题的反面:

How to create secrets using Kubernetes Python client?

即:

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

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

谢谢!

最佳答案

  1. 安装 Kubernetes client对于 python
  2. 现在你可以解开 secret 了。例如 secret 名称 - 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)

希望这会有所帮助。

关于python - k8s/ python : How do I read a secret using the Kubernetes Python client?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55159582/

相关文章:

mongodb - 如何检查数组字段是否是 MongoDB 中另一个数组的一部分?

kubernetes - 使用服务的外部 IP 作为部署的环境变量

kubernetes - 非法状态异常 : No entry found for connection 1001 Kafka Kubernetes

kubernetes - K8s : how to install charts from the Helm Hub

python - Twisted Web 服务 Django 项目

python - 使用临时列名而不是索引从 MySQL/Python 查询中检索数据

python - 如何使用python在unicode中转换像,"a³ a¡ a´a§"这样的字符?

mongodb - 如何在不使用 mongClient.getDatabaseNames() 的情况下检查 MongoDB 上是否存在数据库

javascript - Mongoose 的回调()参数无效错误

python - 如何让元素粘在 Tkinter 的右下角?