Kubernetes Engine API delete pod

8/22/2019

I need to delete POD on my GCP kubernetes cluster. Actually in Kubernetes Engine API documentation I can find only REST api's for: projects.locations.clusters.nodePools, but nothing for PODs.

-- Ted
google-cloud-platform
google-kubernetes-engine
kubernetes
terraform-provider-gcp

1 Answer

8/22/2019

The GKE API is used to manage the cluster itself on an infrastructure level. To manage Kubernetes resources, you'd have to use the Kubernetes API. There are clients for various languages, but of course you can also directly call the API.

Deleting a Pod from within another or the same Pod:

PODNAME=ubuntu-xxxxxxxxxx-xxxx
curl https://kubernetes/api/v1/namespaces/default/pods/$PODNAME \
  -X DELETE -k \
  -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)"

From outside, you'd have to use the public Kubernetes API server URL and a valid token. Here's how you get those using kubectl:

APISERVER=$(kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}')
TOKEN=$(kubectl get secret $(kubectl get serviceaccount default -o jsonpath='{.secrets[0].name}') -o jsonpath='{.data.token}' | base64 --decode )

Here's more official information on accessing the Kubernetes API server.

-- Markus Dresch
Source: StackOverflow