Regex in node selector

2k views Asked by At

I have 4 k8's nodes, let's say apple, ball, cat, dell nodes. If I want to assign a pod(the kind used is deployment/statefullset) to a selective node then I could use node_selector for it. I'm going to implement vertical autoscaling of nodes. If autoscaling of node occurs then, the pod can allocate to any nodes. I need this to be done using node selector only. So what could be done. Is there any possibility of regex in node selector or any idea ? I tried a below but unachievable

nodeSelector:
   kubernetes.io/hostname: "apple([-A-Za-z0-9_.]*)"
1

There are 1 answers

0
Akin Ozer On

This is an area where node labels comes to rescue. You should label your nodes in the creation phase. If you are on EKS(because you selected ec2 tag), it should add automatic labels when you use node groups with auto scaling groups. If you are using any other k8s distribution you can do it on ansible/user data scripts.

Basically you can create multiple autoscaling groups for each "apple, ball, cat, dell" nodes and assign them node labels like: "node-group: apple". Then you can use this on pod spec:(you should use only preferred or required per node-group this is just an example to show you both)

spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: node-group
            operator: In
            values:
            - cat
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: node-group
            operator: In
            values:
            - apple

You can learn more about this on the kubernetes docs: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/