Labels are mechanism we use to organize the Kubernetes objects. A label is Key-Value pair without any predefined meaning. Labeling can be done with declarative method (using manifest file) or imperative method (using cli). Usually scheduler will automatically do reasonable placement of pods. however with node tagging and label selectors we can specify pods to run on specific nodes considering factors like SSD,CPU,MEMORY,STORAGE or DATA Center location. We can group servers based on various considerations like Rack/Blade/Tower and label them.
In my setup, the cluster contains two worker nodes and one control plane node.
I have specified the nodeSelector in the PodSpec using a key-value pair. If the key-value pair matches exactly the label defined in the node, the pod will get matched to the specific node. The following command add labels to the nodes.
kubectl label nodes <node-name> <key>=<value>
As an example, Nodes selectors can be
nodeSelector: disktype: SSD Location: Texas
[root@kubernetesM ~]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS kubernetesm.ranjeetbadhe.com Ready master 162d v1.14.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=kubernetesm.ranjeetbadhe.com,kubernetes.io/os=linux,node-role.kubernetes.io/master= kubernetesn1.ranjeetbadhe.com Ready <none> 162d v1.14.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=kubernetesn1.ranjeetbadhe.com,kubernetes.io/os=linux kubernetesn2.ranjeetbadhe.com Ready <none> 162d v1.14.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=kubernetesn2.ranjeetbadhe.com,kubernetes.io/os=linux
[root@kubernetesM ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION kubernetesm.ranjeetbadhe.com Ready master 162d v1.14.0 kubernetesn1.ranjeetbadhe.com Ready <none> 162d v1.14.0 kubernetesn2.ranjeetbadhe.com Ready <none> 162d v1.14.0 [root@kubernetesM ~]# kubectl label nodes kubernetesn1.ranjeetbadhe.com location=texas node/kubernetesn1.ranjeetbadhe.com labeled [root@kubernetesM ~]# kubectl label nodes kubernetesn2.ranjeetbadhe.com location=newyork node/kubernetesn2.ranjeetbadhe.com labelled [root@kubernetesM ~]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS kubernetesm.ranjeetbadhe.com Ready master 162d v1.14.0 hostname=kubernetesm.ranjeetbadhe.com,kubernetes.io/os=linux,node-role.kubernetes.io/master= kubernetesn1.ranjeetbadhe.com Ready <none> 162d v1.14.0 hostname=kubernetesn1.ranjeetbadhe.com,kubernetes.io/os=linux,location=texas kubernetesn2.ranjeetbadhe.com Ready <none> 162d v1.14.0 hostname=kubernetesn2.ranjeetbadhe.com,kubernetes.io/os=linux,location=newyork
[root@kubernetesM ~]# cat pods-node.yml apiVersion: apps/v1 kind: Deployment metadata: labels: # This label is applied to the Deployment type: dev name: nginx-deploy spec: replicas: 1 selector: matchLabels: ## This label is used to match the Pod to create replicas type: dev template: metadata: labels: ## This label is applied to the Pod type: dev spec: containers: - image: nginx name: nginx ports: - containerPort: 80 nodeSelector: ## This label is used to deploy the pod on matching nodes location : texas [root@kubernetesM ~]# kubectl apply -f pods-node.yml deployment.apps/nginx-deploy created
[root@kubernetesM ~]# kubectl get pods NAME READY STATUS RESTARTS AGE mariadb-deployment-6dd68bd9c5-vhc5g 1/1 Running 0 6m58s nginx-deploy-b4f44957b-v8x7n 0/1 ContainerCreating 0 5s [root@kubernetesM ~]# kubectl get pods NAME READY STATUS RESTARTS AGE mariadb-deployment-6dd68bd9c5-vhc5g 1/1 Running 0 39m nginx-deploy-b4f44957b-v8x7n 1/1 Running 0 32m [root@kubernetesM ~]# kubectl describe pods nginx-deploy-b4f44957b-v8x7n Name: nginx-deploy-b4f44957b-v8x7n Namespace: default Priority: 0 PriorityClassName: <none> Node: kubernetesn1.ranjeetbadhe.com/192.168.0.52 Start Time: Mon, 20 Feb 2023 16:23:11 +0530 Labels: pod-template-hash=b4f44957b type=dev Annotations: <none> Status: Running IP: 172.16.1.54 Controlled By: ReplicaSet/nginx-deploy-b4f44957b Containers: nginx: Container ID: docker://8a77dac46038dd3c5f525f0ac3ce99577c62a82b66d99d49694049a3a5259977 Image: nginx Image ID: docker-pullable://docker.io/nginx@sha256:6650513efd1d27c1f8a5351cbd33edf85cc7e0d9d0fcb4ffb23d8fa89b601ba8 Port: 80/TCP Host Port: 0/TCP State: Running Started: Mon, 20 Feb 2023 16:23:16 +0530 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-4pxmt (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-4pxmt: Type: Secret (a volume populated by a Secret) SecretName: default-token-4pxmt Optional: false QoS Class: BestEffort Node-Selectors: location=texas Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: <none>
[root@kubernetesM ~]# kubectl describe pods nginx-deploy-b4f44957b-v8x7n Name: nginx-deploy-b4f44957b-v8x7n Namespace: default Priority: 0 PriorityClassName: <none> Node: kubernetesn1.ranjeetbadhe.com/192.168.0.52 Start Time: Mon, 20 Feb 2023 16:23:11 +0530 Labels: pod-template-hash=b4f44957b type=dev Annotations: <none> Status: Running IP: 172.16.1.54 Controlled By: ReplicaSet/nginx-deploy-b4f44957b Containers: nginx: Container ID: docker://8a77dac46038dd3c5f525f0ac3ce99577c62a82b66d99d49694049a3a5259977 Image: nginx Image ID: docker-pullable://docker.io/nginx@sha256:6650513efd1d27c1f8a5351cbd33edf85cc7e0d9d0fcb4ffb23d8fa89b601ba8 Port: 80/TCP Host Port: 0/TCP State: Running Started: Mon, 20 Feb 2023 16:23:16 +0530 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-4pxmt (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-4pxmt: Type: Secret (a volume populated by a Secret) SecretName: default-token-4pxmt Optional: false QoS Class: BestEffort Node-Selectors: location=texas Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: <none>
[root@kubernetesM ~]# kubectl get pods NAME READY STATUS RESTARTS AGE mariadb-deployment-6dd68bd9c5-n58b8 1/1 Running 0 29s nginx-deploy-b4f44957b-v8x7n 1/1 Running 1 17h
Placement of MariaDB pod is automatically done by scheduler as depicted from the below output. I have not put any constraints for this pod in my manifest.
[root@kubernetesM ~]# kubectl describe pods mariadb-deployment-6dd68bd9c5-n58b8 Name: mariadb-deployment-6dd68bd9c5-n58b8 Namespace: default Priority: 0 PriorityClassName: <none> Node: kubernetesn1.ranjeetbadhe.com/192.168.0.52 Start Time: Tue, 21 Feb 2023 09:52:21 +0530 Labels: app=mariadb pod-template-hash=6dd68bd9c5 type=database Annotations: <none> Status: Running IP: 172.16.1.56 Controlled By: ReplicaSet/mariadb-deployment-6dd68bd9c5 Containers: mariadb: Container ID: docker://0e612a177414c233a741c6d33a719d591658cf1f54753a283940ae682eee9948 Image: mariadb Image ID: docker-pullable://docker.io/mariadb@sha256:dd0f492b6b6e7bb4aa707181b799d4efe42cb3a9f6012ec3dbaf326d402151e8 Port: 3306/TCP Host Port: 0/TCP State: Running Started: Tue, 21 Feb 2023 09:52:46 +0530 Ready: True Restart Count: 0 Environment: MYSQL_ROOT_PASSWORD: my-password Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-4pxmt (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-4pxmt: Type: Secret (a volume populated by a Secret) SecretName: default-token-4pxmt Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message --- ------ ---- ---- ------- Normal Scheduled 45s default-scheduler Successfully assigned default/mariadb-deployment-6dd68bd9c5-n58b8 to kubernetesn1.ranjeetbadhe.com Normal Pulling 44s kubelet, kubernetesn1.ranjeetbadhe.com Pulling image "mariadb" Normal Pulled 21s kubelet, kubernetesn1.ranjeetbadhe.com Successfully pulled image "mariadb" Normal Created 20s kubelet, kubernetesn1.ranjeetbadhe.com Created container mariadb Normal Started 20s kubelet, kubernetesn1.ranjeetbadhe.com Started container mariadb
Thank you for reading my blog. If you require any further information, please feel free to contact me.
In my upcoming blogs I will cover Service mesh , Kubernetes networking covering container networking interface (Plugins like Calico, Flannel, Weave and Cilium) ,BGP configuration and other aspects of solution building in 5g telecom domain