Kubernetes K8S之固定節點nodeName和nodeSelector調度詳解

 

Kubernetes K8S之固定節點nodeName和nodeSelector調度詳解與示例html

 

主機配置規劃

服務器名稱(hostname) 系統版本 配置 內網IP 外網IP(模擬)
k8s-master CentOS7.7 2C/4G/20G 172.16.1.110 10.0.0.110
k8s-node01 CentOS7.7 2C/4G/20G 172.16.1.111 10.0.0.111
k8s-node02 CentOS7.7 2C/4G/20G 172.16.1.112 10.0.0.112

 

nodeName調度

nodeName是節點選擇約束的最簡單形式,可是因爲其限制,一般不多使用它。nodeName是PodSpec的領域。node

pod.spec.nodeName將Pod直接調度到指定的Node節點上,會【跳過Scheduler的調度策略】,該匹配規則是【強制】匹配。能夠越過Taints污點進行調度。linux

nodeName用於選擇節點的一些限制是:web

  • 若是指定的節點不存在,則容器將不會運行,而且在某些狀況下可能會自動刪除。
  • 若是指定的節點沒有足夠的資源來容納該Pod,則該Pod將會失敗,而且其緣由將被指出,例如OutOfmemory或OutOfcpu。
  • 雲環境中的節點名稱並不是老是可預測或穩定的。

 

nodeName示例

獲取當前的節點信息docker

1 [root@k8s-master scheduler]# kubectl get nodes -o wide
2 NAME         STATUS   ROLES    AGE   VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION           CONTAINER-RUNTIME
3 k8s-master   Ready    master   42d   v1.17.4   172.16.1.110   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://19.3.8
4 k8s-node01   Ready    <none>   42d   v1.17.4   172.16.1.111   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://19.3.8
5 k8s-node02   Ready    <none>   42d   v1.17.4   172.16.1.112   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://19.3.8

 

當nodeName指定節點存在

要運行的yaml文件api

 1 [root@k8s-master scheduler]# pwd
 2 /root/k8s_practice/scheduler
 3 [root@k8s-master scheduler]# cat scheduler_nodeName.yaml 
 4 apiVersion: apps/v1
 5 kind: Deployment
 6 metadata:
 7   name: scheduler-nodename-deploy
 8   labels:
 9     app: nodename-deploy
10 spec:
11   replicas: 5
12   selector:
13     matchLabels:
14       app: myapp
15   template:
16     metadata:
17       labels:
18         app: myapp
19     spec:
20       containers:
21       - name: myapp-pod
22         image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1
23         imagePullPolicy: IfNotPresent
24         ports:
25           - containerPort: 80
26       # 指定節點運行
27       nodeName: k8s-master

 

運行yaml文件並查看信息服務器

 1 [root@k8s-master scheduler]# kubectl apply -f scheduler_nodeName.yaml 
 2 deployment.apps/scheduler-nodename-deploy created
 3 [root@k8s-master scheduler]# 
 4 [root@k8s-master scheduler]# kubectl get deploy -o wide
 5 NAME                        READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                      SELECTOR
 6 scheduler-nodename-deploy   0/5     5            0           6s    myapp-pod    registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1   app=myapp
 7 [root@k8s-master scheduler]# 
 8 [root@k8s-master scheduler]# kubectl get rs -o wide
 9 NAME                                  DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                                                      SELECTOR
10 scheduler-nodename-deploy-d5c9574bd   5         5         5       15s   myapp-pod    registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1   app=myapp,pod-template-hash=d5c9574bd
11 [root@k8s-master scheduler]# 
12 [root@k8s-master scheduler]# kubectl get pod -o wide
13 NAME                                        READY   STATUS    RESTARTS   AGE   IP             NODE         NOMINATED NODE   READINESS GATES
14 scheduler-nodename-deploy-d5c9574bd-6l9d8   1/1     Running   0          23s   10.244.0.123   k8s-master   <none>           <none>
15 scheduler-nodename-deploy-d5c9574bd-c82cc   1/1     Running   0          23s   10.244.0.119   k8s-master   <none>           <none>
16 scheduler-nodename-deploy-d5c9574bd-dkkjg   1/1     Running   0          23s   10.244.0.122   k8s-master   <none>           <none>
17 scheduler-nodename-deploy-d5c9574bd-hcn77   1/1     Running   0          23s   10.244.0.121   k8s-master   <none>           <none>
18 scheduler-nodename-deploy-d5c9574bd-zstjx   1/1     Running   0          23s   10.244.0.120   k8s-master   <none>           <none>

