简体   繁体   中英

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

I'm new to GKE-Python. I would like to delete my GKE(Google Kubernetes Engine) cluster using a python script. I found an API delete_cluster() from the google-cloud-container python library to delete the GKE cluster. 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. Can anyone explain me with an example?

Or else If there is any other way to delete the GKE cluster in python?

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. Basically, set up a virtual environment and install the library with 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:

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. 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:

...
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.

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.

After running the script you could use a curl command in a similar fashion to:

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. Or your could also use the requests library or any equivalent to automate this checking procedure of the long running operation within your script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM