如何使用Kubernetes Python客户端连接到Google 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()) 在我需要使用

我正在使用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 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 Python客户端提供Google凭据?
  • 如果我的思路正确,那么在哪里可以找到用于谷歌云的主机/端口?

  • 一些片段将不胜感激

    最后,我自己找到了解决办法

    首先,需要获取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'))