Pod Priority and Affinity in juju/charms

Hi,

I was wondering what the approach was with regards to affinity and anti-affinity of containers within juju. I had a couple of questions:

  • How can I set affinity/anti-affinity in juju?
  • How can I set pod priority in juju?
  • Would it be the charm a charm option or a juju option?
  • How can I create a charm which takes advantage these features?

Cheers for the help
Peter

One mechanism via which affinity/anti-affinity is currently supported via tag constraints.

juju deploy foo --constraints="tags=foo=a|b|c,^bar=d|e|f"

would result in a pod with an node selector expression of “foo in a|b|c” and "bar not in “d|e|f”.

Zone constraints, if specifed, map to a node selector with key
failure-domain.beta.kubernetes.io/zone

Currently, constraints are a deploy time option; Juju doesn’t support the concept of charm constraints as a thing (although we have talked about it at various times).

Pod priority is simply a vale in the pod spec

...
kubernetesResources:
  pod:
    priorityClassName: system-cluster-critical
    priority: 2000000000

Great thanks @wallyworld!

Hello @wallyworld,

I’d like to ask something related to the affinity and anti-affinity rules. Apparently, the constraints allow us to implement (anti-)affinity rules at the kubernetes node level.

If you see this documentation, you will see that there’s another level of (anti-)affinity at the inter-pod level. To, for instance, have different pods of the same application (units) allocated in different nodes.

Is this podAntiAffinity field possible in the current podSpec?

apiVersion: v1
kind: Pod
metadata:
  name: with-pod-affinity
spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: security
            operator: In
            values:
            - S1
        topologyKey: topology.kubernetes.io/zone
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: security
              operator: In
              values:
              - S2
          topologyKey: topology.kubernetes.io/zone
  containers:
  - name: with-pod-affinity
    image: k8s.gcr.io/pause:2.0

At the moment, only node affinity is supported via Juju constraint tags.
eg

juju deploy myapp --constraints "tags=foo=bar|bar2,^baz=bar"

would create a pod with node affinity (hopefully no typos)

  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: foo
            operator: In
            values:
            - bar
            - bar2
          - key: baz
            operator: NotIn
            values:
            - bar

An initial thought is that we could look to extend the syntax to account for “node” and “pod” affinity

juju deploy myapp --constraints "tags=node.foo=bar,pod.baz=bar2"

(default would be “node”).

We’d need to think it through to be comfortable with the syntax etc; there’s limited options available whilst preserving compatibility with what can be supported.