Bird
Raised Fist0
Kubernetesdevops~20 mins

Cost optimization in Kubernetes - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Kubernetes Cost Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Kubernetes Resource Requests and Limits

Which statement best explains how setting resource requests and limits in Kubernetes helps optimize costs?

ARequests and limits automatically scale the number of nodes in the cluster to match pod demand, reducing idle nodes.
BRequests reserve resources for pods, ensuring they get guaranteed CPU and memory, preventing overcommitment and reducing wasted resources.
CLimits allow pods to use unlimited resources, which helps avoid throttling and improves performance at no extra cost.
DSetting requests and limits disables autoscaling, which saves costs by fixing resource usage.
Attempts:
2 left
💡 Hint

Think about how reserving resources affects scheduling and cluster utilization.

💻 Command Output
intermediate
2:00remaining
Identifying Idle Nodes with kubectl

You run the command kubectl top nodes and see the following output:

NAME           CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
node-1         50m          5%     200Mi          10%
node-2         10m          1%     100Mi          5%
node-3         300m         30%    1Gi            50%

Which node is the best candidate for scaling down to save costs?

Anode-2 because it has the lowest CPU and memory usage.
Bnode-3 because it uses the most CPU and memory.
Cnode-1 because it has moderate CPU and memory usage.
DNone, all nodes are equally used.
Attempts:
2 left
💡 Hint

Look for the node with the least resource usage to identify idle nodes.

🔀 Workflow
advanced
2:00remaining
Implementing Cluster Autoscaler for Cost Savings

You want to enable automatic scaling of your Kubernetes cluster nodes to save costs during low demand periods. Which configuration step is essential to allow the Cluster Autoscaler to remove nodes safely?

AManually delete pods before the autoscaler removes nodes.
BDisable resource requests and limits on all pods to allow flexible scheduling.
CSet pod disruption budgets to ensure critical pods are not evicted during node scale-down.
DSet all nodes to unschedulable to prevent new pods from being scheduled.
Attempts:
2 left
💡 Hint

Think about how to protect important pods during node removal.

Troubleshoot
advanced
2:00remaining
Troubleshooting High Costs Due to Overprovisioning

Your Kubernetes cluster costs are unexpectedly high. You suspect overprovisioning. Which of the following is the most likely cause?

AHorizontal Pod Autoscaler is scaling pods based on CPU usage.
BPods have no resource requests or limits, so the scheduler packs them tightly on nodes.
CCluster Autoscaler is enabled and removing unused nodes automatically.
DPods have resource requests set too high, causing the scheduler to reserve more node capacity than needed.
Attempts:
2 left
💡 Hint

Consider how resource requests affect node allocation.

Best Practice
expert
3:00remaining
Choosing the Best Strategy for Cost Optimization

Which strategy provides the most effective cost optimization in a Kubernetes environment running variable workloads?

AUse Cluster Autoscaler with right-sized resource requests and limits, combined with Horizontal Pod Autoscaler for workload scaling.
BSet fixed large resource requests for all pods to avoid scheduling delays and keep nodes fully utilized.
CDisable autoscaling and manually add nodes during peak hours to control costs.
DRun all workloads on a single large node to minimize node count.
Attempts:
2 left
💡 Hint

Think about combining multiple scaling mechanisms and resource management.

Practice

(1/5)
1. What is the main purpose of setting resource requests and limits on Kubernetes pods for cost optimization?
easy
A. To disable autoscaling features in the cluster
B. To control how much CPU and memory a pod can use, preventing waste
C. To increase the number of pods running simultaneously
D. To allow pods to use unlimited resources

Solution

  1. Step 1: Understand resource requests and limits

    Requests define minimum resources a pod needs; limits set maximum usage.
  2. Step 2: Link resource control to cost optimization

    By setting these, Kubernetes schedules pods efficiently and avoids resource waste.
  3. Final Answer:

    To control how much CPU and memory a pod can use, preventing waste -> Option B
  4. Quick Check:

    Resource limits prevent waste = C [OK]
Hint: Requests and limits control pod resource use to save costs [OK]
Common Mistakes:
  • Thinking limits increase pod count
  • Confusing requests with autoscaling
  • Assuming unlimited resources save money
