[关闭]
@weakiwi 2017-05-19T07:50:55.000000Z 字数 2839 阅读 889

Inter-pod topological affinity and anti-affinity(pod之间拓扑上的关联与反关联)

翻译


podAffinity

requiredDuringSchedulingIgnoredDuringExecution

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: test1
  5. spec:
  6. affinity:
  7. podAffinity:
  8. requiredDuringSchedulingIgnoredDuringExecution:
  9. - labelSelector:
  10. matchExpressions:
  11. - key: "mysvc"
  12. operator: In
  13. values:
  14. - S1
  15. topologyKey: kubernetes.io/hostname
  16. containers:
  17. - name: dao-2048
  18. image: daocloud.io/daocloud/dao-2048
  19. imagePullPolicy: IfNotPresent
  1. 通过该文件创建得到的pod,一直处于pending状态,错误原因如下图
    ,可知当使用requiredDuringSchedulingIgnoredDuringExecution参数时,如果无法满足affinity条件,pod无法启动
  2. 执行kubectl label po test1 mysvc=S1``kubectl label po test1 mysvc=S1修改pod标签使pod满足条件,通过kubectl get pod,可观察到pod状态变为running。再执行kubectl label po test1 mysvc-删除pod的标签,使pod不满足affinity条件,发现pod转态仍为running。由此确定schedule状态包含pending而且在IgnoredDuringExecutio
    n
    条件下,在执行期间发生条件改变,对pod的状态是不会产生影响的。

preferredDuringSchedulingIgnoredDuringExecution

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: test1
  5. spec:
  6. affinity:
  7. podAffinity:
  8. preferredDuringSchedulingIgnoredDuringExecution:
  9. - weight: 100
  10. podAffinityTerm:
  11. labelSelector:
  12. matchExpressions:
  13. - key: security
  14. operator: In
  15. values:
  16. - S1
  17. topologyKey: kubernetes.io/hostname
  18. containers:
  19. - name: dao-2048
  20. image: daocloud.io/daocloud/dao-2048
  21. imagePullPolicy: IfNotPresent

通过配置文件产生pod,观察可得pod的状态为running,由此可知preferredDuringScheduling下的affinity为非必须条件。

podAntiAffinity

requiredDuringSchedulingIgnoredDuringExecution

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: test4
  5. labels:
  6. security: S1
  7. spec:
  8. affinity:
  9. podAntiAffinity:
  10. requiredDuringSchedulingIgnoredDuringExecution:
  11. - labelSelector:
  12. matchExpressions:
  13. - key: security
  14. operator: In
  15. values:
  16. - S1
  17. topologyKey: kubernetes.io/hostname
  18. containers:
  19. - name: dao-2048
  20. image: daocloud.io/daocloud/dao-2048
  21. imagePullPolicy: IfNotPresent

使用该文件创建3个pod,可以发现3个pod运行在不同的node上。。如果再创建一个pod,可以观察到该pod处于pending状态。因为在该环境只有三台worknode,符合条件的所有worknode均被占用。

preferredDuringSchedulingIgnoredDuringExecution

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: test3
  5. labels:
  6. security: S1
  7. spec:
  8. affinity:
  9. podAntiAffinity:
  10. preferredDuringSchedulingIgnoredDuringExecution:
  11. - weight: 100
  12. podAffinityTerm:
  13. labelSelector:
  14. matchExpressions:
  15. - key: security
  16. operator: In
  17. values:
  18. - S1
  19. topologyKey: kubernetes.io/hostname
  20. containers:
  21. - name: dao-2048
  22. image: daocloud.io/daocloud/dao-2048
  23. imagePullPolicy: IfNotPresent

使用该配置模板创建三个pod,可以发现pod依旧分配到了不同的节点上。当创建第四个pod时,第四个pod能够被顺利创建,说明preferredDuringScheduling在podAntiAnffinity下也是不严格匹配规则。

podAntiAffinity的对称性

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: test3
  5. labels:
  6. security: S1
  7. spec:
  8. containers:
  9. - name: dao-2048
  10. image: daocloud.io/daocloud/dao-2048
  11. imagePullPolicy: IfNotPresent

以及

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: test2
  5. labels:
  6. security: S1
  7. spec:
  8. affinity:
  9. podAntiAffinity:
  10. requiredDuringSchedulingIgnoredDuringExecution:
  11. - labelSelector:
  12. matchExpressions:
  13. - key: security
  14. operator: In
  15. values:
  16. - S1
  17. topologyKey: kubernetes.io/hostname
  18. containers:
  19. - name: dao-2048
  20. image: daocloud.io/daocloud/dao-2048
  21. imagePullPolicy: IfNotPresent

先使用第二个配置文件创建两个pod,再用第一个文件创建一个pod,可以观察到,三个pod在三个不同的node上。,说明当设置test1对test2排他,不用设置test2对test1排他。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注