Kubectl Label Command Usage in Kubernetes

Itacen Sabacok | May 14, 2022

A label key and value must beging 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.

 1# syntax
 2kubectl label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]
 3
 4# Update pod 'foo' with the label 'status' and the value 'unhealthy', overwriting any existing value
 5kubectl label --overwrite pods foo status=unhealthy
 6
 7# Update a pod identified by the type and name in "pod.json"
 8kubectl label -f pod.json status=unhealthy
 9
10# Update pod 'foo' only if the resource is unchanged from version 1
11kubectl label pods foo status=unhealthy --resource-version=1
12
13# Update pod 'foo' with the label 'unhealthy' and the value 'true'
14kubectl label pods foo unhealthy=true