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>