使用 python api 从 GCP 管理 Kubernetes 集群
Posted
技术标签:
【中文标题】使用 python api 从 GCP 管理 Kubernetes 集群【英文标题】:Managing Kubernetes cluster from GCP with python api 【发布时间】:2019-02-02 21:24:34 【问题描述】:我希望能够从用 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))
但是,我不知道如何设置我的端点和身份验证信息。 谁能帮帮我?
【问题讨论】:
【参考方案1】:您可以使用不记名令牌而不是使用基本身份验证:
from google.auth import compute_engine
from google.cloud.container_v1 import ClusterManagerClient
from kubernetes import client
def test_gke(request):
project_id = "my-gcp-project"
zone = "my-zone"
cluster_id = "my-existing-cluster"
credentials = compute_engine.Credentials()
cluster_manager_client = ClusterManagerClient(credentials=credentials)
cluster = cluster_manager_client.get_cluster(name=f'projects/project_id/locations/zone/clusters/cluster_id')
configuration = client.Configuration()
configuration.host = f"https://cluster.endpoint:443"
configuration.verify_ssl = False
configuration.api_key = "authorization": "Bearer " + credentials.token
client.Configuration.set_default(configuration)
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
pods = v1.list_pod_for_all_namespaces(watch=False)
for i in pods.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
【讨论】:
谢谢 :) 我完全坚持这个!当我第一次打印 credentials.token 时,我发现了一个空变量。似乎得到一个集群会填满它! 获取集群没有为我获取token。要填充token
,我必须使用credentials.refresh(google.auth.transport.requests.Request())
。【参考方案2】:
您可以使用 google.oauth2 包通过 GCP 服务帐户进行身份验证。
from google.oauth2 import service_account
from google.cloud.container_v1 import ClusterManagerClient
from kubernetes import client, config
import os
def test_gke(project_id, zone, cluster_id):
SCOPES = ['https://www.googleapis.com/auth/cloud-platform']
credentials = service_account.Credentials.from_service_account_file(os.getenv('GOOGLE_APPLICATION_CREDENTIALS'), scopes=SCOPES)
cluster_manager_client = ClusterManagerClient(credentials=credentials)
cluster = cluster_manager_client.get_cluster(project_id, zone, cluster_id)
configuration = client.Configuration()
configuration.host = "https://"+cluster.endpoint+":443"
configuration.verify_ssl = False
configuration.api_key = "authorization": "Bearer " + credentials.token
client.Configuration.set_default(configuration)
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
pods = v1.list_pod_for_all_namespaces(watch=False)
for i in pods.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
请参阅以下链接以了解有关 GCP 授权 API 调用的更多信息 https://developers.google.com/identity/protocols/OAuth2ServiceAccount
【讨论】:
【参考方案3】:这是一个使用 GCP 服务帐户生成不记名令牌的示例。
请注意,您应确保在连接到集群时启用 SSL 验证,否则您很容易受到中间人攻击。 GKE 根据您需要手动配置的集群自己的证书执行此操作。
import base64
import google.auth.transport.requests
from google.oauth2 import service_account
from google.cloud.container_v1 import ClusterManagerClient
from kubernetes import client
from python_hosts.hosts import Hosts, HostsEntry
def test_gke(request):
project_id = "my-gcp-project"
zone = "my-zone"
cluster_id = "my-existing-cluster"
# Use a service account configured in GCP console,
# authenticating with a JSON key
credentials = service_account.Credentials \
.from_service_account_file('gcloud_key.json')
# Get cluster details
cluster_manager_client = ClusterManagerClient(credentials=credentials)
cluster = cluster_manager_client.get_cluster(
project_id=project_id, zone=zone,
cluster_id=cluster_id)
# Save cluster certificate for SSL verification
cert = base64.b64decode(cluster.master_auth.cluster_ca_certificate)
cert_filename = 'cluster_ca_cert'
cert_file = open(cert_filename, 'wb')
cert_file.write(cert)
cert_file.close()
# Configure hostname for SSL verification
hosts = Hosts()
hosts.add([HostsEntry(
entry_type='ipv4',
address=cluster.endpoint, names=['kubernetes'])])
hosts.write()
# Get a token with the scopes required by GKE
kubeconfig_creds = credentials.with_scopes(
['https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/userinfo.email'])
auth_req = google.auth.transport.requests.Request()
kubeconfig_creds.refresh(auth_req)
configuration = client.Configuration()
configuration.host = "https://kubernetes"
configuration.ssl_ca_cert = cert_filename
kubeconfig_creds.apply(configuration.api_key)
client.Configuration.set_default(configuration)
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
pods = v1.list_pod_for_all_namespaces(watch=False)
for i in pods.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
这里还有一个 Python 库列表(它们的 pip 项目名称):
kubernetes google-api-python-client 谷歌云容器 python 主机【讨论】:
以上是关于使用 python api 从 GCP 管理 Kubernetes 集群的主要内容,如果未能解决你的问题,请参考以下文章
使用 python 对 GCP 计算 API 端点进行身份验证
使用GCP API密钥限制对特定GCP App Engine服务的访问?