Kubernetes: Node Affinity

In the last article we studied about node selectors which are a way to put pods on a specific nodes. Here we will discuss about another better way to do the same. This is node affinity.

Node affinity provides us a much better way to place the pods on the nodes. We can specify multiple conditions and use In , NotIn etc.

Now let us say we want pods to be scheduled only on the nodes having size Medium and Large . First of all we will label our nodes as below.

[root@node1 kubernetes]# kubectl label nodes node3.example.com size=Large
[root@node1 kubernetes]# kubectl label nodes node2.example.com size=Medium
node/node2.example.com labeled
[root@node1 kubernetes]#

Below is the pod definition file specifying the node selector

[root@node1 kubernetes]# cat node-affinity.yaml
apiVersion: v1
kind: Pod
  name: nginx-node-affinity
        - matchExpressions:
          - key: size
            operator: In
            - Large
            - Medium
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent

This pod definition file specifies that pod can be placed on any nodes whose size is either Large or Medium.

That is it for this article, in the next article we will discuss about resource requirements and limits. Thanks for reading it . Hope this is benefecial.

220 views0 comments

Subscribe Form

©2020 by Linux Advise