Skip to Content
This project is a work in progress. If you have any questions or suggestions, feel free to contact me.
KubernetesCluster ManagementLabels and Selectors

Kubernetes Labels and Selectors

Labels

  • Labels are kind of tags that can be assigned to any object in kubernetes.
  • A label of the mechanism you use to organize Kubernetes object.
  • A label is a key value pair without any predefined meaning that can be attached to the object
  • Labels are similar to tags in AWS or git where you use a name for quick reference
  • So you are free to choose labels as you need it to refer an environment which is used for dev or testing or production, refer a product group like Department A department B etc.
  • Multiple labels can be added to a single object.
  • Unlike name or UID’s, labels do not provide uniqueness, as in general, we can expect many objects to carry the same label.

Label Selectors

  • Selectors are the way to filter objects using labels also called label selectors.
  • The API currently support two types of selectors that is equality based and set based
  • A label selector can be made of multiple requirements which are comma separated
Type of selectorOperatorExample
Equality based=,!=name=example
Set basednotin, in, existenv in (production, dev)

More examples of set-based selectors i.e., match multiple values

kubectl get pods -l 'env in (development, testing)'
kubectl get pods -l 'env notin (development, testing)'
kubectl get pods -l class=pods,env=testing

Example of labels

  • Labels are always written in metadata
k8s_example_5.yml
kind: Pod apiVersion: v1 metadata: name: example-5 labels: env: development class: pods spec: containers: - name: c00 image: ubuntu command: [ "/bin/bash", "-c", "while true; do echo The hostname is $(hostname); sleep 5; done", ]

Commands

Create pod

kubectl apply -f k8s_example_5.yml
Output
controlplane $ kubectl apply -f k8s_example_5.yml pod/example-5 created

Adding a label to an existing pod

kubectl label pods example-5 group=example

Here example-5 is a pod name. group=example is the label

keyvalueDescription
groupexampleHere in the label it is in form of Key-Pair
Output
controlplane $ kubectl label pods example-5 group=example pod/example-5 labeled

Show labels

kubectl get pods --show-labels
Output
controlplane $ kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS example-5 1/1 Running 0 46s class=pods,env=development,group=example example-6 2/2 Running 0 21m class=pods,env=development example-7 2/2 Running 0 36m class=pods,env=development

Filter pods using labels

kubectl get pods -l group=example kubectl get pods -l group!=example
Output
controlplane $ kubectl get pods -l group=example NAME READY STATUS RESTARTS AGE example-5 1/1 Running 0 24m controlplane $ kubectl get pods -l group!=example NAME READY STATUS RESTARTS AGE example-6 2/2 Running 0 29m example-7 2/2 Running 0 32m

Delete pods using labels

kubectl delete pods -l group=example
Output
controlplane $ kubectl delete pods -l group=example pod "example-5" deleted
Last updated on