k8s学习(23):固定节点调度

指定调度节点

1 Pod.spec.nodeName将Pod直接调度到指定的Node节点上,会跳过Scheduler的调度策略,该匹配规则是强制匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
myweb
spec:
replicas: 7
template:
metadata:
labels:
app: myweb
spec:
nodeName: k8s-node01
containers:
- name: myweb
image: hub.test.com/library/myapp:v1
ports:
- containerPort: 80

运行一下看看效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@k8s-master node]# kubectl apply -f deploy.yaml 
deployment.extensions/myweb created
[root@k8s-master node]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp 1/1 Running 0 25m
myweb-7bff67bccc-4wbpm 0/1 ContainerCreating 0 2s
myweb-7bff67bccc-5m24f 0/1 ContainerCreating 0 2s
myweb-7bff67bccc-8z6dh 0/1 ContainerCreating 0 2s
myweb-7bff67bccc-9gdlp 0/1 ContainerCreating 0 2s
myweb-7bff67bccc-9vnnz 0/1 ContainerCreating 0 2s
myweb-7bff67bccc-rzckd 0/1 ContainerCreating 0 2s
myweb-7bff67bccc-s49tp 0/1 ContainerCreating 0 2s
myweb-7bff67bccc-td6kn 0/1 ContainerCreating 0 2s
myweb-7bff67bccc-vrpsn 0/1 ContainerCreating 0 2s
myweb-7bff67bccc-w7568 0/1 ContainerCreating 0 2s
pod-taint-toleration 1/1 Running 0 13m
[root@k8s-master node]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 25m 10.244.1.120 k8s-node01 <none> <none>
myweb-7bff67bccc-4wbpm 1/1 Running 0 6s 10.244.1.123 k8s-node01 <none> <none>
myweb-7bff67bccc-5m24f 1/1 Running 0 6s 10.244.1.128 k8s-node01 <none> <none>
myweb-7bff67bccc-8z6dh 1/1 Running 0 6s 10.244.1.130 k8s-node01 <none> <none>
myweb-7bff67bccc-9gdlp 1/1 Running 0 6s 10.244.1.121 k8s-node01 <none> <none>
myweb-7bff67bccc-9vnnz 1/1 Running 0 6s 10.244.1.122 k8s-node01 <none> <none>
myweb-7bff67bccc-rzckd 1/1 Running 0 6s 10.244.1.124 k8s-node01 <none> <none>
myweb-7bff67bccc-s49tp 1/1 Running 0 6s 10.244.1.127 k8s-node01 <none> <none>
myweb-7bff67bccc-td6kn 1/1 Running 0 6s 10.244.1.125 k8s-node01 <none> <none>
myweb-7bff67bccc-vrpsn 1/1 Running 0 6s 10.244.1.129 k8s-node01 <none> <none>
myweb-7bff67bccc-w7568 1/1 Running 0 6s 10.244.1.126 k8s-node01 <none> <none>
pod-taint-toleration 1/1 Running 0 13m 10.244.2.210 k8s-node02 <none> <none>

可以看到,所有的Pod被调度到k8s-node01节点上

2 Pod.spec.nodeSelector:通过kubernetes的label-selector机制选择节点,由调度器调度策略匹配label,而后调度Pod到目标节点,该匹配规则属于强制约束

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: myweb-2
spec:
replicas: 2
template:
metadata:
labels:
app: myweb-2
spec:
nodeSelector:
disk: ssd
containers:
- name: myweb-2
image: hub.test.com/library/myapp:v1
ports:
- containerPort: 80

运行该deploy

1
2
3
4
5
6
7
[root@k8s-master node]# kubectl apply -f deploy2.yaml 
deployment.extensions/myweb created
[root@k8s-master node]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb-689f546447-8tftj 0/1 Pending 0 3s
myweb-689f546447-xsdz4 0/1 Pending 0 3s
myweb-689f546447-z2zp9 0/1 Pending 0 3s

设置node label为disk=ssd,可以看到pod成功被调度到node上

1
2
3
4
5
6
7
[root@k8s-master node]# kubectl label node k8s-node01 disk=ssd
node/k8s-node01 labeled
[root@k8s-master node]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb-689f546447-8tftj 1/1 Running 0 60s
myweb-689f546447-xsdz4 1/1 Running 0 60s
myweb-689f546447-z2zp9 1/1 Running 0 60s