
If you are not familiar with the oc command, refer to OpenShift - Getting Started with the oc command.
Annotations and Labels are similar, but have important differences.
- Labels are used by OpenShift/Kubernetes, are limited by RFC 1123 to a maximum of 63 characters, and are used so that resource "a" can select resource "b" via the label.
- Annotations are NOT limited by RFC 1123 to a maximum of 63 characters, and contain metadata about a resource. Check out my article on the oc annotate command.
- Some annotations are for humans (NOT OpenShift/Kuberneters), to displays some sort of metadata about a resource (e.g. app owner: John Doe)
- Some annotations are used by OpenShift/Kubernetes (e.g. deployment.kubernetes.io/revision: "2")
Labels are metadata used so that resource "a" knows resource "b". For example, let’s say you have a deployment labeled my-deployment.
~]$ oc get deployment my-deployment -o yaml
metadata:
labels:
app: my-deployment
app.kubernetes.io/component: my-deployment
app.kubernetes.io/instance: my-deployment
And let's say replica set my-deployment-7d7f45d979 has selector matchLabels deployment: my-deployment. This is how the replica set knows that pods should be created for deployment "my-deployment".
~]$ oc get replicaset my-deployment-7d7f45d979 --output yaml
spec:
selector:
matchLabels:
deployment: my-deployment
The pods will have a label so that the pods are for my-deployment.
~]$ oc get pod my-deployment-7d7f45d979-dm9g2 --output yaml
labels:
deployment: my-deployment
The service will have a selector specification that is used to determine the pod that the service should forward requests onto.
~]$ oc get service my-service --output yaml
spec:
selector:
deployment: my-deployment
The selector specification will tell the service to be labeled with my-deployment so that the service forwards requests onto the my-deployment pod.
~]$ oc get service my-service --output yaml
labels:
app: my-deployment
app.kubernetes.io/component: my-deployment
app.kubernetes.io/instance: my-deployment
The route will have the “to” specification to know which service and pod the request should be routed onto.
~]$ oc get route my-route --output yaml
spec:
to:
kind: Service
name: my-deployment
The route will be labeled with my-deployment so that the route forwards requests onto the service that forwards requests onto the my-deployment pod.
~]$ oc get route my-route --output yaml
labels:
app: my-deployment
app.kubernetes.io/component: my-deployment
app.kubernetes.io/instance: my-deployment
Did you find this article helpful?
If so, consider buying me a coffee over at