CKA · Question #40
Check to see how many nodes are ready schedulable (not including nodes tainted NoSchedule ) and write the number to /opt/KUSC00402/kusc00402.txt.
The task requires using kubectl with jq to identify and count nodes that are both in a 'Ready' state and not marked with a 'NoSchedule' taint, then writing the count to a specified file.
Question
Exhibits
Explanation
The task requires using kubectl with jq to identify and count nodes that are both in a 'Ready' state and not marked with a 'NoSchedule' taint, then writing the count to a specified file.
Approach. The correct approach involves executing a series of kubectl commands in the provided web terminal to query Kubernetes node information, filter it based on the specified criteria, and then output the final count to the designated file.
- Identify Ready nodes: Nodes must have a
Readycondition withstatus: True. - Exclude NoSchedule-tainted nodes: Nodes must either have no taints, or if they have taints, none of them should have
effect: NoSchedule.
This can be achieved efficiently using kubectl with jq for advanced JSON parsing and filtering:
```bash
COUNT=$(kubectl get nodes -o json | jq -r \
'.items[] | \
select(.status.conditions[] | select(.type=="Ready" and .status=="True")) | \
select((.spec.taints // [] | map(select(.effect == "NoSchedule")) | length) == 0) | \
.metadata.name' | wc -l)
echo $COUNT > /opt/KUSC00402/kusc00402.txt
```
**Explanation of the `jq` command:**
* `kubectl get nodes -o json`: Retrieves all node information in JSON format.
* `jq -r '.items[] | ...'`: Iterates through each node item in the JSON output.
* `select(.status.conditions[] | select(.type=="Ready" and .status=="True"))`: Filters for nodes that have a 'Ready' condition set to 'True'. This ensures only 'Ready' nodes are considered.
* `select((.spec.taints // [] | map(select(.effect == "NoSchedule")) | length) == 0)`: This is the crucial part for handling taints.
* `.spec.taints // []`: Safely accesses the `taints` array, defaulting to an empty array `[]` if `taints` is `null` (i.e., no taints exist). This prevents errors if a node has no taints.
* `map(select(.effect == "NoSchedule"))`: Filters the (possibly empty) `taints` array to find any taints with `effect: NoSchedule`.
* `| length) == 0`: Checks if the resulting array of 'NoSchedule' taints has a length of 0. If it does, it means the node does *not* have a 'NoSchedule' taint, thus fulfilling the 'schedulable' criteria.
* `|.metadata.name'`: Extracts the name of the nodes that pass both filters.
* `| wc -l`: Counts the number of lines (i.e., node names) returned, giving the final count.
* `echo $COUNT > /opt/KUSC00402/kusc00402.txt`: Writes the calculated count to the specified file.
Common mistakes.
- common_mistake. Common mistakes include:
- Not filtering for 'Ready' nodes: Simply counting nodes without 'NoSchedule' taint would include nodes that are not yet ready or have failed, which are not truly schedulable.
- Incorrectly handling 'NoSchedule' taints: Counting nodes that are 'Ready' but ignoring the
NoScheduletaint, or conversely, excluding nodes with any taint (instead of specificallyNoSchedule). The question explicitly targetsNoSchedule. - Manual counting: Attempting to manually count nodes after running
kubectl get nodesandkubectl describe nodefor each, especially in a larger cluster, is time-consuming, inefficient, and prone to human error under exam pressure. - Forgetting file output: Not writing the final numerical count to the specified file (
/opt/KUSC00402/kusc00402.txt) as required by the question.
Concept tested. Kubernetes Node management, understanding Node status conditions (specifically Ready), Node scheduling concepts (including Taints and their effects like NoSchedule), and proficiency with kubectl commands, JSON output parsing (using jq or jsonpath), and basic shell scripting/file redirection.
Topics
Community Discussion
No community discussion yet for this question.

