k8s pod節點標籤選擇器-nodeSelector
阿新 • • 發佈:2021-10-29
1. 節點標籤選擇器
-
nodeSelector:用於將Pod排程到匹配Label的Node上,如果沒有匹配的標籤會排程失敗。
- 作用:
約束Pod到特定的節點執行
完全匹配節點標籤 - 應用場景:
專用節點:根據業務線將Node分組管理
配備特殊硬體:部分Node配有SSD硬碟、GPU
- 作用:
-
示例:
確保Pod分配到具有SSD硬碟的節點上
-
第一步:給節點新增標籤
格式:kubectl label nodes <node-name> <label-key>=<label-value>
例如:
kubectl label nodes k8s-node1 disktype=ssd
驗證:
kubectl get nodes --show-labels
-
第二步:新增nodeSelector欄位到Pod配置中
apiVersion: v1 kind: Pod metadata: name: pod-example spec: nodeSelector: disktype: "ssd" containers: - name: nginx image: nginx:1.19
-
最後,驗證:
kubectl get pods -o wide
-
2. nodeSelector案例
-
給k8s-node1標記標籤
[root@k8s-master yaml]# kubectl label nodes k8s-node1 disktype="ssd" node/k8s-node1 labeled
-
檢視驗證
[root@k8s-master yaml]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS k8s-master Ready master 5d1h v1.19.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/master= k8s-node1 Ready <none> 5d v1.19.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux k8s-node2 Ready <none> 5d v1.19.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux
-
編寫配置檔案
[root@k8s-master yaml]# vim pod-example.yaml [root@k8s-master yaml]# cat pod-example.yaml apiVersion: v1 kind: Pod metadata: name: pod-example spec: nodeSelector: disktype: "ssd" containers: - name: nginx image: nginx:1.19
-
執行服務
[root@k8s-master yaml]# kubectl apply -f pod-example.yaml pod/pod-example created
-
驗證服務
[root@k8s-master yaml]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESinit-demo 1/1 Running 0 23h 10.244.36.93 k8s-node1 <none> <none>pod-envars 1/1 Running 2 24h 10.244.36.91 k8s-node1 <none> <none>pod-example 1/1 Running 0 107s 10.244.36.94 k8s-node1 <none> <none>web 1/1 Running 0 130m 10.244.169.158 k8s-node2 <none> <none>
-
註釋:
- 經過測試,在k8s-node1上已經做好標籤,在配置檔案寫入的時候,會引入標籤使用,
- 如果在,主機上沒有設定標籤,就會一直在pending的狀態,知道有標籤才會分配
3. nodeselector使用deployment來建立
-
檢視標籤名
[root@k8s-master yaml]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELSk8s-master Ready master 5d1h v1.19.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/master=k8s-node1 Ready <none> 5d v1.19.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linuxk8s-node2 Ready <none> 5d v1.19.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux
-
生成配置檔案
[root@k8s-master pod]# kubectl create deployment web --replicas=1 --image=nginx --dry-run=client -o yaml > kubelet_pod.yaml[root@k8s-master pod]# vim kubelet_pod.yaml [root@k8s-master pod]# cat kubelet_pod.yaml apiVersion: apps/v1kind: Deploymentmetadata: labels: app: web name: webspec: replicas: 1 selector: matchLabels: app: web strategy: {} template: metadata: labels: app: web spec: nodeSelector: kubernetes.io/hostname: "k8s-node01" containers: - image: nginx name: nginx
-
啟動配置檔案
[root@k8s-master pod]# kubectl apply -f kubelet_pod.yaml deployment.apps/web unchanged
-
驗證是否在node01節點
[root@k8s-master pod]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESweb-6b8c6845f-b4l2m 1/1 Running 0 4m12s 10.244.85.212 k8s-node01 <none> <none>