k8s/python:如何使用Kubernetes Python客户端读取机密? [英] k8s/python: How do I read a secret using the Kubernetes Python client?

查看:175
本文介绍了k8s/python:如何使用Kubernetes Python客户端读取机密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做与这个问题相反的事情:

I want to do the opposite of this question:

如何使用Kubernetes Python客户端创建秘密?

即:

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

用例是:我想从jupyter笔记本(也在群集中运行)对mongodb(在群集中运行)进行身份验证,而出于明显的原因,而无需在jupyter笔记本中保存mongodb auth密码.

The use case is: I want to authenticate to mongodb (running in my cluster) from a jupyter notebook (also running in my cluster) without, for obvious reasons, saving the mongodb auth password inside the jupyter notebook.

谢谢!

推荐答案

  1. 为python安装 Kubernetes客户端
  2. 现在您可以揭开秘密了.例如,秘密名称-mysql-pass,名称空间-default
  1. Install Kubernetes client for python
  2. Now you can pull the secret. For example secret name - mysql-pass, namespace - 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)

希望这会有所帮助.

这篇关于k8s/python:如何使用Kubernetes Python客户端读取机密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