Kubernetes : Deploy only in one node-pool

3/18/2020

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

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

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

My api.deployment.yaml looks something like this :

---
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"

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

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

How can I precise to K8s to deploy those 8 replicas only in my api-pool ?

-- Bibas
deployment
google-kubernetes-engine
kubernetes

1 Answer

3/18/2020

You can use a nodeselector which is the simplest recommended form of node selection constraint.

label the nodes of api-pool with pool=api

kubectl label nodes nodename pool=api

Add nodeSelector in pod spec.

---
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

For mode advanced use cases you can use node affinity.

-- Arghya Sadhu
Source: StackOverflow