kubernetes提供了一种亲和性调度(Affinity)。它在NodeSelector的基础之上的进行了扩展,可以通过配置的形式,实现优先选择满足条件的Node进行调度,如果没有,也可以调度到不满足条件的节点上,使调度更加灵活。
Affinity主要分为三类:
- nodeAffinity(node亲和性): 以node为目标,解决pod可以调度到哪些node的问题
- podAffinity(pod亲和性) : 以pod为目标,解决pod可以和哪些已存在的pod部署在同一个拓扑域中的问题
- podAntiAffinity(pod反亲和性) : 以pod为目标,解决pod不能和哪些已存在pod部署在同一个拓扑域中的问题
首先我们先看下nodeAffinity,同样我们举个例子,方便理解
nodeAffinity
首先创建pod.yaml
这里使用的是 requiredDuringSchedulingIgnoredDuringExecution
硬相关,没有匹配到,则返回失败
apiVersion: v1
kind: Pod
metadata:
name: nodeaffinity #pod名称
namespace: dev
spec:
containers:
- name: nginx
image: nginx: latest
affinity: #亲和性设置
nodeAffinity: #设置node亲和性
requiredDuringSchedulingIgnoredDuringExecution: # 硬限制
nodeSelectorTerms:
- matchExpressions: # 匹配env的值在["aa","bb"]中的标签
- key: nodeenv
operator: In
values: ["aa","bb"]
生成yaml对应的pod
# 创建pod
[root@k8s-master01 ~]# kubectl create -f pod.yaml
pod/nodeaffinity created
# 查看pod状态 (运行失败)
[root@k8s-master01 ~]# kubectl get pods nodeaffinity -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE ......
nodeaffinity 0/1 Pending 0 6s <none> <none> ......
# 查看Pod的详情
# 发现调度失败,提示node选择失败
[root@k8s-master01 ~]# kubectl describe pod nodeaffinity -n dev
......
#接下来,停止pod
[root@k8s-master01 ~]# kubectl delete -f pod.yaml
pod "nodeaffinity" deleted
# 修改文件,将values: ["aa","bb"]------> ["pro","pod"]
[root@k8s-master01 ~]# vim pod.yaml
# 再次启动
[root@k8s-master01 ~]# kubectl create -f pod.yaml
pod/nodeaffinity created
# 此时查看,发现调度成功,已经将pod调度到了node1上
[root@k8s-master01 ~]# kubectl get pods nodeaffinity -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE ......
nodeaffinity 1/1 Running 0 11s 10.200.1.9 node1 ......
接下来再演示一下requiredDuringSchedulingIgnoredDuringExecution
同样先创建pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nodeaffinity
namespace: dev
spec:
containers:
- name: nginx
image: nginx:latest
affinity: #亲和性设置
nodeAffinity: #设置node亲和性
preferredDuringSchedulingIgnoredDuringExecution: # 软限制 优先匹配设定的规则,没有匹配也可以运行
- weight: 1
preference:
matchExpressions: # 匹配env的值在["dev","test"]中的标签(当前环境没有)
- key: nodeenv
operator: In
values: ["dev","test"]
# 创建pod
[root@k8s-master01 ~]# kubectl create -f pod.yaml
pod/nodeaffinity created
# 查看pod状态 (运行成功),即使没有这样标签的node
[root@k8s-master01 ~]# kubectl get pod nodeaffinity -n dev
NAME READY STATUS RESTARTS AGE
nodeaffinity 1/1 Running 0 40s
NodeAffinity规则设置的注意事项:
1 如果同时定义了nodeSelector和nodeAffinity,那么必须两个条件都得到满足,Pod才能运行在指定的Node上
2 如果nodeAffinity指定了多个nodeSelectorTerms,那么只需要其中一个能够匹配成功即可
3 如果一个nodeSelectorTerms中有多个matchExpressions ,则一个节点必须满足所有的才能匹配成功
4 如果一个pod所在的Node在Pod运行期间其标签发生了改变,不再符合该Pod的节点亲和性需求,则系统将忽略此变化
PodAffinity
PodAffinity主要实现以运行的Pod为参照,实现让新创建的Pod跟参照pod在一个区域的功能,前面是以Node为参照。
同样首先创建一个参照Pod yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-target
namespace: dev
labels:
podenv: pro #设置标签
spec:
containers:
- name: nginx
image: nginx: latest
nodeName: node1 # 将目标pod名确指定到node1上
# 启动目标pod
[root@k8s-master01 ~]# kubectl create -f pod-target.yaml
pod/pod-target created
# 查看pod状况
[root@k8s-master01 ~]# kubectl get pods pod-target -n dev
NAME READY STATUS RESTARTS AGE
pod-target 1/1 Running 0 4s
2)创建pod-podaffinity-required.yaml,内容如下:
apiVersion: v1
kind: Pod
metadata:
name: pod-podaffinity-required
namespace: dev
spec:
containers:
- name: nginx
image: nginx: latest
affinity: #亲和性设置
podAffinity: #设置pod亲和性
requiredDuringSchedulingIgnoredDuringExecution: # 硬限制 同样还有软连接效果类似,只是即使未匹配依然可以运行
- labelSelector:
matchExpressions: # 匹配env的值在["aa","bb"]中的标签
- key: podenv
operator: In
values: ["aa","bb"]
topologyKey: kubernetes.io/hostname
# 启动pod
[root@k8s-master01 ~]# kubectl create -f pod-podaffinity-required.yaml
pod/pod-podaffinity-required created
# 查看pod状态,发现未运行
[root@k8s-master01 ~]# kubectl get pods pod-podaffinity-required -n dev
NAME READY STATUS RESTARTS AGE
pod-podaffinity-required 0/1 Pending 0 9s
# 查看详细信息
[root@k8s-master01 ~]# kubectl describe pods pod-podaffinity-required -n dev
......
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling <unknown> default-scheduler 0/3 nodes are available: 2 node(s) didn't match pod affinity rules, 1 node(s) had taints that the pod didn't tolerate.
# 接下来修改 values: ["aa","bb"]----->values:["pro","pod"]
[root@k8s-master01 ~]# vim pod-podaffinity-required.yaml
# 然后重新创建pod,查看效果
[root@k8s-master01 ~]# kubectl delete -f pod-podaffinity-required.yaml
pod "pod-podaffinity-required" deleted
[root@k8s-master01 ~]# kubectl create -f pod-podaffinity-required.yaml
pod/pod-podaffinity-required created
# 发现此时Pod运行正常
[root@k8s-master01 ~]# kubectl get pods pod-podaffinity-required -n dev
NAME READY STATUS RESTARTS AGE LABELS
pod-podaffinity-required 1/1 Running 0 6s <none>
podAntiAffinity
这个与podAAffinity 使用方式完全一致,只是效果相反,不再赘述。