1. nodeName: Choose the node that you want the pod to be deployed to by using nodeName
2. nodeSelector: Labelling node as you have many nodes (N1, N2...N100) then by using nodeSelector
3. nodeAffinity: Node affinity is conceptually similar to nodeSelector -- it allows you to constrain which nodes your pod is eligible to be scheduled on, based on labels on the node.
## There are currently two types of node affinity, called:
1) requiredDuringSchedulingIgnoredDuringExecution
2) preferredDuringSchedulingIgnoredDuringExecution
You can think of them as "hard" and "soft" respectively, in the sense that the former specifies rules that must be met for a pod to be scheduled onto a node (just like nodeSelector but using a more expressive syntax), while the latter specifies preferences that the scheduler will try to enforce but will not guarantee. The "IgnoredDuringExecution" part of the names means that, similar to how nodeSelector works, if labels on a node change at runtime such that the affinity rules on a pod are no longer met, the pod will still continue to run on the node.
Example: pod can only be placed on a node with a label whose key is env=test or env=stag
4. Taints and Tolerations
While nodeAffinity is a property of Pods that attracts them to a set of nodes (either as a preference or a hard requirement), taints are the opposite -- they allow a node to repel a set of pods.Example: pod can only be placed on a node with a label whose key is env=test or env=stag
4. Taints and Tolerations
Tolerations are applied to pods, and allow (but do not require) the pods to schedule onto nodes with matching taints.
Taints and tolerations work together to ensure that pods are not scheduled onto inappropriate nodes. One or more taints are applied to a node; this marks that the node should not accept any pods that do not tolerate the taints:
kubectl describe node kmaster
kubectl describe node kworker1
- if there is at least one un-ignored taint with effect NoSchedule then Kubernetes will not schedule the pod onto that node.
- if there is no un-ignored taint with effect NoSchedule but there is at least one un-ignored taint with effect PreferNoSchedule then Kubernetes will try to not schedule the pod onto the node.
- if there is at least one un-ignored taint with effect NoExecute then the pod will be evicted from the node (if it is already running on the node), and will not be scheduled onto the node (if it is not yet running on the node).
## To change the nodes properties (examples):
Step1: Check the labels on all the nodes
kubectl get nodes --show-labels
Step2: Check the label on a specific node ( say kwn2)
kubectl get nodes --show-labels
Step3: Create a label env=prod for a worker node ( say kwn2)
kubectl label nodes kworker1 env=test
kubectl label nodes kworker2 env=prod
Step4: Delete a label from a node
Step1: Check the labels on all the nodes
kubectl get nodes --show-labels
Step2: Check the label on a specific node ( say kwn2)
kubectl get nodes --show-labels
Step3: Create a label env=prod for a worker node ( say kwn2)
kubectl label nodes kworker1 env=test
kubectl label nodes kworker2 env=prod
Step4: Delete a label from a node
kubectl edit node kworker2 (then dd the line with the label..lol)
1. nodeName Example:
1. nodeName Example:
Add a line (nodeName) in the yaml file
vi nodeName.yaml
apiVersion: v1
kind: Pod
metadata:
name: podnodeName
spec:
containers:
- name: nginx-container
image: nginx
nodeName: kworker1
2. nodeSelector Example:
vi nodeName.yaml
apiVersion: v1
kind: Pod
metadata:
name: podnodeName
spec:
containers:
- name: nginx-container
image: nginx
nodeName: kworker1
2. nodeSelector Example:
Step1: Check the labels on all the nodes
kubectl get nodes --show-labels
Step2: Check the label on a specific node (kworker2)
kubectl get nodes --show-labels kworker2
Step3: Create a label env=prod for a kworker2 node
kubectl label nodes kworker2 env=prod
Step4: Create a pod with nodeSelector specification. Create file with name nodeselector.yaml
vi nodeselector.yaml
apiVersion: v1
kind: Pod
metadata:
name: podnodeSelector
spec:
containers:
- name: container1
image: nginx
nodeSelector:
env: "prod"
Step5: Create the pod by running below command
kubectl create -f nodeselector.yaml
Step6: Verify the pod “podselector” is created on kwn2 by running below command
kubectl get pods -o wide
3.1 nodeAffinity - requiredDuringScheduling
Step1: Check the labels on all the nodes
kubectl get nodes --show-labels
Step2: Check the label on a specific node (kworker1)
kubectl get nodes --show-labels kworker1
Step3: Create a label env=test for a worker node (kworker1)
kubectl label nodes kworker1 env=test --overwrite
Step4: Create a pod which can be placed on nodes which has labels env=test or stag. Create a file called nodeaffinity.yaml
vi nodeaffinity.yaml
apiVersion: v1
kind: Pod
metadata:
name: podaffinity
spec:
containers:
- name: nginx-container
image: nginx
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: env
kubectl get nodes --show-labels
Step2: Check the label on a specific node (kworker2)
kubectl get nodes --show-labels kworker2
Step3: Create a label env=prod for a kworker2 node
kubectl label nodes kworker2 env=prod
Step4: Create a pod with nodeSelector specification. Create file with name nodeselector.yaml
vi nodeselector.yaml
apiVersion: v1
kind: Pod
metadata:
name: podnodeSelector
spec:
containers:
- name: container1
image: nginx
nodeSelector:
env: "prod"
Step5: Create the pod by running below command
kubectl create -f nodeselector.yaml
Step6: Verify the pod “podselector” is created on kwn2 by running below command
kubectl get pods -o wide
3.1 nodeAffinity - requiredDuringScheduling
Step1: Check the labels on all the nodes
kubectl get nodes --show-labels
Step2: Check the label on a specific node (kworker1)
kubectl get nodes --show-labels kworker1
Step3: Create a label env=test for a worker node (kworker1)
kubectl label nodes kworker1 env=test --overwrite
Step4: Create a pod which can be placed on nodes which has labels env=test or stag. Create a file called nodeaffinity.yaml
vi nodeaffinity.yaml
apiVersion: v1
kind: Pod
metadata:
name: podaffinity
spec:
containers:
- name: nginx-container
image: nginx
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: env
operator: In #operator has 3: in, not in, exist
values:
- test
- stag
Step5: Create the pod by running below command
kubectl create -f nodeaffinity.yaml
Step6: Verify the pod “podaffinity” is created on kwn2 by running below command
kubectl get pods -o wide
3.2 nodeAffinity - preferredDuringScheduling
vi nodeaffinity2.yaml
apiVersion: v1
kind: Pod
metadata:
name: podaffinity2
spec:
containers:
- name: c1
image: nginx
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- preference:
matchExpressions:
- key: size
operator: In
values:
- extralarge
weight: 1
values:
- test
- stag
Step5: Create the pod by running below command
kubectl create -f nodeaffinity.yaml
Step6: Verify the pod “podaffinity” is created on kwn2 by running below command
kubectl get pods -o wide
3.2 nodeAffinity - preferredDuringScheduling
vi nodeaffinity2.yaml
apiVersion: v1
kind: Pod
metadata:
name: podaffinity2
spec:
containers:
- name: c1
image: nginx
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- preference:
matchExpressions:
- key: size
operator: In
values:
- extralarge
weight: 1
4. Taints and Tolerations
Example: Create a pod which has the toleration on worker node which is tainted for app=web only.
Step1: Taint a node with key app and value web with NoSchedule effect
kubectl taint nodes kworker1 app=web:NoSchedule
Step2: Create a pod with toleration of web app. Create a file taint.yaml
vi taint.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
containers:
- name: nginx-container
image: nginx
tolerations:
- key: app
operator: "Equal"
value: "web"
effect: "NoSchedule"
Step3: create the pod by running below command
kubectl create -f taint.yaml
Step4: Verify the pod that where the pod get created (not necessarily it will create on tainted node)
kubectl get pods -o wide
4.2 Removing a taint from a node
You can use kubectl taint to remove taints. You can remove taints by key, key-value, or key-effect.
kubectl taint node kworker1 app-
The example above removes from node kworker1 all the taints with key app.