简体   繁体   English

如何使用 python 删除 GKE(Google Kubernetes 引擎)集群?

[英]How to delete GKE (Google Kubernetes Engine) cluster using python?

I'm new to GKE-Python.我是 GKE-Python 的新手。 I would like to delete my GKE(Google Kubernetes Engine) cluster using a python script.我想使用 python 脚本删除我的 GKE(Google Kubernetes 引擎)集群。 I found an API delete_cluster() from the google-cloud-container python library to delete the GKE cluster.我从google-cloud-container python 库中找到了一个 API delete_cluster()来删除 GKE 集群。 https://googleapis.dev/python/container/latest/index.html https://googleapis.dev/python/container/latest/index.html

But I'm not sure how to use that API by passing the required parameters in python.但我不确定如何通过在 python 中传递所需的参数来使用 API。 Can anyone explain me with an example?任何人都可以用一个例子来解释我吗?

Or else If there is any other way to delete the GKE cluster in python?或者如果还有其他方法可以删除 python 中的 GKE 集群?

Thanks in advance.提前致谢。

This page contains an example for the command you are trying to perform.页面包含您尝试执行的命令的示例。

To give some more details that are required for the command to succeed - Your environment needs to contain environment variables, this page contains instructions for how to do that.要提供命令成功所需的更多详细信息 - 您的环境需要包含环境变量,此页面包含有关如何执行此操作的说明。

Once your environment is successfully authenticated we can run the delete cluster command like so -一旦您的环境成功通过身份验证,我们就可以像这样运行删除集群命令 -

from google.cloud import container_v1

client = container_v1.ClusterManagerClient()

response = client.delete_cluster(name=projects/<project>/locations/<location>/clusters/<cluster>)

First you'd need to configure the Python Client for Google Kubernetes Engine as explained on this section of the link you shared.首先,您需要为 Google Kubernetes 引擎配置 Python 客户端,如您共享的链接的本节所述。 Basically, set up a virtual environment and install the library with pip install google-cloud-container .基本上,设置一个虚拟环境并使用pip install google-cloud-container安装库。

If you are running the script within an environment such as the Cloud Shell with an user that has enough access to manage the GKE resources (with at least the Kubernetes Engine Cluster Admin permission assigned) the client library will handle the necessary authentication from the script automatically and the following script will most likely work:如果您在Cloud Shell等环境中运行脚本,并且用户具有足够的访问权限来管理 GKE 资源(至少分配了Kubernetes 引擎集群管理员权限),客户端库将自动处理来自脚本的必要身份验证并且以下脚本很可能会起作用:

from google.cloud import container_v1

project_id = "YOUR-PROJECT-NAME" #Change me. 
zone = "ZONE-OF-THE-CLUSTER" #Change me.
cluster_id = "NAME-OF-THE-CLUSTER" #Change me.
name = "projects/"+project_id+"/locations/"+zone+"/clusters/"+cluster_id

client = container_v1.ClusterManagerClient()

response = client.delete_cluster(name=name)

print(response)

Notice that as per the delete_cluster method documentation you only need to pass the name parameter.请注意,根据delete_cluster方法文档,您只需要传递name参数。 If by some reason you are just provided the credentials (generally in the form of a JSON file) of a service account that has enough permissions to delete the cluster you'd need to modify the client for the script and use the credentials parameter to get the client correctly authenticated in a similar fashion to:如果由于某种原因您只是获得了具有足够权限以删除集群的服务帐户的凭据(通常以 JSON 文件的形式),您需要修改脚本的客户端并使用凭据参数来获取客户端以类似的方式正确验证:

...
client = container_v1.ClusterManagerClient(credentials=credentials)
...

Where the credentials variable is pointing to the JSON filename (and path if it's not located in the folder where the script is running) of the service account credentials file with enough permissions that was provided. credentials变量指向具有足够权限的服务帐户凭据文件的 JSON 文件名(以及路径,如果它不在运行脚本的文件夹中)。

Finally notice that the response variable that is returned by the delete_cluster method is of the Operations class which can serve to monitor a long running operation in a similar fashion as to how it is explained here with the self_link attribute corresponding to the long running operation.最后请注意, delete_cluster方法返回的response变量是操作 class的操作,它可以用于监视长时间运行的操作,其方式与此处解释的方式类似,其中self_link属性对应于长时间运行的操作。

After running the script you could use a curl command in a similar fashion to:运行脚本后,您可以使用类似于以下方式的 curl 命令:

curl -X GET \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
https://container.googleapis.com/v1/projects/[RPOJECT-NUMBER]/zones/[ZONE-WHERE-THE-CLUSTER-WAS-LOCATED]/operations/operation-[OPERATION-NUMBER]

by checking the status field (which could be in RUNNING state while it is happening) of the response to that curl command.通过检查对该 curl 命令的响应的status字段(可能在 RUNNING state 中)。 Or your could also use the requests library or any equivalent to automate this checking procedure of the long running operation within your script.或者您也可以使用requests库或任何等效库来自动执行脚本中长时间运行操作的检查过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 google-cloud-container 模块在 python 中的 google 云平台中创建 kubernetes 集群 - How to create kubernetes cluster in google cloud platform in python using google-cloud-container module 如何使用 Python 的 Kubernetes 库删除节点污点 - How to delete a node taint using Python's Kubernetes library 如何使用 python 3.5 在 Google App Engine 中打开文件? - How to open a file in Google App Engine using python 3.5? 如何将 Google App Engine 更新到 Python 3.8 - How to update Google App Engine to Python 3.8 使用 python api 从 GCP 管理 Kubernetes 集群 - Managing Kubernetes cluster from GCP with python api 适用于Google App Engine的Python 3 - Python 3 for Google App Engine Google App Engine和Python - Google App Engine & Python 我需要使用 kubernetes python 客户端在 Kubernetes 集群中获取 Pod 的数量 - I need to get number of Pods in a Kubernetes Cluster with kubernetes python client 如何在 App Engine 中使用 Python3 连接到 Google Cloud Platform 中的 SQL 实例 - How to connect to SQL instance in Google Cloud Platform using Python3 in App Engine 如何使用tfrecord中的python API从谷歌地球引擎下载哨兵图像 - How to download a sentinel images from google earth engine using python API in tfrecord
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM