Mark Robinson
Mark Robinson

Reputation: 3165

How can I target a dynamic label using podAntiAffinity?

What I would like is to use podAntiAffinity to limit the number of pods I run on a host of the same version of code.

Specifically, I would like to run 1 pod of version A, and 1 pod of version B. This is to allow to canary deploys without spinning up a large number of new nodes.

I have tried setting my podAntiAffinity

        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - podAffinityTerm:
                labelSelector:
                - matchExpressions:
                  - key: "k8s.git/commit-sha"
                    operator: In
                    values:
                    - valueFrom:
                        fieldRef:
                          fieldPath: "metadata.labels['k8s.git/commit-sha']"
                topologyKey: kubernetes.io/hostname
              weight: 100

But looking at the source code for k8s, it expected a string object instead of a map object.

Is there another way to accomplish this? Has anyone implemented something similar? I'm running Kubernetes 1.18.

Upvotes: 0

Views: 755

Answers (1)

gohm'c
gohm'c

Reputation: 15530

I had this thought before and I resolved it using the method describe here Inter-pod affinity. In short, the method deploy to nodes that already run pod that match the intend label - which in your case the commit-sha.

Upvotes: 1

Related Questions