How To Write Annotations
Annotations just like labels are key value pairs. Therefore for writing an annotation, we add a “key” and a corresponding “value” to the key. Kubernetes only allows keys and the values to be in string. therefore, any other data type (boolean, int, etc.) must be avoided. the syntax for annotations looks like the following.
Syntax:
"metadata": {
"annotations": {
"key1" : "value1",
"key2" : "value2"
}
}
1. Annotation Keys: For creating valid annotation keys, it must have a name, having a prefix is optional. you can use alphanumeric characters for the names and prefixes. You can use dashes (“-“), underscores (“_”) and dots (“.”) as separations. name and prefix are separated by a slash (“/”).According to the Kubernetes convention, private user keys don’t have prefix, while the system components in Kubernetes that add annotations to the end-user objects always holds a prefix. Examples can be “kube-scheduler” or “kube-controller-manager”.
An example of Annotation key that has a name and a prefix is following:
a8r.io/owner: “@1shubham7”
Here @1shubham7 is my GitHub username. An example of Annotation key that has a name but not a prefix is following:
description: "This is the description"
An example service file using Annotations looks like this:
a8r.io/owner represents the GitHub username, email address linked to the GitHub account, or unstructured owner description.
2. Values: Annotation Values are the values or information associated with the corresponding Annotation key. In the above example, Annotation value for a8r.io/owner is “@1shubham7”.
How to Use Kubernetes Annotations?
Annotations are key-value pairs that are used to attach non-identifying metadata to Kubernetes objects. Various tools that are built over Kubernetes use this metadata attached by annotations to perform actions or enhance resource management. Labels and Annotations are used to attach metadata to Kubernetes objects. This makes annotations important for working with Kubernetes clusters. To learn more about Kubernetes cluster and its architecture refer to Kubernetes – Architecture.