label

Update the labels on a resource.

  • A label key and value must begin with a letter or number, and may contain letters, numbers, hyphens, dots, and underscores, up to 63 characters each.
  • Optionally, the key can begin with a DNS subdomain prefix and a single ‘/’, like example.com/my-app
  • If –overwrite is true, then existing labels can be overwritten, otherwise attempting to overwrite a label will result in an error.
  • If –resource-version is specified, then updates will use this resource version, otherwise the existing resource-version will be used.

Command

  1. $ kubectl label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]

Example

Current Status

  1. $ kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. nginx-6db489d4b7-b5nsn 1/1 Running 0 16m
  4. nginx-6db489d4b7-vdhvz 1/1 Running 0 15m

Command

  1. kubectl label pods nginx-6db489d4b7-b5nsn unhealthy=true

Output

  1. $ kubectl describe pods nginx-6db489d4b7-b5nsn
  2. Name: nginx-6db489d4b7-b5nsn
  3. Namespace: default
  4. Priority: 0
  5. Node: minikube/172.17.0.32
  6. Start Time: Sun, 20 Sep 2020 15:09:09 +0000
  7. Labels: pod-template-hash=6db489d4b7
  8. run=nginx
  9. unhealthy=true
  10. Annotations: <none>
  11. Status: Running
  12. IP: 172.18.0.6
  13. ...
  14. Events:
  15. Type Reason Age From Message
  16. - - - -
  17. Normal Scheduled 15m default-scheduler Successfully assigned default/nginx-6db489d4b7-b5nsn to minikube
  18. Normal Pulling 15m kubelet, minikube Pulling image "nginx"
  19. Normal Pulled 14m kubelet, minikube Successfully pulled image "nginx"
  20. Normal Created 14m kubelet, minikube Created container nginx
  21. Normal Started 14m kubelet, minikube Started container nginx

Notice that the labels has unhealthy=true as a last entry.

More Examples

  1. # Update pod 'foo' with the label 'unhealthy' and the value 'true'.
  2. kubectl label pods foo unhealthy=true
  1. # Update pod 'foo' with the label 'status' and the value 'unhealthy', overwriting any existing value.
  2. kubectl label --overwrite pods foo status=unhealthy
  1. # Update all pods in the namespace
  2. kubectl label pods --all status=unhealthy
  1. # Update a pod identified by the type and name in "pod.json"
  2. kubectl label -f pod.json status=unhealthy
  1. # Update pod 'foo' only if the resource is unchanged from version 1.
  2. kubectl label pods foo status=unhealthy --resource-version=1
  1. # Update pod 'foo' by removing a label named 'bar' if it exists.
  2. # Does not require the --overwrite flag.
  3. kubectl label pods foo bar-