Labels
Video Tutorial
Overview of Labels
As resource volumes increase, efficient classification and management become essential. Kubernetes provides labels to help you manage almost all resources easily.
Labels are key-value pairs that can be set during or after resource creation and modified at any time as needed.
The following figures show how labels work. Managing multiple pods of various types without classification can be challenging.

Labeling these pods makes management much clearer.

Adding a Label
The following example shows how to add labels app=nginx and env=prod during pod creation:
apiVersion: v1 kind: Pod metadata: name: nginx labels: # Add two labels to the pod. app: nginx env: prod spec: containers: - image: nginx:alpine name: container-0 resources: limits: cpu: 100m memory: 200Mi requests: cpu: 100m memory: 200Mi imagePullSecrets: - name: default-secret
After labeling a pod, you can view its labels by adding --show-labels to the pod query command.
$ kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 0 50s app=nginx,env=prod
You can also use -L to query specific labels.
$ kubectl get pod -L app,env NAME READY STATUS RESTARTS AGE APP ENV nginx 1/1 Running 0 1m nginx prod
For existing pods, you can run kubectl label to add labels to them.
$ kubectl label pod nginx creation_method=manual pod/nginx labeled $ kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 0 50s app=nginx, creation_method=manual,env=prod
Modifying a Label
To modify a label, add --overwrite to the command for managing labels.
$ kubectl label pod nginx env=debug --overwrite pod/nginx labeled $ kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 0 50s app=nginx,creation_method=manual,env=debug
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.