<p>I'm currently creating a Kubernetes cluster for a production environment.
In my cluster, I have 2 node-pool, let's call them <code>api-pool</code> and <code>web-pool</code></p>

<p>In my <code>api-pool</code>, I have 2 nodes with 4CPU and 15Gb of RAM each.</p>

<p>I'm trying to deploy 8 replicas of my api in my <code>api-pool</code>, each replicas should have 1CPU and 3.5Gi of RAM.</p>

<p>My api.deployment.yaml looks something like this : </p>

<pre class="lang-yaml prettyprint-override"><code>---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-dev
spec:
  replicas: 8
  selector:
    matchLabels:
      app: my-api
  template:
    metadata:
      labels:
        app: my-api
    spec:
      containers:
      - name: api-docker
        image: //MY_IMAGE
        imagePullPolicy: Always
        envFrom:
          - configMapRef:
              name: api-dev-env
          - secretRef:
              name: api-dev-secret
        ports:
          - containerPort: 80
        resources:
          requests:
            cpu: "1"
            memory: "3.5Gi"
</code></pre>

<p>But my problem is that <strong>Kubernetes is deploying the pods on nodes on my <code>web-pool</code> as well as in my <code>api-pool</code></strong> but I want those pods to be deployed only in my <code>api-pool</code>.</p>

<p>I tried to label my nodes of the <code>api-pool</code> to use a selector that matches labels but it doesn't work and I'm not sure it's supposed to work that way.</p>

<p><strong>How can I precise to K8s to deploy those 8 replicas only in my <code>api-pool</code> ?</strong></p>

<p>You can use a <a href="https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#nodeselector" rel="nofollow noreferrer">nodeselector</a> which is the simplest recommended form of node selection constraint.</p>

<p>label the nodes of api-pool with pool=api</p>

<pre><code>kubectl label nodes nodename pool=api
</code></pre>

<p>Add <code>nodeSelector</code> in pod spec.</p>

<pre><code>---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-dev
spec:
  replicas: 8
  selector:
    matchLabels:
      app: my-api
  template:
    metadata:
      labels:
        app: my-api
    spec:
      containers:
      - name: api-docker
        image: //MY_IMAGE
        imagePullPolicy: Always
        envFrom:
          - configMapRef:
              name: api-dev-env
          - secretRef:
              name: api-dev-secret
        ports:
          - containerPort: 80
        resources:
          requests:
            cpu: "1"
            memory: "3.5Gi"
      nodeSelector:
        pool: api
</code></pre>

<p>For mode advanced use cases you can use <a href="https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity" rel="nofollow noreferrer">node affinity</a>.</p>
