In our manifest file, under spec.template.metadata.labels, we've specified that our Elasticsearch Pods should carry the label app: elasticsearch.
Label is one of two methods to attach arbitrary metadata to Kubernetes Objects, with the other being annotations.
Both labels and annotations are implemented as key-value stores, but they serve different purposes:
- Labels: Used to identify an Object as belonging to a certain group of similar Objects. In other words, it can be used to select a subset of all Objects of the same type. This can be used to apply Kubernetes commands to only a subset of all Kubernetes Objects.
- Annotations: Any other arbitrary metadata not used to identify the Object.
A label key consists of two components—an optional prefix, and a name—separated by a forward slash (/).
The prefix exists as a sort of namespace, and allows third-party tools to select only the Objects that it is managing. For instance, the core Kubernetes components have a label with a prefix of kubernetes.io/.
Labeled Objects can then be selected using label selectors, such as the one specified in our Deployment manifest:
selector:
matchLabels:
app: elasticsearch
This selector instructs the Deployment Controller to manage only these Pods and not others.