Search code examples
kubernetes-helm

using node selector helm chart to assign pods to a specific node pool


i'm trying to assign pods to a specific node as part of helm command, so by the end the deployment yaml should look like this

spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  nodeSelector:
    node-name: dev-cpu-pool

i'm using this command as part of Jenkins file deployment

`sh "helm upgrade -f charts/${job_name}/default.yaml --set nodeSelector.name=${deployNamespace}-cpu-pool --install ${deployNamespace}-${name} helm/${name} --namespace=${deployNamespace} --recreate-pods --version=${version}`"

                

the deployment works good and the pod is up and running but from some reason i cannot see the nodeSelector key and value as part of the deployment yaml and as a results pods not assign to the specific node i want. any idea what is wrong ? should i put any place holder as part of my chart template or is not must ?


Solution

  • The artifacts that Helm submits to the Kubernetes API are exactly the result of rendering the chart templates; nothing more, nothing less. If your templates don't include a nodeSelector: block then the resulting Deployment never will either. Even if you helm install --set ... things that could match Kubernetes API fields, nothing will implicitly fill them in.

    If you want an option to specify rarely-used fields like nodeSelector: then your chart code needs to include them. You can make the presence of the field conditional on the value being set, but you do need to explicitly list it out:

    apiVersion: apps/v1
    kind: Deployment
    spec:
      template:
        spec:
    {{- if .Values.nodeSelector }}
          nodeSelector: {{- .Values.nodeSelector | toYaml | nindent 8 }}
    {{- end }}