Sym*_*mon 3 python authentication google-cloud-platform kubernetes 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())
Run Code Online (Sandbox Code Playgroud)
一切正常,直到我需要使用拥有 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/..."
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试加载它(可能以错误的方式加载):
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.abspath('credentials.json')
config.load_incluster_config()
Run Code Online (Sandbox Code Playgroud)
但是这段代码引发了一个异常 kubernetes.config.config_exception.ConfigException: Service host/port is not set.
问题是:
一些片段将不胜感激。
最后,我自己找到了解决方案。
首先,您需要获取 Kubernetes 配置文件。因此,转到 Google Cloud PlatformKubernetes Engine面板。选择要连接的集群并按下connect按钮。选择Run in Cloud Shell并在您登录到 shell 后键入建议的字符串,例如:
$ gcloud container clusters get-credentials ...
Run Code Online (Sandbox Code Playgroud)
然后你可以在~/.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'))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1013 次 |
| 最近记录: |