Labels and Selectors in Kubernetes

Labels and Selectors in Kubernetes

Labels

  1. Label are the mechanism you use to organize Kubernetes object.

  2. A Label is a key value pair without any pre-defined meaning that can be attached to an object.

  3. Labels are similar to tags in AWS or git where you use a name to pick references.

  4. So you are free to choose a label as you need it to refer an environment which is used for development or testing or production refer a product group like Department A, or Department B.

  5. Multiple labels can added to a single object.

You can also perform practical based upon this giving code.
Firstly you get start minikube and then check the "minikube status".
Create a pod and apply by "kubectl apply -f pod5.yml"

kind: Pod
apiVersion: v1
metadata:
  name: delhipod
  labels:                                                   
    env: development
    class: pods
spec:
    containers:
       - name: c00
         image: ubuntu
         command: ["/bin/bash", "-c", "while true; do echo Hello-Sidharth; sleep 5 ; done"]
kubectl apply -f pod5.yml
kubectl get pods --show-labels

Now if you want to add a label to an Existing pod.

kubectl label pods delhipos myname=sidharth

for cheking the labels you can see by this command

kubectl get pods --show-labels

Now list pods matching a label

kubectl get pods -l env=development

Now give a list where the 'development' label is not present

kubectl get pods -l env!=development

for cheking the list of pods

kubectl get pods

Label selectors

Unlike name/UIDs, label do not provvide uniqueness, as in general , we can expect many object to carry the same label.
Once label are attached to an object , we would need to filter to narrow down and these are called as label selectors.
The API currently support two type of selectors- Equality based and Set Based .
A label selectors can be made of multiple requirements which are comma - seperated.

Equality based:(=, !=)

Set based:(in, notin and exits)

some comman commands are use there are as follows:-
kubectl get pods -l 'env in (development, testing)'
kubectl get pods -l 'env notin (development, testing)'
kubectl get pods -l class=pods, myname=sidharth

for cheking pod IP

kubectl get pod -o wide

for checking labels

kubectl get pods --show-labels

for adding labels

kubectl label pods delhipod myname=sidharth

getting pod by label name

kubectl get pods -l env=development

delete pod by label name

kubectl delete pod -l env!=development

searching by label

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

you can also search as:-

kubectl get pods -l class=pods

you can also delete pod by label

kubectl delete -l 'env in (development)'

Node selector

  1. One use can for selecting labels is to contrain the set of nodes onto which a pod can schedule ie. you can tell a pod to only be able to run on particular Nodes.

  2. Generally such constraints are unnecessary , as the scheduler will automatically do a reasonable placement , bot on certain circumstances we might need it.

  3. we can use label to tag nodes.

  4. The node are tagged, you can use the label selectors to specify the pods run only of specific nodes.

  5. First we give label to the nodes and then use node selector to the pod configuration.

Scaling and Replication

Kubernetes was designed to orchestrate multiple container and replication.
Need for multiple containers/replication helps us with these:-

Reliability

By having multiple version of an application you present problems if one or more fails.

Load balancing

Having multiple version of container enables you to easily send traffic to different instances to prevent overloading of a single instance or node.

Scaling

When load does become too much for the number of existing instance, kubernetes enable you to easily scale up your application , adding additional instances as needed.

Rolling updates

Update to a service by replacing Pods one by one.

Replication controller

A replication controller is a object that enables you to easily create multiple pods, then make sure that number of pods always exist.
If a pod created using RC will be automatically replaced if they does crash, failed or terminated.
Replication control is recommanded if you just want to make sure 1 pod is always running then after system reboot.
You can run the RC with 1 replica & the RC will make sure that Pod is always running

yaml code of Replication controller

kind: ReplicationController               
apiVersion: v1
metadata:
  name: myreplica
spec:
  replicas: 2            
  selector:        
    myname: sidharth                            
  template:                
    metadata:
      name: testpod6
      labels:            
        myname: sidharth
    spec:
     containers:
       - name: c00
         image: ubuntu
         command: ["/bin/bash", "-c", "while true; do echo Hello-Bhupinder; sleep 5 ; done"]

you can create pods by following commands also do some experiments on it:-

kubectl apply -f filename.yml

for geting replicas

kubectl get rc

for getting details

kubectl describe rc myreplica

for getting labels

kubectl get pods --show-labels

for scaling up and down

for scaling

kubectl scale --replicas=8 rc -l myname=sidharth

for scale down

kubectl scale --replicas=1 ec -l myname=sidharth

for checking

kubectl get rc

for deleteing

kubectl delete filename.yml

Replica set

  1. Replica set is next generation replication controller

  2. The replication controller only supports equality based selectors where as the replica set support set based selectors ie. filtering according to set of values.

  3. Replication set rather then a replication controller is what by other object like deployment.

yaml code

kind: ReplicaSet                                    
apiVersion: apps/v1                            
metadata:
  name: myrs
spec:
  replicas: 2  
  selector:                  
    matchExpressions:                             # these must match the labels
      - {key: myname, operator: In, values: [Bhupinder, Bupinder, Bhopendra]}
      - {key: env, operator: NotIn, values: [production]}
  template:      
    metadata:
      name: testpod7
      labels:              
        myname: Bhupinder
    spec:
     containers:
       - name: c00
         image: ubuntu
         command: ["/bin/bash", "-c", "while true; do echo Technical-Guftgu; sleep 5 ; done"]

you can do some experiments by following command:-

kubectl apply -f filename.yml
kubectl get pods
kubectl scale --replicas=1 rs/myrs
kubectl get pods
kubectl get rs
kubectl delete pod myrs
kubectl get rs
kubectl get pods.

Conclusion

we get familier with label and selectors, you can use chatgpt for doing some more experiments. If you like it then share with others.