由上可見,yaml文件中nodeName: k8s-master生效,全部pod被調度到了k8s-master節點。若是這裏是nodeName: k8s-node02,那麼就會直接調度到k8s-node02節點。app

 

當nodeName指定節點不存在

要運行的yaml文件ide

 1 [root@k8s-master scheduler]# pwd
 2 /root/k8s_practice/scheduler
 3 [root@k8s-master scheduler]# cat scheduler_nodeName_02.yaml 
 4 apiVersion: apps/v1
 5 kind: Deployment
 6 metadata:
 7   name: scheduler-nodename-deploy
 8   labels:
 9     app: nodename-deploy
10 spec:
11   replicas: 5
12   selector:
13     matchLabels:
14       app: myapp
15   template:
16     metadata:
17       labels:
18         app: myapp
19     spec:
20       containers:
21       - name: myapp-pod
22         image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1
23         imagePullPolicy: IfNotPresent
24         ports:
25           - containerPort: 80
26       # 指定節點運行,該節點不存在
27       nodeName: k8s-node08

 

運行yaml文件並查看信息google

 1 [root@k8s-master scheduler]# kubectl apply -f scheduler_nodeName_02.yaml 
 2 deployment.apps/scheduler-nodename-deploy created
 3 [root@k8s-master scheduler]# 
 4 [root@k8s-master scheduler]# kubectl get deploy -o wide
 5 NAME                        READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                      SELECTOR
 6 scheduler-nodename-deploy   0/5     5            0           4s    myapp-pod    registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1   app=myapp
 7 [root@k8s-master scheduler]# 
 8 [root@k8s-master scheduler]# kubectl get rs -o wide
 9 NAME                                   DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                                                      SELECTOR
10 scheduler-nodename-deploy-75944bdc5d   5         5         0       9s    myapp-pod    registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1   app=myapp,pod-template-hash=75944bdc5d
11 [root@k8s-master scheduler]# 
12 [root@k8s-master scheduler]# kubectl get pod -o wide
13 NAME                                         READY   STATUS    RESTARTS   AGE   IP       NODE         NOMINATED NODE   READINESS GATES
14 scheduler-nodename-deploy-75944bdc5d-c8f5d   0/1     Pending   0          13s   <none>   k8s-node08   <none>           <none>
15 scheduler-nodename-deploy-75944bdc5d-hfdlv   0/1     Pending   0          13s   <none>   k8s-node08   <none>           <none>
16 scheduler-nodename-deploy-75944bdc5d-q9qgt   0/1     Pending   0          13s   <none>   k8s-node08   <none>           <none>
17 scheduler-nodename-deploy-75944bdc5d-q9zl7   0/1     Pending   0          13s   <none>   k8s-node08   <none>           <none>
18 scheduler-nodename-deploy-75944bdc5d-wxsnv   0/1     Pending   0          13s   <none>   k8s-node08   <none>           <none>

由上可見,若是指定的節點不存在,則容器將不會運行,一直處於Pending 狀態。

 

nodeSelector調度

nodeSelector是節點選擇約束的最簡單推薦形式。nodeSelector是PodSpec的領域。它指定鍵值對的映射。

Pod.spec.nodeSelector是經過Kubernetes的label-selector機制選擇節點,由調度器調度策略匹配label,然後調度Pod到目標節點,該匹配規則屬於【強制】約束。因爲是調度器調度,所以不能越過Taints污點進行調度。

 

nodeSelector示例

