zhangguanzhang's Blog

kubernetes的PodAffinity的不解

字数统计: 553阅读时长: 2 min
2018/06/02

官方文档来看是下面的说法,摘自官方文档https://kubernetes.io/docs/concepts/configuration/assign-pod-node/

  • An example of a pod that uses pod affinity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
apiVersion: v1
kind: Pod
metadata:
name: with-pod-affinity
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: security
operator: In
values:
- S1
topologyKey: failure-domain.beta.kubernetes.io/zone
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: security
operator: In
values:
- S2
topologyKey: kubernetes.io/hostname
containers:
- name: with-pod-affinity
image: k8s.gcr.io/pause:2.0

More precisely, the pod is eligible to run on node N if node N has a label with key failure-domain.beta.kubernetes.io/zone and some value V such that there is at least one node in the cluster with key failure-domain.beta.kubernetes.io/zone and value V that is running a pod that has a label with key “security” and value “S1”

pod能够落在的node N 必须满足:N有一个key为failure-domain.beta.kubernetes.io/zone并且value为某个值V的标签,这样集群中就至少有一个node拥有这个标签,并且有一个 key 为 “security” 并且 value 为 “S1” 的标签的 pod 在之上运行

这里的demo的yaml里根本就没有判断failure-domain.beta.kubernetes.io/zone的值,文档里却说and value V
顺着官方的说法也就是说存在这个键的node去判断

  • 但是现实里使用的更多是去根据值去判断node,例如
1
2
3
4
area: beijing
area: shanghai
disktype: ide
disktype: ssd

甚至自己的label都用这样值去区分

1
2
3
4
5
6
[root@k8s-m1 k8s]# kubectl get nodes --show-labels 
NAME STATUS ROLES AGE VERSION LABELS
k8s-m1 Ready master 6d v1.10.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=k8s-m1,node-role.kubernetes.io/master=
k8s-m2 Ready master 6d v1.10.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=k8s-m2,node-role.kubernetes.io/master=
k8s-m3 Ready master 6d v1.10.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=k8s-m3,node-role.kubernetes.io/master=
k8s-n1 Ready node 6d v1.10.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=k8s-n1,node-role.kubernetes.io/node=
  • 那按照官方的这个去使用就是下面的这样的标签了
1
2
disktype-ssd: true
disktype-ide: true

这样很反人类了
如果说有这样一个需求:
把该pod亲和到ssd硬盘的node上

1
topologyKey: disktype-ssd

还不如下面好用

1
2
3
4
topology: 
key: disktype
operator: In
value: ssd

搜索了下一大堆人在哪瞎扯,说topologyKey是zone,事实我给node加上一个lable都可以亲和上去,而不是看值的

CATALOG