Search code examples
kuberneteskubernetes-pod

Kubernetes: How to use Gt operator in pod nodeAffinity


I was going through Kubernetes documentation for node affinity and match expression under it. Among other operators it listed Gt and Lt (which I think is abbreviation for Greater than and less than respectively). I was thinking about how can I use them or what could be valid use cases for them but could not think of any.

Can you tell what are the possible use cases for Gt and Lt to be used in node affinity and how will I use them.


Solution

  • values in matchExpressions is an array of string. If you use Gt or Lt as the operator, the values array must have a single element, which will be interpreted as an integer.

    apiVersion: v1
    kind: Pod
    metadata:
      name: with-node-affinity
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/e2e-az-numeric-value
                operator: Gt
                values:
                - 100
      containers:
      - name: with-node-affinity
        image: k8s.gcr.io/pause:2.0
    

    The pod will run on a node if the node's label has a key kubernetes.io/e2e-az-numeric-value with value that is greater than 100.