獲取當前的節點信息

1 [root@k8s-master ~]# kubectl get node -o wide --show-labels
2 NAME         STATUS   ROLES    AGE   VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION           CONTAINER-RUNTIME   LABELS
3 k8s-master   Ready    master   42d   v1.17.4   172.16.1.110   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://19.3.8     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=
4 k8s-node01   Ready    <none>   42d   v1.17.4   172.16.1.111   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://19.3.8     beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01,kubernetes.io/os=linux
5 k8s-node02   Ready    <none>   42d   v1.17.4   172.16.1.112   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://19.3.8     beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02,kubernetes.io/os=linux

 

添加label標籤

運行kubectl get nodes以獲取羣集節點的名稱。而後能夠對指定節點添加標籤。好比:k8s-node01的磁盤爲SSD,那麼添加disk-type=ssd;k8s-node02的CPU核數高,那麼添加cpu-type=hight;若是爲Web機器,那麼添加service-type=web。怎麼添加標籤能夠根據實際規劃狀況而定。

 1 ### 給k8s-node01 添加指定標籤
 2 [root@k8s-master ~]# kubectl label nodes k8s-node01 disk-type=ssd
 3 node/k8s-node01 labeled
 4 #### 刪除標籤命令 kubectl label nodes k8s-node01 disk-type-
 5 [root@k8s-master ~]# 
 6 [root@k8s-master ~]# kubectl get node --show-labels
 7 NAME         STATUS   ROLES    AGE   VERSION   LABELS
 8 k8s-master   Ready    master   42d   v1.17.4   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=
 9 k8s-node01   Ready    <none>   42d   v1.17.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disk-type=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01,kubernetes.io/os=linux
10 k8s-node02   Ready    <none>   42d   v1.17.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02,kubernetes.io/os=linux

由上可見,已經爲k8s-node01節點添加了disk-type=ssd 標籤。

 

當nodeSelector標籤存在

要運行的yaml文件

 1 [root@k8s-master scheduler]# pwd
 2 /root/k8s_practice/scheduler
 3 [root@k8s-master scheduler]# 
 4 [root@k8s-master scheduler]# cat scheduler_nodeSelector.yaml 
 5 apiVersion: apps/v1
 6 kind: Deployment
 7 metadata:
 8   name: scheduler-nodeselector-deploy
 9   labels:
10     app: nodeselector-deploy
11 spec:
12   replicas: 5
13   selector:
14     matchLabels:
15       app: myapp
16   template:
17     metadata:
18       labels:
19         app: myapp
20     spec:
21       containers:
22       - name: myapp-pod
23         image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1
24         imagePullPolicy: IfNotPresent
25         ports:
26           - containerPort: 80
27       # 指定節點標籤選擇,且標籤存在
28       nodeSelector:
29         disk-type: ssd

 

運行yaml文件並查看信息

 1 [root@k8s-master scheduler]# kubectl apply -f scheduler_nodeSelector.yaml 
 2 deployment.apps/scheduler-nodeselector-deploy created
 3 [root@k8s-master scheduler]# 
 4 [root@k8s-master scheduler]# kubectl get deploy -o wide
 5 NAME                            READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                      SELECTOR
 6 scheduler-nodeselector-deploy   5/5     5            5           10s   myapp-pod    registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1   app=myapp
 7 [root@k8s-master scheduler]# 
 8 [root@k8s-master scheduler]# kubectl get rs -o wide
 9 NAME                                       DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                                                      SELECTOR
