Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a label for a kubernetes pod

I want delete label from a node or a pod by kubernetes API, my kubernetes version:1.24

kubectl get pod --show-labels | grep all-flow-0fbah
all-flow-0fbah      1/1     Running   2        9d      app=all-flow,op=vps1,version=1001

I use command as below:

 curl --request PATCH --header "Content-Type:application/json-patch+json" http://10.93.78.11:8080/api/v1/namespaces/default/pods/all-flow-0fbah --data '{"metadata":{"labels":{"a":"b"}}}'

But this doesn't work. Return message as below:

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "the server responded with the status code 415 but did not return more information",
  "details": {},
  "code": 415
}

Then I change the curl header like this :

curl --request PATCH --header "Content-Type:application/merge-patch+json" http://10.93.78.11:8080/api/v1/namespaces/default/pods/all-flow-0fbah --data '{"meadata":{"labels":{"op":"vps3"}}}'

It not delete label but add a new label to that pod. So is there any one can tell me how to delete a label for a pod like use command :

kubectl label pod all-flow-0fbah key-

Thanks!

like image 667
workhardcc Avatar asked May 27 '16 10:05

workhardcc


People also ask

How do I change the Pod label in Kubernetes?

You can change the labels on individual pods using the kubectl label command, documented here. Changing the label of a running pod should not cause it to be restarted, and services will automatically detect and handle label changes. Save this answer.

How do I look up a POD label?

To list the pods with label key “owner” and value “ahmad”, we will use the --selector option. Next, use the short option -l to select the pod with label env=develop. Kubernetes labels are not only for pods. You can apply them to all sorts of objects, including nodes, services, and deployments.

How do I remove objects from Kubernetes?

You can use the delete command to delete an object from a cluster: delete <type>/<name>


1 Answers

Was looking for CLI command myself. Here is what worked for me:

kubectl patch pod <podname> --type=json -p='[{"op": "remove", "path": "/metadata/labels/somelabelkey"}]'
like image 106
user2174835 Avatar answered Oct 01 '22 06:10

user2174835