Labels
In Kubernetes, any object can be identified using a label.
You can assign multiple labels per object, but you should avoid using too many labels, or too few; too many will get you confused and too few won’t give the real benefits of grouping, selecting, and searching.
Best practice is to assign labels to indicate:
Application/program ID using this pod
Owner (who manages this pod/application)
Stage (the pod/application in development/testing/production version)
Resource requirements (SSD, CPU, storage)
Location (preferred location/zone/data center to run this pod/application)
Okay, let’s assign labels for (stage: testing
) and (zone: production
) to two nodes,
respectively, then try to launch a pod in a node that has the label
(stage: testing
):
$kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS cent222 Ready <none> 2h v1.9.2 <none> cent111 NotReady <none> 2h v1.9.2 <none> cent333 Ready <none> 2h v1.9.2 <none> $kubectl label nodes cent333 stage=testing $kubectl label nodes cent222 stage=production $kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS cent222 Ready <none> 2h v1.9.2 stage=production cent111 NotReady <none> 2h v1.9.2 <none> cent333 Ready <none> 2h v1.9.2 stage=testing
Now let’s launch a basic Nginx pod tagged with stage: testing
in the nodeSelector and confirm it will
land on a node tagged with stage: testing
. Kube-scheduler uses labels mentioned in the nodeSelector section
of the pod YAML to select the node to launch the pod:
Kube-scheduler picks the node based on various factors like individual and collective resource requirements, hardware, software, or policy constraints, affinity and anti-affinity specifications, data locality, inter-workload interference, and deadlines.
#pod-webserver-do-label.yaml apiVersion: v1 kind: Pod metadata: name: contrail-webserver labels: app: webserver spec: containers: - name: contrail-webserver image: contrailk8sdayone/contrail-webserver nodeSelector: stage: testing $ $ kubectl create -f pod-webserver-do-label.yaml pod "contrail-webserver" created $ kubectl get pods --output=wide NAME READY STATUS RESTARTS AGE IP NODE contrail-webserver 1/1 Running 0 48s 10.47.255.238 cent333
You can assign a pod to a certain node without labels
by adding the argument nodeName: nodeX
under
the spec in the YAML file where nodeX
is
the name of the node.