DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Close Site Search Indexing via Kubernetes HAProxy Ingress
  • Simplifying A/B/n Testing of Backend Services
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements

Trending

  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  • Why We Chose Iceberg Over Delta After Evaluating Both at Scale
  • LLM Integration in Enterprise Applications: A Practical Guide
  • What Nobody Tells You About Multimodal Data Pipelines for AI Training
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Setting Kubernetes Labels and Annotations

Setting Kubernetes Labels and Annotations

In this article, we look at configuring labels and annotations. All Kubernetes objects should have a set of labels.

By 
Randhir Singh user avatar
Randhir Singh
·
May. 20, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
7.9K Views

Join the DZone community and get the full member experience.

Join For Free

Helm chart is a popular method of packaging applications to be installed on the Kubernetes cluster. As Helm chart developers, we have to decide what configurations need to be made available to the customer and how should they be offered. In this article, we look at configuring labels and annotations. All Kubernetes objects should have a set of labels for external tools to identify and use them in a consistent manner. Specifying labels and annotations involves solving some key challenges.

  1. What are the labels that should be common to all Kubernetes objects?
  2. How to expose labels and annotations configuration parameters in values.yaml?
  3. We cannot say upfront what all the possible labels and annotation parameter names that a customer might want for application Pods.
  4. Allow customers to expose the services externally in the various ways that they might want.

Required Labels

The first challenge is to come up with a list of required labels. The official documentation provides a list of recommended labels. These labels are recommended for every object that we create and have a shared prefix:  app.kubernetes.io . Prefixes ensure that recommended labels do not get mixed up with custom labels defined by the customers. 

YAML
 




xxxxxxxxxx
1


 
1
metadata:
2
  labels:
3
    app.kubernetes.io/name: {{ template "chart.fullname" .}}
4
    helm.sh/chart: {{ template "chart.fullnameandversion" .}}
5
    app.kubernetes.io/instance: {{ .Release.Name }}
6
    app.kubernetes.io/managed-by: {{ .Release.Service }}
7
    app.kubernetes.io/component: my-component
8
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
9

          



In addition to these, we need to have an organization-wide consensus on the mandatory set of labels.

Exposing Labels and Annotations Configuration

Next, we need to let customers specify their custom labels and annotations. This can be supported with toYaml:

YAML
 




xxxxxxxxxx
1


 
1
labels:
2
  {{- with .Values.my-service.extraLabels }}
3
  {{ toYaml . | indent 4 }}
4
  {{- end }}



This allows the customer to set additional labels in the custom values file such as:

YAML
 




x


 
1
my-service:
2
  extraLabels:
3
    name: project-name
4
    env: lab
5
    version: 1.0.0
6
    component: api-gateway
7
    part-of: application
8
    managed-by: orchestrator
9
    location-name: branch



Exposing Services Externally

Customers need to expose some services externally to the cluster via a LoadBalancer. This requires the services to be annotated with annotations specific to that LoadBalancer. For example, a customer may require the following annotations to support the F5 Load Balancer and services that need to be exposed externally to the cluster.

YAML
 




xxxxxxxxxx
1


 
1
annotations:
2
  app: application-name
3
  cis.f5.com/as3-tenant: tenant
4
  cis.f5.com/as-app: ingress
5
  cis.f5.com/as-pool: ingress-pool



This can be supported in a similar way as labels:

YAML
 




xxxxxxxxxx
1


 
1
annotations:
2
  {{- with .Values.my-service.annotations }}
3
  {{ toYaml . | indent 4 }}
4
  {{- end }}



With this background, we can set the labels and annotations for different Kubernetes Objects.

Deployment YAML

Labels will have mandatory labels as decided by the organization and extra labels from the customers and the same for annotations. The deployment file for an application should have labels and annotations as illustrated below.

YAML
 




x


 
1
labels:
2
    app.kubernetes.io/name: {{ template "chart.fullname" .}}
3
    helm.sh/chart: {{ template "chart.fullnameandversion" .}}
4
    app.kubernetes.io/instance: {{ .Release.Name }}
5
    app.kubernetes.io/managed-by: {{ .Release.Service }}
6
    app.kubernetes.io/component: my-component
7
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
8
  {{- with .Values.extraLabels }}
9
    {{- toYaml . | nindent 4 }}
10
  {{- end }}
11
annotations:
12
  prometheus.io/path: /actuator/prometheus
13
  prometheus.io/port: '9000'
14
  prometheus.io/scrape: 'true'
15
  {{- with .Values.deploymentAnnotations }}
16
    {{- toYaml . | nindent 4 }}
17
  {{- end }}



Service YAML

Service with mandatory labels required of all Kubernetes objects and customer provided annotations.

YAML
 




x


 
1
labels:
2
    app.kubernetes.io/name: {{ template "chart.fullname" .}}
3
    helm.sh/chart: {{ template "chart.fullnameandversion" .}}
4
    app.kubernetes.io/instance: {{ .Release.Name }}
5
    app.kubernetes.io/managed-by: {{ .Release.Service }}
6
    app.kubernetes.io/component: my-component
7
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
8
    {{- with .Values.extraLabels }}
9
      {{- toYaml . | nindent 4 }}
10
    {{- end }}
11
{{- if .Values.serviceAnnotations }}
12
annotations:
13
  {{ toYaml .Values.serviceAnnotations | indent 4 }}
14
{{- end }}



Other Objects

The other common Kubernetes Objects, e.g., Horizontal Pod Autoscalar, ConfigMap, Secrets should have the mandatory labels common to all objects.

YAML
 




xxxxxxxxxx
1


 
1
labels:
2
    app.kubernetes.io/name: {{ template "chart.fullname" .}}
3
    helm.sh/chart: {{ template "chart.fullnameandversion" .}}
4
    app.kubernetes.io/instance: {{ .Release.Name }}
5
    app.kubernetes.io/managed-by: {{ .Release.Service }}
6
    app.kubernetes.io/component: my-component
7
    app.kubernetes.io/version: {{ .Chart.AppVersion }}



Conclusion

Labels and annotations are one of the main foundations for Kubernetes. They are both ways of adding metadata to Kubernetes objects. Kubernetes labels allow us to identify, select, and operate on Kubernetes objects, whereas annotations are non-identifying metadata.

They are used by external tools to help them to provide extra functionalities. However, there are no standard practices enforced. In this article, we attempted to provide a guideline that will serve as best practices when labeling and annotating Kubernetes objects.

Label Kubernetes Annotation

Opinions expressed by DZone contributors are their own.

Related

  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Close Site Search Indexing via Kubernetes HAProxy Ingress
  • Simplifying A/B/n Testing of Backend Services
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook