Now that we have a good number of longer exercises, it makes sense to rename the shorter demos/labs into 'labs' to avoid confusion between the two.
3.9 KiB
Labels and annotations
-
Most Kubernetes resources can have labels and annotations
-
Both labels and annotations are arbitrary strings
(with some limitations that we'll explain in a minute)
-
Both labels and annotations can be added, removed, changed, dynamically
-
This can be done with:
-
the
kubectl editcommand -
the
kubectl labelandkubectl annotate -
... many other ways! (
kubectl apply -f,kubectl patch, ...)
-
Viewing labels and annotations
- Let's see what we get when we create a Deployment
.lab[
-
Create a Deployment:
kubectl create deployment clock --image=jpetazzo/clock -
Look at its annotations and labels:
kubectl describe deployment clock
]
So, what do we get?
Labels and annotations for our Deployment
-
We see one label:
Labels: app=clock -
This is added by
kubectl create deployment -
And one annotation:
Annotations: deployment.kubernetes.io/revision: 1 -
This is to keep track of successive versions when doing rolling updates
And for the related Pod?
- Let's look up the Pod that was created and check it too
.lab[
-
Find the name of the Pod:
kubectl get pods -
Display its information:
kubectl describe pod clock-xxxxxxxxxx-yyyyy
]
So, what do we get?
Labels and annotations for our Pod
-
We see two labels:
Labels: app=clock pod-template-hash=xxxxxxxxxx -
app=clockcomes fromkubectl create deploymenttoo -
pod-template-hashwas assigned by the Replica Set(when we will do rolling updates, each set of Pods will have a different hash)
-
There are no annotations:
Annotations: <none>
Selectors
-
A selector is an expression matching labels
-
It will restrict a command to the objects matching at least all these labels
.lab[
-
List all the pods with at least
app=clock:kubectl get pods --selector=app=clock -
List all the pods with a label
app, regardless of its value:kubectl get pods --selector=app
]
Settings labels and annotations
- The easiest method is to use
kubectl labelandkubectl annotate
.lab[
-
Set a label on the
clockDeployment:kubectl label deployment clock color=blue -
Check it out:
kubectl describe deployment clock
]
Other ways to view labels
-
kubectl getgives us a couple of useful flags to check labels -
kubectl get --show-labelsshows all labels -
kubectl get -L xyzshows the value of labelxyz
.lab[
-
List all the labels that we have on pods:
kubectl get pods --show-labels -
List the value of label
appon these pods:kubectl get pods -L app
]
class: extra-details
More on selectors
-
If a selector has multiple labels, it means "match at least these labels"
Example:
--selector=app=frontend,release=prod -
--selectorcan be abbreviated as-l(for labels)We can also use negative selectors
Example:
--selector=app!=clock -
Selectors can be used with most
kubectlcommandsExamples:
kubectl delete,kubectl label, ...
Other ways to view labels
- We can use the
--show-labelsflag withkubectl get
.lab[
- Show labels for a bunch of objects:
kubectl get --show-labels po,rs,deploy,svc,no
]
Differences between labels and annotations
-
The key for both labels and annotations:
-
must start and end with a letter or digit
-
can also have
.-_(but not in first or last position) -
can be up to 63 characters, or 253 +
/+ 63
-
-
Label values are up to 63 characters, with the same restrictions
-
Annotations values can have arbitrary characters (yes, even binary)
-
Maximum length isn't defined
(dozens of kilobytes is fine, hundreds maybe not so much)
???
:EN:- Labels and annotations :FR:- Labels et annotations