Kubernetes - Assigning Pod to Nodes
Learn how to use Kubernetes nodeSelector to assign pod to particular nodes
Table of Contents
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.
$ 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
$ 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
You can remove incorrect label with <label>-
$ kubectl label node 192.168.1.1 networkSpeed-
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"
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
- Using
""
to quote the label value in yaml (https://github.com/kubernetes/kubernetes/issues/23251)
unable to decode "nginx.yaml": [pos 1742]: json: expect char '"' but got char 't'
See Also
- Kubernetes - Two Steps Installation
- Kubernetes - Installation
- Rolling Updates with Kubernetes Deployments
- Kubernetes - Pod
- Kubernetes - High Availability
To reproduce, republish or re-use the content,
please attach with link: https://tachingchen.com/
Twitter
Google+
Facebook
Reddit
LinkedIn
StumbleUpon
Pinterest
Email