Good Practices

  • Make the best use of labels by applying all the Recommended Labels. Recommended Labels are a set of standard labels list provided by Kubernetes to fully utilize it. All of them describe a unique thing about the application. Some of them are:

app.kubernetes.io/name: demo_app

app.kubernetes.io/instance: exception-fix-2

app.kubernetes.io/version: “1.2.2”

app.kubernetes.io/component: mongodb

app.kubernetes.io/managed-by: kubernetes

  • We should avoid changing the labels multiple times without some valid reason. This can also lead to unwanted results like the crashing of pods since their labels change or reverse.
  • We should always manage labels with code and not directly command line. Even though it is easier to just use the kubectl command to create/delete/update labels, it should be done through code so that it is maintained properly.
  • We should try to avoid conflicts in the naming of labels by adding some prefix/suffix so that it is recognized easily.
  • Never store sensitive data in labels. Labels are not encrypted and thus should not be used to store private data – because if anyone gets access to the cluster can see all the labels & selectors.
  • We should also use a convention for labels. This helps us in avoiding conflict between labels. The convention could be like this – <prefix>/<name>. Prefix could be a domain or higher level app and the name could be the specific name.

How To Use Kubernetes Labels and Selectors?

Kubernetes (also known as K8s) is an open-source Container Management tool. It helps us automate all the processes like deployment, load balancing, rolling update, etc. We can basically deploy, scale, and manage containerized applications.

In Kubernetes, labels and selectors provide a flexible and effective approach to facilitate selection for managing resources like restart/delete/etc. operations, and also for human readability. They are mentioned in the configuration file i.e., yaml files for deployments. They are used to connect services with a pod.

A briefer understanding of Labels and Selectors could be as follows:

Labels are key-value pairs that are used to identify K8s resources like deployments, pods, services. They are completely user-defined, may be tailored to meet our particular needs, and are used to identify and classify resources.

Selectors are utilized to find and categorize resources according to their labels. They can be used to specify criteria for choosing resources that correspond to particular label combinations. They also help in specifying the set of resources to be targeted or managed in the Kubernetes components, including services, deployments, replica sets, and stateful sets.

Similar Reads

Using Labels and Selectors

To use labels and selectors in our applications, we need to specify them in the yaml file....

Advantages of using Labels and Selectors:

As mentioned earlier labels and selectors are used to select and filter the resources in Kubernetes, but this also has many added advantages. Some of them include:...

Good Practices

Make the best use of labels by applying all the Recommended Labels. Recommended Labels are a set of standard labels list provided by Kubernetes to fully utilize it. All of them describe a unique thing about the application. Some of them are:...

Conclusion

Labels and selectors are very useful if utilized properly. By using labels, we can maintain our resources very efficiently. They also act as metadata in Kubernetes which gives useful insights about the data (application). This can be quite valuable for people working with the system, as demonstrated by features like human-readable annotations, as it helps them navigate the resources and gives them a sense of how everything fits together....