- Next up, let's talk about how we can search by labels.
In order to do that, let me create some
sample infrastructure for us to run.
I'm going to do this with kubectl create -f
sample-infrastructure-with-labels.yml.
All this is doing behind the scenes
is creating a bunch of pods
that have labels associated with them.
So I'm going to clear screen, and
do kubectl get pods.
We have some pods
and this example is a large e-commerce store
that has different teams associated with
different applications.
For example, there's a cart application,
there's a catalog, there's a homepage
and a login application, ordering, et cetera.
And it also has different tiers, like dev,
production, staging.
Let's take a look again to see if the pods are running.
It looks like all the pods are up and running.
Next, let's take a look at the labels associated with them.
Get pods...
Great.
As you can see, we have a lot of labels
associated with the application.
What I'm going to do is,
I'm going to reduce my font a little bit.
Once again, we notice that we have a lot of labels,
and we've seen how we can list labels,
but let's figure out how we can search for them.
In order to do label searches, we want to use selectors
with labels to do this.
So clear my screen,
and we are going to look for all of our labels
that are in the production environment.
In order to do this,
I would do a kubectl get pods, just like before.
I'm going to add a selector,
and I'm going to put env=production.
And our list of pods has drastically reduced
and all of the pods returned are pods
that we have running in production.
In fact, I'm going to do show-labels here,
and as you can tell, we have a cart-prod,
and we have an env in production.
And if I search for this, you'll notice that
all of my labels are associated with an application
that's been tagged to run in a production-like setting.
All right, let's do another example.
Let's take a look at what we have.
Let's search for somebody who's a dev-lead,
'cause a lot of times, different developers
are associated with applications.
So, kubectl
get pods minus-minus selector
and dev-lead, I'm going to search for Carisa.
So, in this scenario, we have four applications
where we have a dev-lead and her name is Carisa.
That's great.
What if we wanted to do something more complicated
where we want to add multiple labels
and search by multiple labels?
We can do kubectl get pods minus-minus selector,
dev-lead equals, I'm going to pick Karthik,
and then I'm also going to do an env as staging.
So, we notice in this scenario
out of all the applications that we had,
we have one application called homepage-staging
where we've tagged it with a dev-lead of Karthik,
and it's running in a specific environment of staging.
So this command,
kubectl get pods --selector,
with dev-lead Karthik comma env staging
allows you to add multiple labels
and search by multiple labels to filter down
the pods that we have running.
In our example, we've talked about an application
with a label, dev-lead Karthik in an environment staging.
What if we wanted to search for something
that was not owned by Karthik?
We can do this by adding the not flag.
So, this becomes dev-lead not equals Karthik
in the staging environment.
The result of this is
all of the applications that are not owned by Karthik
in the staging environment.
In fact, let's look at all the labels associated with it.
Dev-lead Carisa, Daniel, Jim, Chen, Amy
and Marketing.
Great.
Next up, we want to actually learn
how to use the in operator.
So, we do a kubectl get pods,
and instead of minus-minus selector,
you can use the minus-l shortcut.
And we're going to look for release-versions
in, let's say,
1.0
to 2.0.
This command will basically look for
anything that has a release-version
that's between 1.0 and 2.0.
As we run that, we get a list of
different applications, or pods,
that are running that have a release-version
between 1.0 and 2.0.
Show-labels.
To verify that, release-version 1.0, 2.0,
looks good.
What if we wanted to do
something that's not in 1.0 or 2.0?
Here we can use the notin operator,
and I'm going to use show-labels as well.
So, here we see some applications,
catalog-dev, for example,
that's in a release-version of 4.0, 12.0
and so on and so forth.
Sometimes we want to also be able to delete by labels.
How do we go about doing that?
So, we have pods...
And we have a lot of them,
and we might want to delete
all of the pods associated with a specific dev-lead.
So, let's take the example of
ctrl-C, minus-minus
show-labels.
Let's pick our favorite dev-lead, Karthik,
and delete all the pods associated with him.
In order to do this, do a kubectl
delete pods
minus-l,
dev-lead
equals Karthik.
This goes ahead and deletes all of the pods
that were associated with a specific dev-lead named Karthik.
So, if we do a show-labels again,
we'll notice that, in our dev-lead column,
we have some pods, homepage-dev, homepage-prod
and homepage-staging that are in their terminating state,
that have a dev-lead of Karthik associated with them.
And we do this again...
There we go.
We just have dev-leads of everybody except Karthik.
So, in this scenario, we've deleted all of our pods
where the dev-lead was Karthik.
And there you have it.
That's an example of how to use labels
to search through your infrastructure.
One of the nice things about labels,
even though we don't have it in this specific example,
is, we've used labels with pods in this example.
However, you can also use labels with deployments,
services, replica sets, et cetera.
So everything I've just shown you
can also be transposed to those constructs
for services and deployments.