K8s官网介绍:
https://kubernetes.io/zh/docs/tasks/configure-pod-container/assign-pods-nodes-using-node-affinity/
节点亲和性:
pod.spec.nodeAffinity
requiredDuringSchedulingIgnoredDuringExecution(硬策略)表示pod必须部署到满足条件的节点上,如果没有满足条件的节点,就不停重试。其中IgnoreDuringExecution表示pod部署成功之后运行的时候,如果节点标签发生了变化,不再满足pod指定的条件,pod也会继续运行。
preferredDuringSchedulingIgnoredDuringExecution(软策略)表示优先部署到满足条件的节点上,如果没有满足条件的节点,就忽略这些条件,按照正常逻辑部署。
键值运算关系:
In:label的值在某个列表中
NotIn:label的值不在某个列表中
Gt:label的值大于某个值
Lt:label的值小于某个值
Exists:某个label存在
DoesNotExist:某个label不存在
查看node标签属性:
[root@master ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 40d v1.17.5 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/master=
node1 Ready <none> 40d v1.17.5 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node1,kubernetes.io/os=linux
node2 Ready <none> 40d v1.17.5 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node2,kubernetes.io/os=linux
硬策略测试:
硬策略是强制性的要求,必须满足,不满足pod只能处于pending状态。
[root@master afff]# cat nodeaf.yaml
apiVersion: v1
kind: Pod
metadata:
name: aff
labels:
app: test-node
spec:
containers:
- name: test-pod
image: nginx
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: NotIn
values:
- node2
[root@master afff]# kubectl apply -f nodeaf.yaml
pod/aff created
[root@master afff]# kubectl get pod -o wide
软策略测试:
软策略定义是尽量满足,如果满足不了也可以选择其他node运行。
[root@master afff]# cat nodeaf.yaml
apiVersion: v1
kind: Pod
metadata:
name: aff
labels:
app: test-node
spec:
containers:
- name: test-pod
image: nginx
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node3
[root@master afff]# kubectl apply -f nodeaf.yaml
pod/aff created
[root@master afff]# kubectl get pod -o wide
Pod亲和性:
pod亲和性(podAffinity):和某些pod调度到同一节点
pod反亲和性(podAntiAffinity):和某些pod调度在不同节点
亲和性调度可以分成两种策略:
硬策略(requiredDuringSchedulingIgnoredDuringExecution):
指定了将 pod 调度到一个节点上必须满足的规则,不满足则会处于pending状态,一直进行重试,直到满足为止。
软策略(preferredDuringSchedulingIgnoredDuringExecution):
优先满足指定的规则,如果不满足则会调度到其他节点上。如果有多个软策略还可以设置优先级(weight)
首先创建一个pod,打上自定标签:
创建一个nginx pod:
[root@master afff]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-example
labels:
app: nginx
spec:
containers:
- name: test-nginx
image: nginx
查看pod标签:
[root@master afff]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-example 1/1 Running 0 4m40s app=nginx
Pod亲和性:指定pod亲和性标签是nginx:
[root@master afff]# cat pod2.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod2
labels:
app: test-node
spec:
containers:
- name: test-pod
image: nginx
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- topologyKey: kubernetes.io/hostname
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- nginx
[root@master afff]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-example 1/1 Running 0 21m 10.244.2.55 node2 <none> <none>
pod2 0/1 ContainerCreating 0 23s <none> node2 <none> <none>
Pod反亲和性:
[root@master afff]# cat pod2.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod2
labels:
app: test-node
spec:
containers:
- name: test-pod
image: nginx
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- topologyKey: kubernetes.io/hostname
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- nginx
[root@master afff]# kubectl apply -f pod2.yaml
pod/pod2 created
[root@master afff]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-example 1/1 Running 0 26m 10.244.2.55 node2 <none> <none>
pod2 0/1 ContainerCreating 0 7s <none> node1 <none> <none>