Kubernetes - Assigning Pod to Nodes

Learn how to use Kubernetes nodeSelector to assign pod to particular nodes

ta-ching chen

2 minute read

 Table of Contents

Intro 

Currently Kubernetes support nodeSelector, affinity and anti-affinity to constraint pod to run on paritcular nodes. In this post, you will learn how to assign pod to specifc nodes by nodeSelector.

Hands-On 

Show Labels 

$ kubectl get nodes --show-labels
NAME            STATUS    AGE       LABELS
192.168.1.1    Ready     4d        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.1.1
192.168.1.2    Ready     4d        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.1.2
192.168.1.3    Ready     4d        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.1.3

Attach Labels 

$ kubectl label node <node name> <label>=<value>
$ kubectl label node 192.168.1.1 networkSpeed=high
$ kubectl label node 192.168.1.2 networkSpeed=high
$ kubectl label node 192.168.1.2 networkSpeed=low

Check labels are attached to nodes

$ kubectl get nodes --show-labels
NAME            STATUS    AGE       LABELS
192.168.1.1    Ready     4d        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.1.1,networkSpeed=high
192.168.1.2    Ready     4d        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.1.2,networkSpeed=high
192.168.1.3    Ready     4d        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.1.3,networkSpeed=low

Remove Labels 

You can remove incorrect label with <label>-

$ kubectl label node 192.168.1.1 networkSpeed-

Create Testing Deployment 

Dont forget to quote the label value.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-test
spec:
  replicas: 3
  template:
    metadata:
      name: nginx
      namespace: default
      labels:
        env: beta
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
      nodeSelector:
        networkSpeed: "high"

Check Which Node Pod Runs On 

Now, we can see that pods only run on the nodes with the label networkSpeed=high attached.

$ kubectl get pod -o wide
NAME                              READY     STATUS    RESTARTS   AGE       IP             NODE
nginx-test-3190407156-4g8k5       1/1       Running   0          17s       172.16.88.2    192.168.1.2
nginx-test-3190407156-837ad       1/1       Running   0          17s       172.16.88.3    192.168.1.2
nginx-test-3190407156-o95l8       1/1       Running   0          17s       172.16.84.2    192.168.1.1

Troubleshooting 

Failed to Create Deployment 

unable to decode "nginx.yaml": [pos 1742]: json: expect char '"' but got char 't'

See Also

To reproduce, republish or re-use the content, please attach with link: https://tachingchen.com/
comments powered by Disqus