How to connect to Google Kubernetes engine with Kubernetes Python client(如何使用Kubernetes Python客户端连接到Google Kubernetes引擎)
本文介绍了如何使用Kubernetes Python客户端连接到Google Kubernetes引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Kubernetes Python客户端管理我本地的Kubernetes集群:
from kubernetes import client, config
config = client.Configuration()
config.host = "http://local_master_node:8080"
client.Configuration.set_default(config)
print(client.CoreV1Api().v1.list_node())
一切正常,直到我需要使用拥有Google项目的客户提供的密钥文件连接到Google Cloud Kubernetes引擎上的项目,如下所示:
{
"type": "...",
"project_id": "...",
"private_key_id": "...",
"private_key": "...",
"client_email": "...",
"client_id": "...",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/..."
}
我正在尝试加载它(可能是以错误的方式加载):
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.abspath('credentials.json')
config.load_incluster_config()
但此代码引发异常kubernetes.config.config_exception.ConfigException: Service host/port is not set.
这些问题是:
- 如何正确为Kubernetes Python客户端提供Google凭据?
- 如果我在正确的轨道上,那么在哪里可以找到用于Google Cloud的主机/端口?
一些代码片断将不胜感激。
推荐答案
最终,我自己找到了解决方案。
首先,您需要获取Kubernetes配置文件。所以,转到Google Cloud PlatformKubernetes Engine面板。选择要连接的群集,然后按connect按钮。选择Run in Cloud Shell,在登录到外壳类型后,建议字符串如下:
$ gcloud container clusters get-credentials ...
然后,您可以在~/.kube文件夹中找到配置文件。将其内容保存到YAML文件,您应该将该文件提供给kubernetes.config.load_kube_config函数:
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.abspath('credentials.json')
config.load_kube_config(os.path.abspath('config.yaml'))
这篇关于如何使用Kubernetes Python客户端连接到Google Kubernetes引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:如何使用Kubernetes Python客户端连接到Google Kubernetes引擎
基础教程推荐
猜你喜欢
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
