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引擎
基础教程推荐
猜你喜欢
- 症状类型错误:无法确定关系的真值 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01