Part3- Labels in Kubernetes
Smooth Transition to Kubernetes
What is a Label:
A label is arbitrary key-value pair and you can attach it to a Kubernetes resource. We need labels to organize the Pods and all other Kubernetes objects through labels. What to do with labels:
- Filtering Resources: Resources are filtered based on whether they include the label specified in the selector like:
→ Check the labels on Github. There are some samples in the “ Useful Label commands “ section, you can use labels as stated there. One more thing, a resource can have more than one label, as long as the keys of those labels are unique within that resource.
- Categorizing Worker Nodes: If you want to constrain a Pod to only be able to run on a particular node(s), you can use “label selector” to make the selection. A label selector is a criterion, which filters resources based on whether they include a certain label with a certain value.
- Used for selection a Node : To assign a Pod to specific Nodes(s), “nodeSelector” is being used and you can put any label in there as in the image. When you then run “ kubectl apply -f <your-yaml> “ the Pod will get scheduled on the node that you attached the label ( based on disk-type and GPU) to. But the Labels should be in the node YAML file beforehand, for detail.
- Using a Label in the selector :
1.The Deployment resource will manage the pod via “app:be-activemq”
2. In every activemq pod creation by the Deployment, an “app:be-activemq” label will be added to the Pod
3. The Service which is used to access a pod will serve the activemq pod via the “app:be-active” label.
Annotating the Pods
- If you need to point Kubernetes to a group of resources, the Labels are the way to go. But if you want to annotate your setup with data that will help people or tools, but not Kubernetes itself, it’s better to put it into annotation metadata.
- Annotations are other key-value pairs, similar to labels, but they can’t be used to group objects the way labels can. While objects can be selected through label selectors, there is no such thing as an annotation selector. Besides that Kubernetes labels and annotations are also both ways of adding metadata to Kubernetes objects.
- Certain annotations are automatically added to objects by Kubernetes, but others are added by users manually. Let’s check it :
$ kubectl apply -f https://raw.githubusercontent.com/ErhanCetin/k8s-smooth-transition/master/k8s/medium-post/pods/news-tracker-api-pod.yaml$ kubectl get pods newsapi-pod -o yaml
When to use an annotation:
If you want to give more details about your project to someone/tools, you can use annotations. Here are some examples of what you put:
- Build, release, or image information like timestamps, release IDs, git branch, PR numbers, image hashes, and registry address.
- Client library or tool information that can be used for debugging purposes, for example, name, version, and build information.
- Phone or pager numbers of persons responsible ( I don’t recommend 🙂 ), or directory entries that specify where that information can be found, such as a team web site. )
- For detail: https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/