Labels are key-value pairs attached to Kubernetes objects. They are used for identifying various attributes of objects which can in turn be used to select and group various subsets of those objects. We can attach labels to objects by listing them in the metadata.labels
section of an object descriptor:
my-production-label-pod
apiVersion: v1
kind: Pod
metadata:
name: my-production-label-pod
labels:
app: my-app
environment: production
spec:
containers:
- name: nginx
image: nginx
Selectors are used for identifying and selecting a specific group of objects using their labels. One way to use selectors is to use them with kubectl get
to retrieve a specific list of objects. We can specify a selector using the -l
flag.
kubectl get pods -l app=my-app
Annotations are similar to labels in that they can be used to store custom metadata about objects. However, unlike labels, annotations cannot be used to select or group objects in Kubernetes. External tools can read, write, and interact with annotation.
apiVersion: v1
kind: Pod
metadata:
name: annotations-demo
annotations:
imageregistry: "https://hub.docker.com/"
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80