Search code examples
kubernetes-helmnginx-ingress

How to properly use controller.affinity annotation?


Does anyone know how to properly use controller.affinity annotation? In the doc we can see only annotation name w/o type or any example. https://docs.nginx.com/nginx-ingress-controller/installation/installation-with-helm/ I was trying to use it as I did for toleration:

--set controller.affinity[0].key=key_here \
--set controller.affinity[0].operator=In \
--set controller.affinity[0].values[0]=value_here

getting validation error expected map instead of array... next few try:

--set controller.affinity[0].key=key_here \
--set controller.affinity[0].operator=In \
--set controller.affinity[0].value=value_here

validation error expected map instead of array

--set controller.affinity.key=key_here \
--set controller.affinity.operator=In \
--set controller.affinity.values=value_here

error validating data: [ValidationError(Deployment.spec.template.spec.affinity): unknown field "key" in io.k8s.api.core.v1.Affinity

Any idea how to properly use it?

For example for controller.tolerations this worked from first try:

--set controller.tolerations[0].key=taint \
--set controller.tolerations[0].value=node_taint_here \
--set controller.tolerations[0].effect=NoSchedule

Solution

  • There is no "key" field in affinity, you can easily check that with

    kubectl explain deployment.spec.template.spec.affinity
    KIND:     Deployment
    VERSION:  apps/v1
    
    RESOURCE: affinity <Object>
    
    DESCRIPTION:
         If specified, the pod's scheduling constraints
    
         Affinity is a group of affinity scheduling rules.
    
    FIELDS:
       nodeAffinity <Object>
         Describes node affinity scheduling rules for the pod.
    
       podAffinity  <Object>
         Describes pod affinity scheduling rules (e.g. co-locate this pod in the
         same node, zone, etc. as some other pod(s)).
    
       podAntiAffinity      <Object>
         Describes pod anti-affinity scheduling rules (e.g. avoid putting this pod
         in the same node, zone, etc. as some other pod(s)).
    

    So if you want to add nodeAffinity, similarly to official docs https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity , you can specify key like this:

    --set 'controller.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions[0].key=topology.kubernetes.io/zone'
    

    As you can see, this can be quite tedious, and generally, you should use or generate values file, like

    controller:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: topology.kubernetes.io/zone
                operator: In
                values:
                - antarctica-east1
                - antarctica-west1
    

    and add it with -f instead of adding 4 big arguments