親和性和反親和性
node 親和性
node親和性策略表示pod部署到符合某些條件的node上.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: name
operator: In
values:
- nginx1
- nginx2
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: app
operator: In
values:
- nginx
containers:
- name: nginx-server
image: nginx:latest
上面的這個例子表示pod必須部署到滿足nodeSelectorTerms中的條件,盡量滿足preference中的條件.
- nodeAffinity表示pod和node的親和性策略
- nodeSelectorTerms 表示打了這些標(biāo)簽的node上才能部署pod,這里的key values都可以指定多個值,可以實現(xiàn)類似nodeSelector的或的操作.
- preference 表示最好部署到滿足這些條件的node上.
策略
requiredDuringSchedulingIgnoredDuringExecution 硬策略表示調(diào)度過程必須滿足執(zhí)行過程忽略
preferredDuringSchedulingIgnoredDuringExecution 軟策略表示調(diào)度過程盡量滿足執(zhí)行過程忽略
表達式
label 的值可選的操作符有:
- In:label 的值在某個列表中
- NotIn:label 的值不在某個列表中
- Exists:某個 label 存在
- DoesNotExist: 某個 label 不存在
- Gt:label 的值大于某個值(字符串比較)
- Lt:label 的值小于某個值(字符串比較
pod 親和性和非親和性
pod親和性和非親和性表示pod部署到或不部署到滿足某些label的pod所在的node上.
- 使用pod非親和性硬策略,實現(xiàn)相同lable pod一定不能調(diào)度到同一節(jié)點
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- nginx
topologyKey: "kubernetes.io/hostname"
containers:
- name: nginx-server
image: nginx:latest
- podAntiAffinity 表示pod的非親和性策略
- matchExpressions 表示符合label條件的pod
- topologyKey 表示作用域key,這里使用 kubernetes.io/hostname表示所有的節(jié)點,因為所有的節(jié)點默認都會打上這個標(biāo)簽
這個例子表示所有有app:nginx標(biāo)簽的pod非親和性,就是不能部署在同一節(jié)點,并且在調(diào)度過程中是硬策略.
- 使用pod非親和性軟策略實現(xiàn)相同label pod盡量不要調(diào)度到同一節(jié)點
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
nodeSelector:
app: nginx
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- nginx
topologyKey: kubernetes.io/hostname
containers:
- name: nginx
image: nginx
- podAntiAffinity 親和性策略
表示盡量把有l(wèi)abel app:nginx的pod部署在同一節(jié)點