转载自:https://blog.csdn.net/jettery/article/details/79003562
现实中应用的运行对于kubernetes在亲和性上提出了一些要求,可以归类到以下几个方面:
1.Pod固定调度到某些节点之上
2.Pod不会调度到某些节点之上
3.Pod的多副本调度到相同的节点之上
4.Pod的多副本调度到不同的节点之上
实践
Pod调动到某些节点上
Pod的定义中通过nodeSelector指定label标签,pod将会只调度到具有该标签的node之上
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
nodeSelector:
disktype: ssd
这个例子中pod只会调度到具有disktype=ssd的node上面.
Affinity/anti-affinity node
Affinity/anti-affinity node 相对于nodeSelector机制更加的灵活和丰富
requiredDuringSchedulingIgnoredDuringExecution表示必须满足亲和性配置;
preferredDuringSchedulingIgnoredDuringExecution表示优先选择满足亲和性配置,无法满足时会选择非nodeSelector 的节点。
topologyKey可以设置成如下几种类型
kubernetes.io/hostname #Node
failure-domain.beta.kubernetes.io/zone #Zone
failure-domain.beta.kubernetes.io/region #Region
可以设置node上的label的值来表示node的name,zone,region等信息,pod的规则中指定topologykey的值表示指定topology范围内的node上运行的pod满足指定规则
例1:不会选择已经运行了label为app:store的pod的节点,而且3个副本不会运行在相同的节点上(topologyKey: "kubernetes.io/hostname")。
apiVersion: apps/v1beta1 # for versions before 1.6.0 use extensions/v1beta1
kind: Deployment
metadata:
name: redis-cache
spec:
replicas: 3
template:
metadata:
labels:
app: store
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- store
topologyKey: "kubernetes.io/hostname"
containers:
- name: redis-server
image: redis:3.2-alpine
例2:在一些应用中,pod副本之间需要共享cache,需要将pod运行在一个节点之上
apiVersion: apps/v1beta1 # for versions before 1.6.0 use extensions/v1beta1
kind: Deployment
metadata:
name: web-server
spec:
replicas: 3
template:
metadata:
labels:
app: web-store
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- web-store
topologyKey: "kubernetes.io/hostname"
containers:
- name: web-app
image: hub.easystack.io/library/nginx:1.9.0