How to add nodeSelector to Deployment yaml file?

8/12/2021

In the pods definition, we add the nodeSelector as a child of spec. But I'm not sure how to add this in deployment yaml file

are we supposed to add it in the template's spec-

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: first-deployment
  labels: 
    app: first
spec:
  replicas: 1
  template:
    metadata:
      name: first-deployment-pod
      label: 
        app: first
    spec:
      containers:
      - name: test
        image: test/test
    nodeSelector:
      testkey: testvalue

 

or do we need to add it in the spec of the deplyment -

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: first-deployment
  labels: 
    app: first
spec:
  replicas: 1
  nodeSelector:
    testkey: testvalue
  template:
    metadata:
      name: first-deployment-pod
      label: 
        app: first
    spec:
      containers:
      - name: test
        image: test/test
    
 
-- AYUSHI NIGAM
deployment
devops
kubernetes
nodeselector

1 Answer

8/12/2021

It should be at the same level than your containers array:

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: first-deployment
  labels: 
    app: first
spec:
  replicas: 1
  template:
    metadata:
      name: first-deployment-pod
      label: 
        app: first
    spec:
      containers:
      - name: test
        image: test/test
      nodeSelector:
        testkey: testvalue
-- SYN
Source: StackOverflow