# Node selector for cronJob

**URL:** <https://discuss.kubernetes.io/t/node-selector-for-cronjob/10014>\
**Category:** General Discussions\
**Created:** [March 10, 2020, 1:58am UTC](https://discuss.kubernetes.io/t/node-selector-for-cronjob/10014 "2020-03-10T01:58:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![raphaelc](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@raphaelc](https://discuss.kubernetes.io/u/raphaelc)\
**Post date:** [March 10, 2020, 1:58am UTC](https://discuss.kubernetes.io/t/node-selector-for-cronjob/10014/1 "2020-03-10T01:58:33Z")

</div>

### Cluster information:

Kubernetes version: 1.17.0  
Cloud being used: bare-metal  
Installation method: apt-get?  
Host OS: Debian 10  
CNI and version: flannel:v0.11.0  
CRI and version:

I want a cron job to run on a specific node. I tried to place the nodeSelector in my kubernetes manifest file but I get an error saying that it doesn’t know what the nodeSelector field is.

error: error validating “highres-backup.yaml”: error validating data: ValidationError(CronJob): unknown field “nodeSelector” in io.k8s.api.batch.v1beta1.CronJob; if you choose to ignore these errors, turn validation off with --validate=false

I have tried putting nodeSelector on various indentation levels but got the same errors.  
I am posting my mildly sensored yaml file. Essentially, I’m trying to schedule files written on a specific node to be transferred to an nfs-volume at regular intervals.

```
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: highres-rsync-cronjob

spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        nodeSelector:
          nfs_backup: yes
        spec:
          containers:
          - name: rsync-cronjob
            image: eeacms/rsync
            args:
            - /bin/sh
            - -c
            - "find /data/high-res/ -cmin +20 -printf %P\\0 | rsync -zarvhO --remove-source-files /data/high-res/ /mnt/high-res/ --exclude '*_1.egg'"
            volumeMounts:
              - name: local-storage
                mountPath: /data/
              - name: nfs-volume
                mountPath: /mnt/
          restartPolicy: OnFailure
          volumes:
          - name: local-storage
            hostPath:
              path: /data
              type: directory
          - name: nfs-volume
            nfs:
              server: censored.ip.address
              path: /data
```

---

<div class="post-metadata">

**Author:** ![stephendotcarter](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/stephendotcarter/32/4528_2.png) [@stephendotcarter](https://discuss.kubernetes.io/u/stephendotcarter)\
**Post date:** [March 10, 2020, 6:03am UTC](https://discuss.kubernetes.io/t/node-selector-for-cronjob/10014/2 "2020-03-10T06:03:47Z")

</div>

Checkout the docs here: [Assigning Pods to Nodes - Kubernetes](https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#nodeselector)

With the example you provided just move it one level down inside the `spec` at same indent as `containers`.

Kind regards,  
Stephen

---

<div class="post-metadata">

**Author:** ![raphaelc](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@raphaelc](https://discuss.kubernetes.io/u/raphaelc)\
**Post date:** [March 10, 2020, 3:48pm UTC](https://discuss.kubernetes.io/t/node-selector-for-cronjob/10014/3 "2020-03-10T15:48:31Z")

</div>

Thank you @stephendotcarter

That was actually my initial guess, but I still get the same api error

```
raphaelc@tpnotc:~/site_atlas$ kubectl apply -f highres-backup.yaml 
Error from server (BadRequest): error when creating "highres-backup.yaml": CronJob in version "v1beta1" cannot be handled as a CronJob: v1beta1.CronJob.Spec: v1beta1.CronJobSpec.JobTemplate: v1beta1.JobTemplateSpec.Spec: v1.JobSpec.Template: v1.PodTemplateSpec.Spec: v1.PodSpec.NodeSelector: ReadString: expects " or n, but found t, error found in #10 byte of ...|_backup":true},"rest|..., bigger context ...|e":"nfs-volume"}]}],"nodeSelector":{"nfs_backup":true},"restartPolicy":"OnFailure","volumes":[{"host|...

```

Do you know what could be wrong? I’m posting my manifest file now.

raphaelc@tpnotc:~/site\_atlas$ cat highres-backup.yaml  
apiVersion: batch/v1beta1  
kind: CronJob  
metadata:  
name: highres-rsync-cronjob

```
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: rsync-cronjob
            image: eeacms/rsync
            args:
            - /bin/sh
            - -c
            - "find /data/high-res/ -cmin +20 -printf %P\\0 | rsync -zarvhO --remove-source-files /data/high-res/ /mnt/high-res/ --exclude '*_1.egg'"
            volumeMounts:
              - name: local-storage
                mountPath: /data/
              - name: nfs-volume
                mountPath: /mnt/
          nodeSelector:
            nfs_backup: yes
          restartPolicy: OnFailure
          volumes: 
          - name: local-storage
            hostPath:
              path: /data
              type: directory
          - name: nfs-volume
            nfs:
              server: censored.ip.address
              path: /data
```

---

<div class="post-metadata">

**Author:** ![stephendotcarter](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/stephendotcarter/32/4528_2.png) [@stephendotcarter](https://discuss.kubernetes.io/u/stephendotcarter)\
**Post date:** [March 10, 2020, 4:15pm UTC](https://discuss.kubernetes.io/t/node-selector-for-cronjob/10014/4 "2020-03-10T16:15:46Z")

</div>

I think this is your issue:

> **[YAML: Keys like "yes" or "no" evaluate to true and false](https://makandracards.com/makandra/24809-yaml-keys-like-yes-or-no-evaluate-to-true-and-false)**
>
> If you parse this Yaml …
> 
> yes: 'Totally'
> no: 'Nope'
> 
> 
> … you get this Ruby hash:
> 
> { true: 'Totally',
> false: 'Nope' }
> 
> 
> In order to use the strings 'yes' and 'no' as keys, you need to wrap them with quotes:
> 
> 'yes': 'Totally'
> 'no': 'Nope'
> 
> 
> There's...

Try changing your selector to:

```auto
nfs_backup: "yes"

```

That will force it to be a `string` instead of a `bool`.

Kind regards,  
Stephen

---

<div class="post-metadata">

**Author:** ![raphaelc](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@raphaelc](https://discuss.kubernetes.io/u/raphaelc)\
**Post date:** [March 11, 2020, 3:25am UTC](https://discuss.kubernetes.io/t/node-selector-for-cronjob/10014/5 "2020-03-11T03:25:20Z")

</div>

Using ‘yes’ as a key was the problem. Thank you!

---

<div class="post-metadata">

**Author:** ![Romiko](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/romiko/32/5823_2.png) [@Romiko](https://discuss.kubernetes.io/u/Romiko)\
**Post date:** [September 7, 2020, 4:09am UTC](https://discuss.kubernetes.io/t/node-selector-for-cronjob/10014/6 "2020-09-07T04:09:10Z")

</div>

Just ensure you specify nodeSelector at the container level.

> <https://gist.github.com/Romiko/2cfb1f2950430ab79842b74bc8a1b894>
