如何使用Kubernetes Python客户端连接到Google Kubernetes引擎 [英] How to connect to Google Kubernetes engine with Kubernetes Python client

查看:19
本文介绍了如何使用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.

这些问题是:

  1. 如何正确为Kubernetes Python客户端提供Google凭据?
  2. 如果我在正确的轨道上,那么在哪里可以找到用于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引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