In the previous post we saw what labels are and what are the best practices to select them. In this post we will deploy pods with a variety of labels and then look at how to select them in a YAML and through the kubectl tool using Kubernetes Selector.
What is a Kubernetes selector
Kubernetes selector allows you to select an object by using its label by matching label against the label name or an operator that acts on the label name.
Kubernetes selector example
Here’s the yaml file – The first object is a pod. It has 3 labels – name, stage and owner. The labels combine to tell us that it is a kubernetes-static-website owned by the digital team and in a canary stage. Canary deployment is where you release an application to a subset of users or servers.
The second pod is the kuberentes-static-website owned by the analytics team and in a production stage. The purpose of labeling the pods will be clear now. Lets say you want to target a service to a particular pod. To do that you will specify a selector that helps you select the required pod. In this case we use the selector to select the pod owned by the digital team and in canary stage. We will now use the kubectl tool to create the resources.
kubectl.exe apply -f labels_demo.yml
The selectors where specified as a map. There are certain resources that allow specifying a selector as a set as well. That is, it allows us to select based on values in a set. The resources that support these are Job, Deployment, Replica Set and DaemonSet. Lets look at an example of a ReplicaSet to see how it works.
The file labels_replicaset.yml contains a replicaset. It specifies a template with stage canary and owner analytics. We use two different kinds of selectors to select pods that are targeted by this ReplicaSet. In MatchLabels we specify key value pairs of labels that we want to use to target the pods and in MatchExpression we use a key, a value and an operator. The operator could be In, NotIn, Exists and DoesNotExist. In our example we use the name in MatchLabels and in the MatchExpression we tell Kubernetes to target pods that contains production or canary as stage and whose owner is not in digital. The clauses in MatchLabels and MatchExpression are Anded together
When we create the ReplicaSet using this YAML, it creates only one pod and not two. What’s going on? If you look at the MatchExpression again you would notice that not only does it select the pod specified in the template but it also selects the pod that we created earlier. i.e the ReplicaSet selects the production-analytics pod that we created earlier. The ReplicaSet includes all pods that matches the selectors and not just the ones that it has created.
We now quickly look at some example of how to use the selectors in the kubectl tool. To get all pods in canary stage type in
kubectl get pods -l "stage=canary"
-l is used the specify selectors. To get all pods in canary whose owner is not digital
kubectl get pods -l "stage=canary,owner notin (digital)"
To get all pods in either canary or production and owned by digital
kubectl get pods -l "stage in (canary,production), owner=digital"
This finishes our post on labels and selectors , in the next post, which is also the last post of this section, we will look at Controllers.