Summary - Labels
- Docker & Kubernetes course
- Intermediate
- Lesson 66/82
- 5 min
Combine labels and selectors to group and filter resources.
Summary: Labels
In this group:
- Labels are selectable key-values; annotations are non-selectable extra info.
kubectl labeladds,--overwritechanges,key-removes;annotateworks with the same syntax.- Selectors:
matchLabels(equality) andmatchExpressions(In, NotIn, Exists, DoesNotExist); a Deployment selector is immutable. -lfilters (comma = AND),-Ladds columns; bulk deletes, logs and restarts are done with selectors.- Set expressions are quoted:
-l 'ortam in (prod,staging)'.
kubectl label pods -l app=web surum=v2
kubectl get pods -l 'surum in (v1,v2)' -L surumPractice Recommended labels
Write a Deployment template's metadata section (starting with metadata:): under labels, app.kubernetes.io/name: web, app.kubernetes.io/version: "1.4.2", app.kubernetes.io/part-of: magaza; under annotations, aciklama: "ana sayfa servisi".
Starter code
# buraya yazSample solution
metadata:
labels:
app.kubernetes.io/name: web
app.kubernetes.io/version: "1.4.2"
app.kubernetes.io/part-of: magaza
annotations:
aciklama: "ana sayfa servisi"Expected output
deployment.apps/web configured
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Why isn't a version label put in a Deployment selector?
- The selector is immutable; when the version changes, the deployment would have to be recreated Correct answer
- Version labels are forbidden
- The Service can't see it
- The label is too long
Explanation: The version goes in the template label, not the selector.
2 Add a new label to all pods that have a label.
kubectl label pods ____ app=web surum=v2
kubectl label pods -l app=web surum=v2
Explanation: All pods selected with -l are labelled.
3 Match.
| Term | Meaning |
|---|---|
label |
For selection |
annotation |
For information |
-l |
Filter |
-L |
Column |