kubectl 1.37 reference · Workloads

kubectl label Command (kubectl)

Add, change or remove labels on resources; labels drive selectors for Services, Deployments, network policies and queries.

kubectl 1.37

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

kubectl label RESOURCE NAME KEY=VALUE [KEY-] [--overwrite]

Parameters

ParameterDescription
KEY=VALUE Add a label.
KEY- Remove a label.
--overwrite Allow changing an existing value.

Examples

Add labels and select by them
kubectl create namespace rc-ref-label > /dev/null
trap 'kubectl delete namespace rc-ref-label --wait=false > /dev/null' EXIT
kubectl -n rc-ref-label create configmap app --from-literal=x=1 > /dev/null
kubectl -n rc-ref-label label configmap app team=payments tier=backend
kubectl -n rc-ref-label get configmap -l team=payments -o name

Output

configmap/app labeled
configmap/app

Run this on your computer — see “Get Started” for setup.

Overwrite and remove
kubectl create namespace rc-ref-label2 > /dev/null
trap 'kubectl delete namespace rc-ref-label2 --wait=false > /dev/null' EXIT
kubectl -n rc-ref-label2 create configmap app --from-literal=x=1 > /dev/null
kubectl -n rc-ref-label2 label configmap app env=dev > /dev/null
kubectl -n rc-ref-label2 label configmap app env=prod 2>&1 | sed 's/^error: //' | cut -c1-70
kubectl -n rc-ref-label2 label configmap app env=prod --overwrite > /dev/null
kubectl -n rc-ref-label2 label configmap app env-
kubectl -n rc-ref-label2 get configmap app -o jsonpath='labels: {.metadata.labels}{"\n"}'

Output

'env' already has a value (dev), and --overwrite is false
configmap/app unlabeled
labels: 

Run this on your computer — see “Get Started” for setup.

Was this page helpful?