我可以在Kubernetes部署中使用多个PodAffiniy吗?

时间:2020-09-24 20:42:34

标签: kubernetes kubernetes-pod amazon-eks

我有一个Pod,如果可能的话,我想在同一个节点上运行,或者起初至少要在同一个可用区中运行,我认为我可以将podAffinity与preferredduringschedulingignoredduringexecution和两个podAffinityTerm一起使用,但是它没有似乎工作正常,所以我想如果我可以运行两个podAffinity,那么如果第一个podAffinity(在同一主机上运行)是不可能的,那么第二个podAffinity(在相同的AZ上运行)将发生。

这是我的部署清单:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-test-deployment
spec:
  selector:
    matchLabels:
      app: app-test-deployment
  replicas: 1
  template:
    metadata:
      labels:
        app: app-test-deployment
    spec:
      affinity:
        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - pod-test
              topologyKey: "kubernetes.io/hostname"
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - pod-test
              topologyKey: "failure-domain.beta.kubernetes.io/zone"
      containers:
      - name: app-test
        image: app-test:1.14.2
        ports:
        - containerPort: 80

1 个答案:

答案 0 :(得分:2)

  1. /zone的权重设置为99
  2. preferredDuringSchedulingIgnoredDuringExecution替换为requiredDuringSchedulingIgnoredDuringExecution,当它失败时-您会看到是什么导致它进入另一个可用区。完成后,将其设置回preferred

此外,您是否尝试将Affinity链接到发布的同一部署?如果是这样,则您的标签不匹配:podAffinityTerm中的部署VS app: app-test-deployment中的app: pod-test