Proper use of Kubernetes labels

Kubernetes labels are a way to organise and categorise Kubernetes objects, such as pods and services. They are key-value pairs that can be added to a resource and used to identify and group related resources together. 

Proper use of Kubernetes labels includes:

  1. Consistency: Use consistent naming conventions and structure for labels to make it easier to search and filter by a label.
  2. Meaningful: Use meaningful labels that describe the characteristics of the object. For example, use labels like environment: production or app: frontend to indicate the environment or application that the object belongs to.
  3. Reusable: Use labels that can be reused across multiple objects, for example, use labels like environment: production for all objects that are in the production environment.
  4. Scalable: Avoid creating too many labels or using too many unique label values, as this can make it difficult to search and filter by a label.
  5. Use selectors: Use label selectors to filter and manage groups of objects that have the same label. For example, use label selectors to find all pods that have the label app: prometheus.
  6. Avoid using labels for identifying individual objects: Avoid using labels as a way to identify individual objects, as they can be changed or deleted. Instead, use unique identifiers like pod names or service names to identify individual objects.

Typically, you want to use labels to organise pods. The usual way is to add labels onto the pod controller (deployment, statefulset or deamonset) template.

Labelling Kubernetes objects correctly and consistently is important for effectively managing and scaling your Kubernetes cluster. It also makes it easier to search, filter and manage your objects based on their characteristics.