我希望能够从用Python编写的Google Cloud函数中访问和管理GKE(Kubernetes)集群。我已经成功访问并检索到了所创建的集群的数据(至少包括端点、用户名和密码),但是我不知道如何将它们与Kubernetes API包一起使用。
以下是我的导入内容:
这里是用于聚类数据的代码:
我想在那之后做类似这样的事情:
然而,我无法确定如何设置我的端点和身份验证信息。有人可以帮忙吗?
以下是我的导入内容:
import google.cloud.container_v1 as container
from google.auth import compute_engine
from google.cloud.container_v1 import ClusterManagerClient
from kubernetes import client, config
这里是用于聚类数据的代码:
project_id = 'my-gcp-project'
zone = 'my-zone'
cluster_id = 'my-existing-cluster'
credentials = compute_engine.Credentials()
gclient: ClusterManagerClient = container.ClusterManagerClient(credentials=credentials)
cluster = gclient.get_cluster(project_id,zone,cluster_id)
cluster_endpoint = cluster.endpoint
print("*** CLUSTER ENDPOINT ***")
print(cluster_endpoint)
cluster_master_auth = cluster.master_auth
print("*** CLUSTER MASTER USERNAME PWD ***")
cluster_username = cluster_master_auth.username
cluster_password = cluster_master_auth.password
print("USERNAME : %s - PASSWORD : %s" % (cluster_username, cluster_password))
我想在那之后做类似这样的事情:
config.load_kube_config()
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
然而,我无法确定如何设置我的端点和身份验证信息。有人可以帮忙吗?
credentials.refresh(google.auth.transport.requests.Request())
。 - Lucas