Kubernetes LimitRange Resource Management

KubernetesKubernetesBeginner
Practice Now

Introduction

In this lab, you will learn about Kubernetes LimitRange, which is used to set limits on resource consumption in Kubernetes pods. LimitRange is a Kubernetes feature that helps you manage the resources allocated to pods and prevent resource contention issues.

You will go through a series of step-by-step instructions to understand the different aspects of LimitRange, starting from simple examples and gradually moving towards more complex scenarios. Each step will include code examples in the form of YAML manifests that you can apply to your Kubernetes cluster to see the effects of LimitRange in action.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") subgraph Lab Skills kubernetes/describe -.-> lab-15819{{"`Kubernetes LimitRange Resource Management`"}} kubernetes/get -.-> lab-15819{{"`Kubernetes LimitRange Resource Management`"}} kubernetes/apply -.-> lab-15819{{"`Kubernetes LimitRange Resource Management`"}} end

Creating a Simple LimitRange

In this step, you will create a simple LimitRange that sets limits on the CPU and memory resources for pods in a namespace. Here's how you can do it:

  1. Create a new YAML file called limitrange.yaml with the following contents:
apiVersion: v1
kind: LimitRange
metadata:
  name: example-limitrange
spec:
  limits:
    - type: Container
      max:
        cpu: "1"
        memory: "1Gi"
      min:
        cpu: "100m"
        memory: "100Mi"
      default:
        cpu: "500m"
        memory: "500Mi"

This LimitRange sets the following limits:

  • Maximum CPU: 1 core
  • Maximum memory: 1 GiB
  • Minimum CPU: 100 milli-cores (100m)
  • Minimum memory: 100 MiB
  • Default CPU: 500 milli-cores (500m)
  • Default memory: 500 MiB
  1. Apply the limitrange.yaml file to your Kubernetes cluster using the kubectl apply command:
kubectl apply -f limitrange.yaml
  1. Verify that the LimitRange has been created successfully by running the following command:
kubectl describe limitrange example-limitrange

You should see the LimitRange example-limitrange listed with the limits you specified in the spec section.

Applying LimitRange to Pods

In this step, you will create a pod that is subject to the LimitRange you created in Step:Creating a Simple LimitRange. Here's how you can do it:

  1. Create a new YAML file called pod.yaml with the following contents:
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
    - name: nginx
      image: nginx

This pod definition creates a simple pod with a single container running the Nginx image.

  1. Apply the pod.yaml file to your Kubernetes cluster using the kubectl apply command:
kubectl apply -f pod.yaml
  1. Verify that the pod has been created successfully by running the following command:
kubectl get pods example-pod

You should see the pod example-pod listed with a status of Running.

  1. Check the resource limits applied to the pod by running the following command:
kubectl describe pod example-pod

You should see the CPU and memory limits for the pod as defined.

Testing LimitRange Enforcement

In this step, you will test the enforcement of the LimitRange by trying to create a pod that exceeds the resource limits defined in the LimitRange. Here's how you can do it:

  1. Create a new YAML file called pod-exceeding-limits.yaml with the following contents:
apiVersion: v1
kind: Pod
metadata:
  name: example-pod-exceeding-limits
spec:
  containers:
    - name: nginx
      image: nginx
      resources:
        limits:
          cpu: "2"
          memory: "2Gi"

This pod definition creates a pod with a container that requests resources that exceed the limits set in the LimitRange (CPU: 2 cores, memory: 2 GiB).

  1. Apply the pod-exceeding-limits.yaml file to your Kubernetes cluster using the kubectl apply command:
kubectl apply -f pod-exceeding-limits.yaml

You can see that the operation to create Pod is rejected, The error message is the Error from server (Forbidden): error when creating "pod-exceeding-limits. yaml": pod "example-pod-exceeding-limits " Forbidden: [Maximum cpu usage per container is 1, but limited to 2, maximum memory usage per container is 1Gi, but limited to 2Gi].

Updating LimitRange

In this step, you will update the LimitRange created in Step:Creating a Simple LimitRange to modify the resource limits. Here's how you can do it:

  1. Modify the limitrange.yaml file to update the resource limits as per your requirements. For example:
apiVersion: v1
kind: LimitRange
metadata:
  name: example-limitrange
spec:
  limits:
    - type: Container
      max:
        cpu: "2"
        memory: "2Gi"
      min:
        cpu: "200m"
        memory: "200Mi"
      default:
        cpu: "1"
        memory: "1Gi"

This updated LimitRange sets the following limits:

  • Maximum CPU: 2 cores
  • Maximum memory: 2 GiB
  • Minimum CPU: 200 milli-cores (200m)
  • Minimum memory: 200 MiB
  • Default CPU: 1 core
  • Default memory: 1 GiB
  1. Apply the updated limitrange.yaml file to your Kubernetes cluster using the kubectl apply command:
kubectl apply -f limitrange.yaml
  1. Verify that the LimitRange has been updated successfully by running the following command:
kubectl describe limitranges example-limitrange

You should see the updated resource limits reflected in the output.

Summary

In this lab, you learned how to use Kubernetes LimitRange to set limits on resource consumption in pods. You started with creating a simple LimitRange, applied it to a pod, tested its enforcement, and then updated the LimitRange to modify the resource limits. LimitRange is a powerful tool for managing resources in Kubernetes and ensuring efficient resource utilization in your cluster.

Other Kubernetes Tutorials you may like