【问题标题】:How to connect to Google Kubernetes engine with Kubernetes Python client如何使用 Kubernetes Python 客户端连接到 Google Kubernetes 引擎
【发布时间】:2019-03-24 05:58:41
【问题描述】:

我正在使用 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 Engine 上的项目,例如:

{
    "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.

问题是:

  1. 如何正确地为 Kubernetes Python 客户端提供 Google 凭据?
  2. 如果我在正确的轨道上,那么我在哪里可以找到与 Google Cloud 一起使用的主机/端口?

一些 sn-ps 将不胜感激。

【问题讨论】:

    标签: python authentication kubernetes google-cloud-platform google-kubernetes-engine


    【解决方案1】:

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

    首先,您需要获取 Kubernetes 配置文件。所以,去谷歌云平台Kubernetes Engine面板。选择要连接的集群,然后按connect 按钮。选择 Run in Cloud Shell 并在您登录到 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'))
    

    【讨论】:

      猜你喜欢
      • 2021-07-19
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 2023-02-16
      • 2019-10-07
      • 2021-01-05
      • 2016-11-07
      相关资源
      最近更新 更多