10 scheduler-nodeselector-deploy-79455db454   5         5         5       14s   myapp-pod    registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1   app=myapp,pod-template-hash=79455db454
11 [root@k8s-master scheduler]# 
12 [root@k8s-master scheduler]# kubectl get pod -o wide
13 NAME                                             READY   STATUS    RESTARTS   AGE   IP             NODE         NOMINATED NODE   READINESS GATES
14 scheduler-nodeselector-deploy-79455db454-745ph   1/1     Running   0          19s   10.244.4.154   k8s-node01   <none>           <none>
15 scheduler-nodeselector-deploy-79455db454-bmjvd   1/1     Running   0          19s   10.244.4.151   k8s-node01   <none>           <none>
16 scheduler-nodeselector-deploy-79455db454-g5cg2   1/1     Running   0          19s   10.244.4.153   k8s-node01   <none>           <none>
17 scheduler-nodeselector-deploy-79455db454-hw8jv   1/1     Running   0          19s   10.244.4.152   k8s-node01   <none>           <none>
18 scheduler-nodeselector-deploy-79455db454-zrt8d   1/1     Running   0          19s   10.244.4.155   k8s-node01   <none>           <none>

由上可見,全部pod都被調度到了k8s-node01節點。固然若是其餘節點也有disk-type=ssd 標籤,那麼pod也會調度到這些節點上。

 

當nodeSelector標籤不存在

要運行的yaml文件

 1 [root@k8s-master scheduler]# pwd
 2 /root/k8s_practice/scheduler
 3 [root@k8s-master scheduler]# 
 4 [root@k8s-master scheduler]# cat scheduler_nodeSelector_02.yaml 
 5 apiVersion: apps/v1
 6 kind: Deployment
 7 metadata:
 8   name: scheduler-nodeselector-deploy
 9   labels:
10     app: nodeselector-deploy
11 spec:
12   replicas: 5
13   selector:
14     matchLabels:
15       app: myapp
16   template:
17     metadata:
18       labels:
19         app: myapp
20     spec:
21       containers:
22       - name: myapp-pod
23         image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1
24         imagePullPolicy: IfNotPresent
25         ports:
26           - containerPort: 80
27       # 指定節點標籤選擇,且標籤不存在
28       nodeSelector:
29         service-type: web

 

運行yaml文件並查看信息

 1 [root@k8s-master scheduler]# kubectl apply -f scheduler_nodeSelector_02.yaml 
 2 deployment.apps/scheduler-nodeselector-deploy created
 3 [root@k8s-master scheduler]# 
 4 [root@k8s-master scheduler]# kubectl get deploy -o wide
 5 NAME                            READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                      SELECTOR
 6 scheduler-nodeselector-deploy   0/5     5            0           26s   myapp-pod    registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1   app=myapp
 7 [root@k8s-master scheduler]# 
 8 [root@k8s-master scheduler]# kubectl get rs -o wide
 9 NAME                                       DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                                                      SELECTOR
10 scheduler-nodeselector-deploy-799d748db6   5         5         0       30s   myapp-pod    registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1   app=myapp,pod-template-hash=799d748db6
11 [root@k8s-master scheduler]# 
12 [root@k8s-master scheduler]# kubectl get pod -o wide
13 NAME                                             READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
14 scheduler-nodeselector-deploy-799d748db6-92mqj   0/1     Pending   0          40s   <none>   <none>   <none>           <none>
15 scheduler-nodeselector-deploy-799d748db6-c2w25   0/1     Pending   0          40s   <none>   <none>   <none>           <none>
16 scheduler-nodeselector-deploy-799d748db6-c8tlx   0/1     Pending   0          40s   <none>   <none>   <none>           <none>
17 scheduler-nodeselector-deploy-799d748db6-tc5n7   0/1     Pending   0          40s   <none>   <none>   <none>           <none>
18 scheduler-nodeselector-deploy-799d748db6-z8c57   0/1     Pending   0          40s   <none>   <none>   <none>           <none>

由上可見,若是nodeSelector匹配的標籤不存在,則容器將不會運行,一直處於Pending 狀態。

 

相關閱讀

一、官網:Pod分配調度

二、Kubernetes K8S之調度器kube-scheduler詳解

三、Kubernetes K8S之affinity親和性與反親和性詳解與示例

四、Kubernetes K8S之Taints污點與Tolerations容忍詳解

完畢!

 


 

 

———END———
若是以爲不錯就關注下唄 (-^O^-) !

相關文章
相關標籤/搜索