Labeling and annotating the Pods
We will now discuss another key concept of Kubernetes: labels and annotations. Labels are key-value pairs that you can attach to your Kubernetes objects. Labels are meant to tag your Kubernetes objects with key-value pairs defined by you. Once your Kubernetes objects have been labeled, you can build a custom query to retrieve specific Kubernetes objects based on the labels they hold. In this section, we are going to discover how to interact with labels through kubectl
by assigning some labels to our Pods.
What are labels and why do we need them?
What label you define for your objects is up to you – there is no specific rule regarding this. These labels are attributes that will allow you to organize your objects in your Kubernetes cluster. To give you a very concrete example, you could attach a label called environment = prod
to some of your Pods, and then use the kubectl get pods
command to list all the Pods within that environment....