2. Which of the following is the correct YAML snippet to set a CPU request of 500m and a memory limit of 256Mi for a container in Kubernetes?
easy
A. resources:\n requests:\n cpu: '500m'\n limits:\n memory: '256Mi'
B. resources:\n limits:\n cpu: '500m'\n requests:\n memory: '256Mi'
C. resources:\n requests:\n cpu: 500\n memory: 256
D. resources:\n requests:\n cpu: '0.5'\n limits:\n memory: '256MB'

Solution

  1. Step 1: Check correct YAML structure for resources

    Requests and limits must be under resources, with proper indentation and units.
  2. Step 2: Validate units and order

    CPU request '500m' means 0.5 CPU; memory limit '256Mi' is correct unit. resources:\n requests:\n cpu: '500m'\n limits:\n memory: '256Mi' matches this.
  3. Final Answer:

    resources:\n requests:\n cpu: '500m'\n limits:\n memory: '256Mi' -> Option A
  4. Quick Check:

    Correct YAML with proper units = B [OK]
Hint: Requests before limits, use 'm' for CPU and 'Mi' for memory [OK]
Common Mistakes:
  • Swapping requests and limits
  • Using wrong units like 'MB' instead of 'Mi'
  • Omitting quotes around values
3. Given this Horizontal Pod Autoscaler (HPA) YAML snippet:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
minReplicas: 2
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50

What happens when CPU usage exceeds 50%?
medium
A. Pods restart automatically
B. The number of pods decreases to 2 to save cost
C. The number of pods increases up to 5 to handle load
D. CPU limits are increased automatically

Solution

  1. Step 1: Understand HPA behavior with CPU utilization

    HPA increases pod count when average CPU usage exceeds target utilization (50%).
  2. Step 2: Check min and max replicas

    Pods scale between 2 and 5 replicas based on load; exceeding 50% triggers scaling up.
  3. Final Answer:

    The number of pods increases up to 5 to handle load -> Option C
  4. Quick Check:

    CPU > 50% triggers scale up = A [OK]
Hint: HPA scales pods up when CPU usage exceeds target [OK]
Common Mistakes:
  • Thinking pods scale down on high CPU
  • Assuming pods restart on high CPU
  • Believing CPU limits auto-increase
4. You notice your Kubernetes cluster is overspending because pods are not scaling down after load decreases. Which is the most likely cause?
medium
A. CPU requests are set higher than limits
B. Resource limits are set too low
C. Pods have no readinessProbe configured
D. The Horizontal Pod Autoscaler has a high minReplicas value

Solution

  1. Step 1: Analyze autoscaling parameters

    A high minReplicas prevents scaling below that number, causing overspending.
  2. Step 2: Evaluate other options

    Low limits or readiness probes don't directly prevent scaling down; CPU requests > limits is invalid.
  3. Final Answer:

    The Horizontal Pod Autoscaler has a high minReplicas value -> Option D
  4. Quick Check:

    High minReplicas blocks scale down = A [OK]
Hint: Check minReplicas to allow scaling down [OK]
Common Mistakes:
  • Confusing limits with requests
  • Ignoring minReplicas effect
  • Assuming readinessProbe affects scaling
5. You want to optimize costs by automatically scaling your Kubernetes cluster nodes based on pod resource usage. Which combination of tools and settings should you use?
hard
A. Cluster Autoscaler with properly set pod resource requests and limits
B. Manual node scaling with no pod resource limits
C. Disable Horizontal Pod Autoscaler and increase node count permanently
D. Set pod resource limits to zero and rely on node autoscaling

Solution

  1. Step 1: Understand cluster autoscaling

    Cluster Autoscaler adjusts node count based on pod scheduling needs and resource requests.
  2. Step 2: Importance of pod resource requests and limits

    Proper requests and limits let the autoscaler know actual resource needs to scale nodes efficiently.
  3. Step 3: Evaluate other options

    Manual scaling wastes resources; disabling HPA or zero limits causes inefficiency or errors.
  4. Final Answer:

    Cluster Autoscaler with properly set pod resource requests and limits -> Option A
  5. Quick Check:

    Autoscaler + resource requests = cost savings [OK]
Hint: Use Cluster Autoscaler plus pod requests/limits for best cost control [OK]
Common Mistakes:
  • Relying on manual scaling only
  • Disabling autoscaling features
  • Setting resource limits to zero