The kubectl label
command in Kubernetes is used to update the labels on resources within the cluster. Labels are key-value pairs attached to Kubernetes objects such as pods, services, and deployments, enabling easier identification and management of these resources.
Example 1: Label a Pod
To add a label “env=prod” to a pod named “nginx”:
kubectl label pod nginx env=prod
This command assigns the label “env=prod” to the pod named “nginx”.
Verification Steps: To verify, use:
kubectl get pod nginx --show-labels
Output should show the pod’s details including the labels.
Example 2: Update Labels of a Service
Modify labels of a service named “frontend” by adding “tier=frontend”:
kubectl label service frontend tier=frontend --overwrite
The --overwrite
flag updates the label “tier” to “frontend” for the service.
Verification Steps: Verify the labels with:
kubectl get svc frontend --show-labels
Check if “tier=frontend” appears among the service’s labels.
Example 3: Remove a Label from Deployment
Remove the label “env” from a deployment named “webapp”:
kubectl label deployment webapp env-
The label “env” is removed from the “webapp” deployment.
Verification Steps: Confirm the label removal with:
kubectl get deployment webapp --show-labels
Ensure the label “env” no longer appears.
Example 4: Label Nodes
Add label “special=true” to a node named “node-1”:
kubectl label node node-1 special=true
This assigns the label “special=true” to the specified node.
Verification Steps: Check the node’s labels using:
kubectl get node node-1 --show-labels
Verify if “special=true” is listed under the node’s labels.
Example 5: Label Namespace
Label the namespace “development” with “environment=dev”:
kubectl label namespace development environment=dev
This command labels the namespace “development” for easier organization.
Verification Steps: Ensure the label is applied with:
kubectl get namespace development --show-labels
Confirm if “environment=dev” is displayed for the namespace.
Example 6: Label PersistentVolume
Label a PersistentVolume named “pv-1” with “type=ssd”:
kubectl label pv pv-1 type=ssd
This assigns the label “type=ssd” to the PersistentVolume “pv-1”.
Verification Steps: Check the labels with:
kubectl get pv pv-1 --show-labels
Ensure “type=ssd” is listed under the PersistentVolume’s labels.
Example 7: Labeling Deployments with Multiple Labels
Add multiple labels to a deployment named “api”:
kubectl label deployment api app=api environment=production
This command adds labels “app=api” and “environment=production” to the deployment.
Verification Steps: Verify the labels applied with:
kubectl get deployment api --show-labels
Check if both labels appear in the deployment’s label list.
Example 8: Labeling Service Account
Label a service account named “default” with “team=frontend”:
kubectl label serviceaccount default team=frontend
This assigns the label “team=frontend” to the service account “default”.
Verification Steps: Confirm the label addition with:
kubectl get serviceaccount default --show-labels
Ensure “team=frontend” is among the service account’s labels.
Example 9: Labeling StatefulSet
Label a StatefulSet named “mysql” with “app=mysql”:
kubectl label statefulset mysql app=mysql
This assigns the label “app=mysql” to the StatefulSet “mysql”.
Verification Steps: Check the labels with:
kubectl get statefulset mysql --show-labels
Ensure “app=mysql” is listed under the StatefulSet’s labels.
Example 10: Labeling Ingress
Add a label “app=nginx-ingress” to an Ingress named “nginx-ingress”:
kubectl label ingress nginx-ingress app=nginx-ingress
This assigns the label “app=nginx-ingress” to the Ingress resource.
Verification Steps: Verify the label with:
kubectl get ingress nginx-ingress --show-labels
Check if “app=nginx-ingress” appears in the Ingress’s labels.
Also check similar articles.
Building Kubernetes Customizations with kubectl kustomize
Waiting for Kubernetes Resources with kubectl wait
Replace Kubernetes Resources with kubectl replace
Patching Kubernetes Resources with kubectl patch
Applying Kubernetes Configurations with kubectl apply
Discussion about this post