Updated on 2025-07-29 GMT+08:00

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.

Figure 1 Unclassified pods

Labeling these pods makes management much clearer.

Figure 2 Pods classified using labels

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