We can use either labels or annotations to attach metadata to Kubernetes objects. Labels can be used to select objects and to find collections of objects that satisfy certain conditions. In contrast, annotations are not used to identify and select objects. The metadata in an annotation can be small or large, structured or unstructured, and can include characters not permitted by labels.
Lets say , if you want to add a download URL to our running pod e.g webserver pod.
$ kubectl annotate pod webserver url=https://hub.docker.com/_/nginx/
$ kubectl describe pod webserver
You will see Annotations
filed contains the above URL.
Use same annotate command and mention only key with a dash (-) at the end of the key . Below command will remove the annotation from Pod.
$ kubectl annotate pod webserver-
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
annotations:
build: demo
environment: dev
owner: Orphan
spec:
containers:
- name: nginx-demo
image: nginx:1.9.1
ports:
- containerPort: 80
Save it as a yaml file e.g annotate-pod.yaml
Deploy Pod and Describe pod to see annotation.
$ kubectl -f annotate-pod.yaml
$ kubectl describe pod nginx
Annotations are used for non-identifying information for the tools or services which are not used internally by k8s.
You can’t specify selectors over them within Kubernetes, but they can be used by external tools and libraries.
For example If you plan to use AWS ALB as ingress controller add the alb.ingress.kubernetes.io/actions.${action-name} annotation
If you want expose the metrics of your application to the Prometheus, then you can prometheus.io/scrape: “true” annotation annotation to your pod configuration.