【发布时间】:2019-01-28 21:16:23
【问题描述】:
我需要使用 Kubernetes Python client 和 Google Cloud python 客户端对在 GKE 中配置的 Kubernetes 集群进行身份验证。出于以下几个原因,我宁愿不向gcloud 支付费用:
- 当我拥有原生 Google Cloud 库时,在 Python 脚本中依赖系统 shell
gcloud是不优雅的 - 要求系统有
gcloud - 我必须将用户切换到相关的 ServiceAccount 并切换回来
- 它会产生启动/加入另一个进程的成本
因此,gcloud container clusters get-credentials(委托给gcloud config config-helper)的工作流程不足以让我获得所需的 API 密钥。如何使用 Google Cloud Python API 获得等效输出?
这是我目前所拥有的:
import kubernetes.client
import googleapiclient.discovery
import base64
# get the cluster object from GKE
gke = googleapiclient.discovery.build('container', 'v1', credentials=config['credentials'])
name = f'projects/{config["project_id"]}/locations/{config["location"]}/{parent}/clusters/{config["name"]}'
gke_clusters = gke.projects().locations().clusters()
gke_cluster = gke_clusters.get(name=name).execute()
# set up Kubernetes Config
kube_config = kubernetes.client.Configuration()
kube_config.host = 'https://{0}/'.format(gke_cluster['endpoint'])
kube_config.verify_ssl = True
#kube_config.api_key['authenticate'] = "don't know what goes here"
# regretably, the Kubernetes client requires `ssl_ca_cert` to be a path, not the literal cert, so I will write it here.
kube_config.ssl_ca_cert = 'ssl_ca_cert'
with open(kube_config.ssl_ca_cert, 'wb') as f:
f.write(base64.decodestring(gke_cluster['masterAuth']['clusterCaCertificate'].encode()))
# use Kubernetes client to do something
kube_client = kubernetes.client.ApiClient(configuration=kube_config)
kube_v1 = kubernetes.client.CoreV1Api(kube_client)
kube_v1.list_pod_for_all_namespaces(watch=False)
【问题讨论】:
标签: kubernetes google-cloud-platform google-oauth google-kubernetes-engine