0
0
Kubernetesdevops~5 mins

Adding labels to resources in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Labels help you organize and find Kubernetes resources easily by attaching small pieces of information to them. Adding labels lets you group resources by purpose, environment, or any other category you want.
When you want to group all resources related to a specific app for easy management.
When you need to select resources for monitoring or updates based on their environment like development or production.
When you want to filter pods or services quickly using commands or dashboards.
When you want to apply policies or configurations only to certain labeled resources.
When you want to track versions or releases of your deployments.
Config File - pod.yaml
pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: my-app
    environment: production
spec:
  containers:
  - name: nginx-container
    image: nginx:1.23.3
    ports:
    - containerPort: 80

This YAML file defines a Pod named my-pod with two labels: app: my-app and environment: production. Labels are under metadata.labels and help identify and group this Pod.

Commands
This command creates the Pod with the labels defined in the YAML file. Applying the file tells Kubernetes to create or update the resource accordingly.
Terminal
kubectl apply -f pod.yaml
Expected OutputExpected
pod/my-pod created
This command lists all Pods and shows their labels so you can verify the labels were added correctly.
Terminal
kubectl get pods --show-labels
Expected OutputExpected
NAME READY STATUS RESTARTS AGE LABELS my-pod 1/1 Running 0 10s app=my-app,environment=production
--show-labels - Displays labels of each resource in the list
This command adds a new label called version with value v1 to the existing Pod without changing other labels.
Terminal
kubectl label pod my-pod version=v1
Expected OutputExpected
pod/my-pod labeled
This command shows the Pod details including all labels after adding the new label to confirm the change.
Terminal
kubectl get pod my-pod --show-labels
Expected OutputExpected
NAME READY STATUS RESTARTS AGE LABELS my-pod 1/1 Running 0 30s app=my-app,environment=production,version=v1
--show-labels - Displays labels of the resource
Key Concept

If you remember nothing else from this pattern, remember: labels are key-value pairs attached to resources to organize and select them easily.

Common Mistakes
Trying to add labels using 'kubectl label' without specifying the resource type.
Kubernetes needs to know what kind of resource you want to label, otherwise the command fails.
Always specify the resource type, for example 'kubectl label pod my-pod version=v1'.
Overwriting existing labels by reapplying a YAML without including all labels.
Applying a YAML file replaces the labels section, so missing labels get removed.
Include all desired labels in the YAML before applying or use 'kubectl label' to add labels incrementally.
Summary
Create or update resources with labels defined under metadata.labels in YAML files.
Use 'kubectl get' with '--show-labels' to view labels on resources.
Add or update labels on existing resources using 'kubectl label' with resource type and name.