📖 Scenario: You are managing a Kubernetes cluster with different types of nodes. You want to schedule a pod to run only on nodes that have a specific label, such as disktype: ssd. This helps ensure your pod runs on nodes with fast storage.
🎯 Goal: Create a pod manifest that uses a node selector to schedule the pod only on nodes labeled with disktype: ssd.
📋 What You'll Learn
Create a pod manifest with metadata name fast-storage-pod
Add a container named app-container running the image nginx
Add a nodeSelector field to schedule the pod on nodes with label disktype: ssd
Print the final pod manifest YAML
💡 Why This Matters
🌍 Real World
Node selectors are used in Kubernetes to ensure pods run on nodes with specific hardware or software characteristics, like SSD storage or GPU support.
💼 Career
Understanding node selectors is important for Kubernetes administrators and DevOps engineers to optimize workload placement and resource usage.
Progress0 / 4 steps
1
Create the basic pod manifest
Create a YAML manifest for a pod named fast-storage-pod with a container named app-container running the image nginx. Do not add node selectors yet.
Kubernetes
Hint
Start with the basic pod YAML structure. Use apiVersion: v1 and kind: Pod. Add metadata with the pod name and a container spec.
2
Add the node selector
Add a nodeSelector field under spec with the key disktype and value ssd to schedule the pod on nodes labeled disktype: ssd.
Kubernetes
Hint
Under spec, add nodeSelector: and then the label key and value as a map.
3
Verify the pod manifest structure
Check that the pod manifest includes nodeSelector with disktype: ssd and the container named app-container with image nginx.
Kubernetes
Hint
Make sure the nodeSelector and container details are exactly as specified.
4
Print the final pod manifest
Print the complete pod manifest YAML to display the pod configuration with the node selector.
Kubernetes
Hint
Store the YAML as a string variable and print it exactly.
Practice
(1/5)
1. What is the main purpose of using nodeSelector in a Kubernetes Pod specification?
easy
A. To limit the number of pods running in the cluster
B. To schedule pods only on nodes with specific labels
C. To automatically update pods when nodes change
D. To create a backup of pod data on nodes
Solution
Step 1: Understand nodeSelector role
The nodeSelector field in a Pod spec is used to specify labels that a node must have for the pod to be scheduled there.
Step 2: Compare options with nodeSelector purpose
Only To schedule pods only on nodes with specific labels correctly describes this scheduling control. Other options describe unrelated features.
Final Answer:
To schedule pods only on nodes with specific labels -> Option B
Quick Check:
nodeSelector = schedule pods on labeled nodes [OK]
Hint: nodeSelector matches pod to node labels for placement [OK]
Common Mistakes:
Thinking nodeSelector limits pod count
Confusing nodeSelector with pod updates
Assuming nodeSelector backs up pod data
2. Which of the following is the correct syntax to specify a nodeSelector in a Pod YAML manifest?
easy
A. nodeSelector: disktype=ssd
B. nodeSelector:
- disktype: ssd
C. nodeSelector:
disktype: ssd
D. nodeSelector: [disktype=ssd]
Solution
Step 1: Recall nodeSelector YAML format
nodeSelector is a map of key-value pairs under the nodeSelector field, indented properly.
Step 2: Check each option's syntax
nodeSelector:
disktype: ssd uses correct YAML mapping with key and value properly indented. Options B, C, and D use invalid YAML or wrong formats.
Final Answer:
nodeSelector:
disktype: ssd -> Option C
Quick Check:
nodeSelector uses key: value map in YAML [OK]
Hint: Use key: value under nodeSelector with proper indentation [OK]
Common Mistakes:
Using list syntax instead of map
Writing nodeSelector as a string
Incorrect indentation causing YAML errors
3. Given the following node labels:
node1: disktype=ssd
node2: disktype=hdd
And this Pod spec snippet:
spec:
nodeSelector:
disktype: ssd
On which node will the Pod be scheduled?
medium
A. node1
B. node2
C. Either node1 or node2
D. No node, pod will remain pending
Solution
Step 1: Match pod nodeSelector with node labels
The pod requires a node with label disktype=ssd. node1 has this label, node2 does not.
Step 2: Determine pod scheduling
Since only node1 matches the label, the pod will be scheduled on node1.
Final Answer:
node1 -> Option A
Quick Check:
Pod nodeSelector matches node1 label [OK]
Hint: Pod schedules only on nodes matching all nodeSelector labels [OK]
Common Mistakes:
Assuming pod can schedule on any node
Ignoring label key-value exact match
Thinking pod schedules on multiple nodes simultaneously
4. You wrote this Pod spec:
spec:
nodeSelector:
disk-type: ssd
But your nodes have label disktype=ssd (no dash). What will happen when you try to schedule the Pod?
medium
A. Pod will remain pending, no matching nodes found
B. Pod will schedule on nodes with label disktype=ssd
C. Pod will schedule on nodes with label disk-type=ssd
D. Pod will schedule on any available node
Solution
Step 1: Compare nodeSelector key with node labels
The pod requests nodes with label disk-type=ssd, but nodes have disktype=ssd. The keys differ by a dash.
Step 2: Understand label matching behavior
Labels must match exactly. Since no node has disk-type=ssd, pod cannot be scheduled.
Final Answer:
Pod will remain pending, no matching nodes found -> Option A
Quick Check:
Label keys must match exactly for nodeSelector [OK]
Hint: Label keys must match exactly, including dashes [OK]
Common Mistakes:
Assuming partial key match works
Thinking pod schedules on any node if no match
Confusing label key spelling
5. You want to ensure a Pod runs only on nodes labeled region=us-west and disktype=ssd. Which is the correct nodeSelector configuration?
hard
A. nodeSelector:
region: us-west, disktype: ssd
B. nodeSelector:
region=us-west
disktype=ssd
C. nodeSelector:
- region=us-west
- disktype=ssd
D. nodeSelector:
region: us-west
disktype: ssd
Solution
Step 1: Understand nodeSelector format for multiple labels
nodeSelector is a map of key-value pairs, each on its own line with proper indentation.
Step 2: Evaluate each option's syntax
nodeSelector:
region: us-west
disktype: ssd correctly lists two key-value pairs. nodeSelector:
region: us-west, disktype: ssd uses invalid comma syntax. nodeSelector:
- region=us-west
- disktype=ssd uses list syntax which is invalid. nodeSelector:
region=us-west
disktype=ssd uses equals signs incorrectly.
Final Answer:
nodeSelector:
region: us-west
disktype: ssd -> Option D
Quick Check:
Multiple labels in nodeSelector use separate key: value lines [OK]
Hint: List each label as key: value pair under nodeSelector [OK]
Common Mistakes:
Using commas to separate labels
Using list syntax instead of map
Using equals signs instead of colon in YAML
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.