Node Affinity in Kubernetes Cluster

Ticker

6/recent/ticker-posts

Node Affinity in Kubernetes Cluster

Question : The Nautilus DevOps team is doing some testing around the Kubernetes cluster for pods, deployments etc. A new ticket has been raised by one of the developer with some particular requirements. Find below more details and perform the task accordingly.

Create a label for node node01 color=grey,

Based on the new label created for node01 using nodeAffinity matchExpressions create a new deployment named grey, use image nginx with latest tag only and remember to mention tag i.e nginx:latest and 3 replicas; ensure container name is nginx-container and that it gets placed on the node01 node only. Also make sure all pods are in running state before clicking on FINISH.

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.


Please Note :-  Perform the below commands based on your question  server,  user name & other details might differ . So please read task carefully before executing. All the Best 👍

Solution:  

1. At first  kubectl  utility configure and working from jump server, run below commands   

thor@jump_host /$ kubectl get deploy

No resources found in default namespace.

thor@jump_host /$

thor@jump_host /$ kubectl get nodes --show-labels

NAME           STATUS   ROLES    AGE     VERSION   LABELS

controlplane   Ready    master   3m47s   v1.19.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=controlplane,kubernetes.io/os=linux,node-role.kubernetes.io/master=

node01         Ready    <none>   3m10s   v1.19.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux

thor@jump_host /$


2.  Set color grey as per the task to your node.

thor@jump_host /$ kubectl  label  nodes node01 color=grey

node/node01 labeled

thor@jump_host /$


3. Create affinity.yaml  file with all the parameters from git lab repo.

    ( Refer Below Video for more clarity )

thor@jump_host /$ vi /tmp/affinity.yml

thor@jump_host /$ cat /tmp/affinity.yml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: grey

spec:

  replicas: 3

  selector:

    matchLabels:

      app: nginx-pod

  template:

    metadata:

      labels:

        app: nginx-pod

    spec:

      affinity:

        nodeAffinity:

          requiredDuringSchedulingIgnoredDuringExecution:

            nodeSelectorTerms:

              - matchExpressions:

                  - key: color

                    operator: In

                    values:

                      - grey

      containers:

        - name: nginx-container

          image: nginx:latest

thor@jump_host /$


4.  Run below command to create pod

thor@jump_host /$  kubectl create -f /tmp/affinity.yml

deployment.apps/grey created

thor@jump_host /$


5.  Wait for  pods to get running status & check the nodes status     
    

thor@jump_host /$ kubectl get deploy

NAME   READY   UP-TO-DATE   AVAILABLE   AGE

grey   3/3     3            3           10s

thor@jump_host /$

thor@jump_host /$ kubectl get pods -o wide

NAME                    READY   STATUS    RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES

grey-5c4bc995f6-bnf4m   1/1     Running   0          62s   10.244.1.4   node01   <none>           <none>

grey-5c4bc995f6-gh22l   1/1     Running   0          62s   10.244.1.2   node01   <none>           <none>

grey-5c4bc995f6-x29xw   1/1     Running   0          62s   10.244.1.3   node01   <none>           <none>

thor@jump_host /$


6.  Click on Finish & Confirm to complete the task successful

Happy Learning!!!!

Apart from this if you need more clarity,  I have made a  tutorial video on this , please go through and share your comments. Like and share the knowledge 





Post a Comment

0 Comments

Latest Posts

KodeKloud Kubernetes Security CKS  Lab Challenge 4 |  Audit-policy | Install & configure falco utility | Inspect the API server audit logs and identify the user