Kubernetes: Usage and Understanding of Kubernetes Labels, MatchLabels, and Selectors. | Part 6

Sarasa Gunawardhana
Nov 8 · 4 min read

Here I’m going to mainly talk about the usage of Labels, MatchLabels, and Selectors and how pod, service, and deployment are going to use them. After you understand this, you will know how Kubernetes objects such asJob , Deployment, Replica Set, and Daemon Set use these concepts.

The main reason for Creating Labels is like an identifier. if we add a label to the pod, then other Kubernetes objects (Ex: Service, DaemonSet) can communicate with the pod by only mentioning the pod’s label under Selector.

Labels are key/value pairs that are attached to objects, such as pods. Labels are intended to be used to specify identifying attributes of objects that are meaningful and relevant to users.

Labels can be attached to objects at creation time and subsequently added and modified at any time

Each object can have a set of key/value labels defined. Each Key must be unique for a given object.

"metadata": {
"labels": {
"key1" : "value1",
"key2" : "value2"
}
}

You can create Labels from the configuration file or command line later after pod or deployment is created.

Define labels on Pod and Deployment

creating labels on pod and deployment are the same.

pod.yaml

apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx

deployment.yaml

here we define the label for deployment, not for the Pod, we can define Label for the pod in deployment under template

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx

Define Label on the pod in Deployment’s configuration file

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx

Note: You can’t see matchLabels in the service’s configuration file. because matchLabels is not supported yet by the service. in the service, you just can add labels you want.

Assume that you are going to deploy a POD as a deployment like below.

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx

Here is you can see selector tag, which is used by deployment to talk with its pods. The selector field defines how the Deployment finds which Pods to manage. In this case, you simply select a label that is defined in the Pod template (app: nginx). However, more sophisticated selection rules are possible, as long as the Pod template itself satisfies the rule.

Define service and its communication with pods

apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: nginx

Here you can see, service has connected with a pod from the selector. the selector:tell the resource, whatever it may be, service, deployment, etc, to match the pod, according to that label. So, maybe a potential reason is for uniformity. For instance, when you want to apply a service (to expose the pod to the web or something), you need to apply that service to the pod with a label.

References :

If you like, Feel free to clap for this article which makes me happy. :D :D

Did you find this guide helpful? Make sure to subscribe to my newsletter so you don’t miss the next article with useful deployment tips!

Follow us on Twitter 🐦 and Facebook 👥 and join our Facebook Group 💬.

To join our community Slack 🗣️ and read our weekly Faun topics 🗞️, click here⬇

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.

Sarasa Gunawardhana

Written by

DevOps Engineer | Full Stack Developer | Tech Blogger | SLIIT

Faun

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade