karthik101
karthik101

Reputation: 1719

kubernetes node selector regex

I'm trying to deploy pods on the nodes which have labels like es-node: data-1, es-node:data-2, es-node:data-3. I can use all the labels in pod's nodeaffinity spec but i just want to use single label entry as es-node:data-* so that it gets deployed on all the nodes. Is this even possible?

Upvotes: 9

Views: 18304

Answers (3)

Manoj_Pachigolla
Manoj_Pachigolla

Reputation: 11

Recently, I faced the same, when I implemented a lifecycle policy on GKE NodePools with a policy to Create_Before_Destroy using Terraform.

This solved the problem that I want to solve. But, landed me with a new project related to NodeSelector.

Every time a node pool creates after the terraform lifecycle policy implementation, node-pool-**** name used to have 4 junk characters appended as its suffix all the time.

This will be a tough challenge if you hardcode the values of the node pool on selectors, as the node pool names keep on changing.

Here are a few of the ways that you can solve this.

  1. Yes, NodeSelector accepts selectors and would accept regular expressions as well. But it would expect in the RFC Format, use the below link to make your own expression.

RFC Expression Online Maker: https://www.regextester.com/103452

  1. The second and easiest option ( which I used ) is to label the node pools themselves with soft values during the infrastructure provision with node_pool: stack-overflow

You can use the below nodeSelector in your Kubernetes resources

nodeSelector: node_pool: stack-overflow

This is the easiet fix.

Instead of using the above key, if you use the cloud provided label, nodeSelector: cloud.google.com/gke-nodepool: stack-overflow-1234

Value would be based on the newly created nodepool name, which you can build using the above provided RFC Rex-Ex Maker.

Thanks

Upvotes: 0

Patrick McMahon
Patrick McMahon

Reputation: 414

Newer resources, such as Job, Deployment, ReplicaSet, and DaemonSet, support set-based requirements. I haven't tested this but you could probably use something like:

selector:
  matchExpressions:
    - {key: es-node, operator: In, values: [data-1, data-2, data-3]}

Source: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#resources-that-support-set-based-requirements

Openshift examples on using matchExpressions:

Upvotes: 1

Erez Rabih
Erez Rabih

Reputation: 15788

I don't think you can specify regular expressions on label selectors but you can just add an additional label, let's say es-node-type: data and put that as a label selector for your deployment or stateful set.

Upvotes: 2

Related Questions