Sub topic 9
Recommended labels and labeling conventions
This chapter assumes you can read and write simple YAML manifests, use basic kubectl commands, and already know how labels and selectors work. It covers the shared label names Kubernetes recommends, and how to apply them consistently.
Why conventions matter
Kubernetes accepts any valid label key, so every team can invent its own names. One team writes app=web, another writes application=web, and a third writes name=web. Dashboards, scripts, and kubectl queries then have to know all three spellings.
The Kubernetes documentation defines a set of recommended labels. They share the prefix app.kubernetes.io/. Tools such as Helm, dashboards, and cost reports can read them without any per-team setup.
The recommended label set
| Label | Meaning | Example |
|---|---|---|
| `app.kubernetes.io/name` | Name of the application | `mysql` |
| `app.kubernetes.io/instance` | Unique name for this installation of the application | `mysql-prod` |
| `app.kubernetes.io/version` | Current version of the application | `8.0.36` |
| `app.kubernetes.io/component` | Role of this part within the architecture | `database` |
| `app.kubernetes.io/part-of` | Larger application this one belongs to | `wordpress` |
| `app.kubernetes.io/managed-by` | Tool that manages the object | `helm` |
The name and instance labels are different on purpose. name identifies the kind of application, such as mysql. instance tells apart several installations of it, such as mysql-prod and mysql-staging in the same cluster.
A Deployment that uses them
Put the labels on the Deployment and on the Pod template. The selector then uses a stable subset of them.
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-db
labels:
app.kubernetes.io/name: mysql
app.kubernetes.io/instance: mysql-prod
app.kubernetes.io/version: "8.0.36"
app.kubernetes.io/component: database
app.kubernetes.io/part-of: wordpress
app.kubernetes.io/managed-by: kubectl
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: mysql
app.kubernetes.io/instance: mysql-prod
template:
metadata:
labels:
app.kubernetes.io/name: mysql
app.kubernetes.io/instance: mysql-prod
app.kubernetes.io/version: "8.0.36"
app.kubernetes.io/component: database
app.kubernetes.io/part-of: wordpress
spec:
containers:
- name: mysql
image: mysql:8.0.36
env:
- name: MYSQL_ALLOW_EMPTY_PASSWORD
value: "1"Quote the version value. Without quotes, YAML may read a value like 8.0 as a number, and label values must be strings.
The selector uses only name and instance. A Deployment's spec.selector is immutable after creation, so it must contain labels that never change. version changes with every upgrade, so keeping it out of the selector lets you roll out a new version without recreating the Deployment.
Querying across a whole application
Because part-of is shared, one query finds every piece of an application.
kubectl get pods -l app.kubernetes.io/part-of=wordpress
kubectl get pods -l app.kubernetes.io/component=database --show-labels
kubectl get all -l app.kubernetes.io/instance=mysql-prodConventions for your own labels
The app.kubernetes.io/ prefix is reserved for the shared set. For labels specific to your organization, use your own domain as a prefix.
- Use a prefix such as
example.com/for custom keys, for exampleexample.com/team. Unprefixed keys are treated as private to the user and risk clashing with other tools. - Decide on a set of required labels, such as
teamandenvironment, and enforce it in review or with an admission policy. - Use consistent lowercase values.
Productionandproductionare different values to a selector. - Keep immutable identity labels in selectors, and put changing data like
versiononly on objects.
Exercise
Why does the Deployment above leave app.kubernetes.io/version out of spec.selector?
Show answer
The selector of a Deployment cannot be changed after it is created, and the version changes on every upgrade. Leaving version out lets you update the Pod template's version label without recreating the Deployment.
Exercise
Write a kubectl command that lists all Pods that belong to the application wordpress and whose component is database.
Show answer
kubectl get pods -l app.kubernetes.io/part-of=wordpress,app.kubernetes.io/component=database