一、Pod调度简介
1.1 调度简介
在默认情况下,一个Pod在哪个Node节点上运行,是由scheduler组件采用相应的算法计算出来的,这个过程是不受人工控制的,但是在实际使用中,这并不满足要求,因为在很多情况下,我们想控制某些Pod在某些某些节点上,这就要求Kubernetes能对Pod进行调度,Kubernetes提供了四种调度方式
- 自动调度:由scheduler计算得出
- 定向调度:NodeName,NodeSelector
- 亲和性调度:NodeAffinity,PodAffinity,PodAntiAffinity
- 污点(容忍)调度:Taints,Toleration
1.2 定向调度简介
定向调度,指的是利用在pod上声明nodeName或者nodeSelector,以此将Pod调度到期望的node节点上,注意,这里所说的调度是强制的,这意味着即使调度的Node不存在,也会向上面进行调度,只不过pod运行失败而已
二、定向调度实例
2.1 基于NodeName定向调度
编辑pod_nodename.yaml文件,定向约束到node2节点,如下:
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: Pod
metadata:
name: pod-nginx
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
nodeName: node2
使用如下命令创建资源
[root@master resource_manage]# kubectl apply -f pod_nodename.yaml
namespace/dev created
pod/pod-nginx created
[root@master resource_manage]#
如下,查看可以发现此时已经调度到node2节点上了
[root@master resource_manage]# kubectl get pod -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-nginx 1/1 Running 0 10s 10.244.2.43 node2 <none> <none>
[root@master resource_manage]#
使用如下命令删除资源
[root@master resource_manage]# kubectl delete -f pod_nodename.yaml
namespace "dev" deleted
pod "pod-nginx" deleted
[root@master resource_manage]#
2.2 基于节点标签使用nodeSelector定向调度
首先给node1和node2节点打不同的标签
[root@master resource_manage]# kubectl label nodes node1 nodeenv=test
node/node1 labeled
[root@master resource_manage]# kubectl label nodes node2 nodeenv=demo
node/node2 labeled
[root@master resource_manage]# kubectl get node --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready control-plane,master 10d v1.21.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
node1 Ready <none> 10d v1.21.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node1,kubernetes.io/os=linux,nodeenv=test
node2 Ready <none> 10d v1.21.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node2,kubernetes.io/os=linux,nodeenv=demo
[root@master resource_manage]#
然后编辑pod_nodeselector.yaml文件,内容如下即将pod调度到nodeenv=demo的节点上即node2节点上
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: Pod
metadata:
name: pod-nginx
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
nodeSelector:
nodeenv: demo
使用如下命令创建资源
[root@master resource_manage]# kubectl apply -f pod_nodeselector.yaml
namespace/dev created
pod/pod-nginx created
[root@master resource_manage]#
如下,可以看到,pod如愿被调度到node2节点上
[root@master resource_manage]# kubectl get pod -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-nginx 1/1 Running 0 105s 10.244.2.44 node2 <none> <none>
[root@master resource_manage]#
最后使用如下命令删除资源
[root@master resource_manage]# kubectl delete -f pod_nodeselector.yaml
namespace "dev" deleted
pod "pod-nginx" deleted
[root@master resource_manage]#