Set-based label selector

With the set-based selector, you can use either the in or notin operator, which is similar to the SQL IN clause that can specify multiple keywords, as in the following examples:

  • Query Pods which belong to the pilot project:
$ kubectl get pods -l "project in (pilot)"
NAME READY STATUS RESTARTS AGE
pilot.dev.memcached 1/1 Running 0 36m
pilot.dev.nginx 1/1 Running 0 36m
pilot.prod.memcached 1/1 Running 0 36m
pilot.prod.nginx 1/1 Running 0 36m

  • Query Pods which belong to the pilot project and frontend tier:
$ kubectl get pods -l "project in (pilot), tier in (frontend)"
NAME READY STATUS RESTARTS AGE
pilot.dev.nginx 1/1 Running 0 37m
pilot.prod.nginx 1/1 Running 0 37m
  • Query Pods which belong to the pilot project and either the frontend or cache tier:
$ kubectl get pods -l "project in (pilot), tier in (frontend,cache)"
NAME READY STATUS RESTARTS AGE
pilot.dev.memcached 1/1 Running 0 37m
pilot.dev.nginx 1/1 Running 0 37m
pilot.prod.memcached 1/1 Running 0 37m
pilot.prod.nginx 1/1 Running 0 37m
  • Query Pods which belong to the pilot project and not the frontend or backend tier (note, we didn't create the backend tier object):
$ kubectl get pods -l "project in (pilot), tier notin (frontend, backend)"
NAME READY STATUS RESTARTS AGE
pilot.dev.memcached 1/1 Running 0 50m
pilot.prod.memcached 1/1 Running 0 50m

As you can see in the preceding examples for both the equality-based and set-based label selector, equality-based is simpler and set-based is more expressive. Note that you can mix both operator as follows:

  • Query Pods which do not belong to the pilot project and develop environment:
$ kubectl get pods -l "project notin (pilot), environment=develop"
NAME READY STATUS RESTARTS AGE
poc.dev.httpd 1/1 Running 0 2m
poc.dev.memcached 1/1 Running 0 2m

So, you can use the most efficient way to filter out the Kubernetes objects. In addition, you can also use either or both types of selectors to configure the Kubernetes Service, Deployments, and so on. However, some objects support the equality-based selector and some objects support both. So, let's take a look at how to define it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.28.80