0
0
Kubernetesdevops~5 mins

Resource requests and limits in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run applications in Kubernetes, you need to tell the system how much CPU and memory your app needs. Resource requests and limits help Kubernetes decide how to share the computer's power fairly and keep apps running smoothly without crashing or slowing down.
When you want to make sure your app always gets enough CPU and memory to work well.
When you want to stop one app from using too much CPU or memory and slowing down others.
When you want Kubernetes to schedule your app only on nodes that have enough free resources.
When you want to avoid your app being killed because it uses too much memory.
When you want to optimize your cluster's resource usage and avoid wasting CPU or memory.
Config File - pod.yaml
pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: resource-demo-pod
spec:
  containers:
  - name: demo-container
    image: nginx
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"

This YAML file creates a pod named resource-demo-pod with one container running the nginx image.

The resources section sets requests and limits for CPU and memory:

  • requests tell Kubernetes the minimum resources the container needs to start and run.
  • limits set the maximum resources the container can use.

Here, the container requests 64Mi memory and 250m CPU (0.25 CPU cores), and limits are 128Mi memory and 500m CPU (0.5 CPU cores).

Commands
This command creates the pod with the resource requests and limits defined in the pod.yaml file.
Terminal
kubectl apply -f pod.yaml
Expected OutputExpected
pod/resource-demo-pod created
This command shows the status and details of the pod to confirm it is running with the specified resources.
Terminal
kubectl get pods resource-demo-pod -o wide
Expected OutputExpected
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES resource-demo-pod 1/1 Running 0 10s 10.244.1.5 worker-node-1 <none> <none>
-o wide - Shows extra details like IP and node name
This command shows detailed information about the pod, including the resource requests and limits set for the container.
Terminal
kubectl describe pod resource-demo-pod
Expected OutputExpected
Name: resource-demo-pod Namespace: default Priority: 0 Node: worker-node-1/192.168.1.10 Start Time: Thu, 01 Jun 2023 10:00:00 +0000 Labels: <none> Annotations: <none> Status: Running IP: 10.244.1.5 Containers: demo-container: Container ID: docker://abcdef123456 Image: nginx Image ID: docker-pullable://nginx@sha256:abcdef1234567890 Port: <none> Host Port: <none> State: Running Started: Thu, 01 Jun 2023 10:00:05 +0000 Ready: True Restart Count: 0 Limits: cpu: 500m memory: 128Mi Requests: cpu: 250m memory: 64Mi Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: <none> QoS Class: Burstable Node-Selectors: <none> Tolerations: <none> Events: <none>
Key Concept

If you remember nothing else from this pattern, remember: resource requests reserve minimum CPU and memory for your app, and limits cap the maximum your app can use to keep the system stable.

Common Mistakes
Setting resource limits lower than requests
Kubernetes will reject the pod because limits must be equal or higher than requests.
Always set limits equal to or higher than requests for both CPU and memory.
Not setting any resource requests or limits
The pod may get scheduled on a node without enough resources or use too much CPU/memory, causing instability.
Always define at least resource requests to help Kubernetes schedule your pod properly.
Using too high resource requests unnecessarily
This wastes cluster resources and may prevent other pods from scheduling.
Set requests based on actual app needs, not just maximum possible usage.
Summary
Create a pod YAML file with resource requests and limits under the container's resources section.
Apply the pod configuration using kubectl apply -f pod.yaml to create the pod.
Check the pod status and details with kubectl get pods and kubectl describe pod to verify resource settings.