Skip to content

Latest commit

 

History

History
121 lines (93 loc) · 3.4 KB

10-Practice-Test-Node-Affinity.md

File metadata and controls

121 lines (93 loc) · 3.4 KB

Practice Test - Node Affinity

Solutions to practice test - node affinity

  1. How many Labels exist on node node01?
    kubectl describe node node01
    

    Look under Labels section

    --- OR ---

    kubectl describe node node01 --show-labels
    
  2. What is the value set to the label key beta.kubernetes.io/arch on node01?

    From the output of Q1, find the answer there.

  3. Apply a label color=blue to node node01
    kubectl label node node01 color=blue
    
  4. Create a new deployment named blue with the nginx image and 3 replicas.
    kubectl create deployment blue --image=nginx --replicas=3
    
  5. Which nodes can the pods for the blue deployment be placed on?

    Check if master and node01 have any taints on them that will prevent the pods to be scheduled on them. If there are no taints, the pods can be scheduled on either node.

    kubectl describe nodes controlplane | grep -i taints
    kubectl describe nodes node01 | grep -i taints
    
  6. Set Node Affinity to the deployment to place the pods on node01 only. Now we edit in place the deployment we created earlier. Remember that we labelled `node01` with `color=blue`? Now we are going to create an affinity to that label, which will "attract" the pods of the deployment to it.
    1. $ kubectl edit deployment blue
      
    2. Add the YAML below under the template.spec section, i.e. at the same level as containers as it is a POD setting. The affinity will be considered only during scheduling stage, however this edit will cause the deployment to roll out again.
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: color
                operator: In
                values:
                - blue
  7. Which nodes are the pods placed on now?
    $ kubectl get pods -o wide
    
  8. Create a new deployment named red with the nginx image and 2 replicas, and ensure it gets placed on the controlplane node only.
    1. Create a YAML template for the deploymemt

      kubectl create deployment red --image nginx --replicas 2 --dry-run=client -o yaml > red.yaml
      
    2. Edit the file

      vi red.yaml
      
    3. Add the toleration using the label stated in the question, and placing it as before for the blue deployment

      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: node-role.kubernetes.io/master
                operator: Exists
    1. Save, exit and create the deployment
    kubectl create -f red.yaml
    
    1. Check the result
    $ kubectl get pods -o wide