Labels:
Labels are used to organize Kubernetes Objects such as Pods, nodes, etc.
You can add multiple labels over the Kubernetes Objects.
Labels are defined in key-value pairs.
Labels are similar to tags in AWS or Azure where you give a name to filter the resources quickly.
You can add labels like environment, department or anything else according to you.
Label-Selectors:
Once the labels are attached to the Kubernetes objects those objects will be filtered out with the help of labels-Selectors known as Selectors.
The API currently supports two types of label-selectors equality-based and set-based. Label selectors can be made of multiple selectors that are comma-separated.
Node Selector:
Node selector means selecting the nodes. Node selector is used to choose the node to apply a particular command.
This is done by Labels where in the manifest file, we mentioned the node label name. While running the manifest file, master nodes find the node that has the same label and create the pod on that container.
Make sure that the node must have the label. If the node doesn’t have any label then, the manifest file will jump to the next node.
Example of labelling a pod:
apiVersion: v1
kind: Pod
metadata:
name: pod-1
labels:
env: testing
department: DevOps
spec:
containers:
- name: containers1
image: ubuntu
ports:
- containerPort: 80
To see the list of pods with labels:
kubectl get pods --show-labels
Now, I want to list those pods that have the label env=testing.
kubectl get pods -l env=testing
kubectl get pods -l department!=DevOps
As we have discussed earlier, there are two types of label-selectors equality and set-based.
This is the example of equality based where we used equalsTo(=).
Now, Suppose I forgot to add the label through the declarative(via manifest) method. So, I can add labels after creating the pods as well which is known as the imperative(via command) method.
kubectl label pods Pod_name Location=India
As we discussed the type of label-selector, let’s see the example of a set-based label-selector where we use in, notin, and exists.
Lets list all those pods that have an env label with value for either testing or development.
kubectl get pods -l ‘env in (testing, development)’
we are trying to list all those pods that don’t have the India or US value for the Location key in the Label.
kubectl get pods -l ‘Location not in (India, US)’
We can also delete the pods as per the label.
Let’s deleted all those pods that don’t have the China value for the location key in the Label.
kubectl delete pod -l Location!=China
Example of NodeSelector:
First add a label to worker-node
kubectl label node <node-name> node-id=node1
And schedule a pod on worker node based on nodeid
apiVersion: v1
kind: Pod
metadata:
name: my-pod
labels:
app: my-app
spec:
containers:
- name: cont-1
image: shaikmustafa/dm
ports:
- containerPort: 80
nodeSelector:
node-id: node1
conclusion:
In conclusion, labels, nodeSelectors, and selectors are foundational components in Kubernetes that enable efficient resource organization, targeted scheduling, and precise workload management. Labels provide a flexible way to tag resources, while nodeSelectors and selectors allow you to define where workloads should run and how resources interact. Mastering these concepts is essential for optimizing cluster performance and maintaining scalability in Kubernetes environments.
If you enjoy stories that help you learn, live, and work better, consider subscribing. If this article provided you with value, please support my work — only if you can afford it. You can also connect with me on Linkedin. Thank you!