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.
Start the Minikube Cluster
Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.
Navigate to your working directory:
Open the terminal and navigate to the default project folder:
cd /home/labex/projectStart Minikube:
Start Minikube to initialize a Kubernetes cluster:
minikube start- This command sets up a single-node Kubernetes cluster on your local machine.
- Minikube may take a few minutes to start depending on your system's performance.
Verify Minikube is running:
Check the status of the Minikube cluster:
minikube status- Look for components like
kubeletandapiserverlisted asRunning. - If the cluster is not running, rerun
minikube start.
- Look for components like
If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.
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:
- Create a new YAML file called
limitrange.yamlwith the following contents:
cd ~/project
touch limitrange.yaml
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
- Apply the
limitrange.yamlfile to your Kubernetes cluster using thekubectl applycommand:
kubectl apply -f limitrange.yaml
- 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 "Creating a Simple LimitRange". Here's how you can do it:
- Create a new YAML file called
pod.yamlwith the following contents:
cd ~/project
touch pod.yaml
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.
- Apply the
pod.yamlfile to your Kubernetes cluster using thekubectl applycommand:
kubectl apply -f pod.yaml
- 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.
- 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:
- Create a new YAML file called
pod-exceeding-limits.yamlwith the following contents:
cd ~/project
touch pod-exceeding-limits.yaml
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).
- Apply the
pod-exceeding-limits.yamlfile to your Kubernetes cluster using thekubectl applycommand:
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 "Creating a Simple LimitRange" to modify the resource limits. Here's how you can do it:
- Modify the
limitrange.yamlfile 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
- Apply the updated
limitrange.yamlfile to your Kubernetes cluster using thekubectl applycommand:
kubectl apply -f limitrange.yaml
- 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.


