1.概述
在创建pod资源是,k8s集群系统会给我们将pod资源随机分配到不同服务器上。我们通过配置nodeSelector可以将pod资源指定到拥有某个标签的服务器上
使用nodeselector前我们要先给每个节点打上标签,在编辑pod资源的时候选择该标签
2.示例
·给节点打标签
[root@master job]# kubectl label node master k8s=master
node/master labeled
[root@master job]# kubectl label node worker1 k8s=worker1
node/worker1 labeled
[root@master job]# kubectl label node worker2 k8s=worker2
node/worker2 labeled
·查看标签
·编辑创建资源
[root@master job]# cat nodeselector.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: dm002
spec:replicas: 5selector:matchLabels:k8s: k8stemplate:metadata:labels:k8s: k8sspec:nodeSelector:k8s: worker2containers:- name: demoooimage: nginx1.20.1-alpineports:- name: portcontainerPort: 80
[root@master job]# kubectl apply -f nodeselector.yaml
deployment.apps/dm001 created
·查看资源
可以看到node就是上面设置的我worker2节点
[root@master job]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dm002-7b46896f67-5rrq7 1/1 Running 0 8s 10.100.2.135 worker2 <none> <none>
dm002-7b46896f67-5xf6s 1/1 Running 0 8s 10.100.2.137 worker2 <none> <none>
dm002-7b46896f67-8l2kh 1/1 Running 0 8s 10.100.2.136 worker2 <none> <none>
dm002-7b46896f67-k2gq4 1/1 Running 0 8s 10.100.2.134 worker2 <none> <none>
dm002-7b46896f67-pvpk8 1/1 Running 0 8s 10.100.2.133 worker2 <none> <none>