Hi, Let us have a quick understanding on selector operator. In many of the objects
like deployments, replica sets or even services we will be using the selector to
select any specific object using the labels and there are different
combinations we can provide to select the objects. For understanding purpose
I'm going to deploy an application and use the selector to select the pods to
understand how different ways we can use the selectors. Let me go ahead and
use a yaml file that's available as a part of kubernetes samples. Here there is a
yaml file all in one guest book where it is going to deploy 3 different services and
deployments, say for example I am going to have a service called redis master
that's going to have different labels and a deployment for redis master at the
same way I'm going to have another service for redis-slave and deployment
for redis slave and deployment for frontend, it's going to have different
labels, I'm going to use this specific yaml file and get this deployed within the
kubernetes. Let me use the command kubectl apply, then the file path you can
read directly from the github user content and automatically it's going to
create the service and deployment. Now let me go ahead and get the deployments I can
say kubectl get deployment and get services, so I should have the details
about the service and the deployments, all the deployments are in readystate the
required capacity it got deployed. Now I can use the get pods option and use the
selector. Now I am going to get all the pods kubectl get pods and I want the
labels to get displayed for each and every pods, so I'll use the option show labels. So
that's going to have the labels attached to those pods like
app guest book, app nginx, app redis, so all the labels will get displayed. Now
I am going to get the pods with what are all the labels attached to what pods. So
I'm going to list the pods with the label app tier and role, for few minutes let me
reduce the font so that I will be getting each and every item in a single row. So
the app what is the value that's used, that'll get listed over here and for tier
what is the role that is used and for role what is the value that was used. Now let
me go ahead and verify the yaml file, here I do have different labels like app
tier, role and this is what we are listing what value was assigned to that specific
pod and what is the label. So I am trying to list the value for the label app, label tier
and label role. So this is another way of listing the pods now I can go ahead and
use the selector where app is redis. So I'm selecting all the pods where the app is
redis, so it's going to get all the pods where app is redis, so I do have 3 pods
with the label app is redis. Now I can go ahead and have another combination
where app is redis and role is slave, so I do have 2 pods with app is redis and role
as slaves and I can go for the collections as well, where role in master or slave. So I'll be
having both the combinations and all the pods meeting this specific condition will
get selected. I can even go for the negative conditions where it doesn't have
the app label. so here I do have all the pods where the app label is set, now I am
going to get all the pods where app label is not set. So I am using the selector not
that is exclamatory mark and then app, so there is no resources
available in case if there were any resources that will get selected. Maybe
I can go ahead and create a sample pod and check whether that is getting
selected or not. So I am going to run a pod with a name nginx run with a image
nginx. Now let me go ahead and use the exclamatory mark where it doesn't have
a label app, of course the new pod that we created we did not set the label app
so it's getting selected for this specific condition. So the selector we will be using
it extensively to pick the pod that we wanted to act on as there will be
hundreds and hundreds of pods and other objects running within the
kubernetes cluster, whenever there is a need to pick a group of pods I will be
using this particular label to pick the pods and take the actions like deploying
them or terminating them or in case if I wanted to scale up scale down n number
of actions I can do. So basically the labels will be used to select where the
pod should run, how the pod should run, what action to be taken against what set
of pods, in lot more places we will be using it. So this is the quick summary on
how we can use the selectors what are all different types of combinations and
of course lot more combinations can be used these are all the
frequently used to combinations while using the label selector.