如何使用Kubernetes Python客户端连接到Google Kubernetes引擎
我正在使用Kubernetes Python客户端管理本地Kubernetes群集:如何使用Kubernetes Python客户端连接到Google Kubernetes引擎,python,authentication,kubernetes,google-cloud-platform,google-kubernetes-engine,Python,Authentication,Kubernetes,Google Cloud Platform,Google Kubernetes Engine,我正在使用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()) 在我需要使用
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 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/..."
}
我正在尝试加载它(可能是以错误的方式加载):
但此代码引发异常kubernetes.config.config\u exception.ConfigException:未设置服务主机/端口。
问题是:
一些片段将不胜感激 最后,我自己找到了解决办法 首先,需要获取Kubernetes配置文件。因此,进入谷歌云平台
Kubernetes引擎面板。选择要连接的群集,然后按connect
按钮。选择在云Shell中运行
,并在登录到Shell类型建议字符串后,如:
$ gcloud container clusters get-credentials ...
然后您可以在~/.kube
文件夹中找到配置文件。将其内容保存到yaml文件中,您应该将其馈送到kubernetes.config.load\u kube\u config
函数:
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.abspath('credentials.json')
config.load_kube_config(os.path.abspath('config.yaml'))
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.abspath('credentials.json')
config.load_kube_config(os.path.abspath('config.yaml'))