Step By Step Guide To Use Annotations
For using annotations in a Kubernetes services file, follow the given steps:
Step 1. Create a file and name it example.yaml. Enter the following command in your terminal to create a new file called
Example.yaml:
touch example.yaml
Step 2. Open a text editor for adding code to the file. Use can use the following command in your terminal to open vim text editor.
vim example.yaml
Step 3. Add the following code to the configuration file. Here we have created a services file and we are using the annotation key “a8r.io/owner” which corresponds to the value ‘@1shubham7″ which is a username.
apiVersion: v1
kind: Service
metadata:
name: example
annotations:
a8r.io/owner: “@1shubham7”
spec:
ports:
- name: http
port: 80
targetPort: 8080
selector:
app: quote
Step 4. enter the following command in your terminal in order to get all the services that have been created in your local machine.
kubectl get services
If you have never created any services, then only auto-generated Kubernetes service will be shown in the output.
Step 5. apply the configuration file to create a Kubernetes service named example
kubectl apply -f example.yaml
Now when you enter the following command in your terminal in order to get all the services that have been created in your local machine:
kubectl get services
you will the example service in the output along with other services:
Step 6. Enter the following command in your terminal to display detailed information about the example service:
kubectl describe service example
This will output a detailed information about the example service. In the “Annotations” part you will find the annotations created by in previous steps:
Or you can also check annotations by using the following command in your terminal:
kubectl get service example -o jsonpath='{.metadata.annotations}'
This will give you an output similar to this:
Here we can see that we have an annotation key (“a8r.io/owner”) and a value (“@1shubham7”) corresponding to it.
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.