Introduction
In this lab, you will learn how to use Kubernetes ResourceQuota to set limits on resource consumption in a Kubernetes cluster. ResourceQuota allows you to control and manage the allocation of resources such as CPU and memory for namespaces in a Kubernetes cluster. You will start with simple examples and gradually progress to more complex scenarios. Each step will provide code examples and instructions on how to apply them to your Kubernetes cluster.
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 ResourceQuota
In this step, you will create a simple ResourceQuota that limits the amount of CPU and memory that can be used in a namespace. Here's how you can do it:
- Create a file named
resourcequota.yamlwith the following contents:
apiVersion: v1
kind: ResourceQuota
metadata:
name: example-resourcequota
spec:
hard:
cpu: "1"
memory: "1Gi"
This ResourceQuota sets the following hard limits:
- CPU: 1 core
- Memory: 1 GiB
- Apply the
resourcequota.yamlfile to your Kubernetes cluster using thekubectl applycommand:
kubectl apply -f resourcequota.yaml
- Verify that the ResourceQuota has been created successfully by running the following command:
kubectl describe resourcequota example-resourcequota
You should see the details of the ResourceQuota in the output.
Applying ResourceQuota to a Namespace
In this step, you will apply the ResourceQuota created in Step:Creating a ResourceQuota to a namespace. Here's how you can do it:
- Create a file named
namespace.yamlwith the following contents:
apiVersion: v1
kind: Namespace
metadata:
name: example-namespace
This namespace definition creates a namespace named example-namespace.
- Apply the
namespace.yamlfile to your Kubernetes cluster using thekubectl applycommand:
kubectl apply -f namespace.yaml
- Apply the ResourceQuota to the
example-namespacenamespace using thekubectl applycommand:
kubectl apply -f resourcequota.yaml -n example-namespace
- Verify that the ResourceQuota has been applied to the namespace by running the following command:
kubectl describe namespace example-namespace
You should see the details of the ResourceQuota applied to the namespace in the output.
Testing ResourceQuota Enforcement
In this step, you will create a pod that exceeds the resource limits defined in the ResourceQuota, and verify that the ResourceQuota enforces the limits. Here's how you can do it:
- Create a file named
pod-exceeding-limits.yamlwith 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 ResourceQuota created in Step:Creating a ResourceQuota (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": pods "example-pod-exceeding-limits" is forbidden: exceeded quota: example-resourcequota, requested: cpu=2,memory=2Gi, used: cpu=0,memory=0, limited: cpu=1,memory=1Gi.
Modifying ResourceQuota
In this step, you will learn how to modify an existing ResourceQuota to update the resource limits. Here's how you can do it:
- Edit the
resourcequota.yamlfile to update the CPU and memory limits to higher values:
apiVersion: v1
kind: ResourceQuota
metadata:
name: example-resourcequota
spec:
hard:
cpu: "2"
memory: "2Gi"
This updates the ResourceQuota to allow for higher limits of CPU and memory (2 cores and 2 GiB respectively).
- Apply the updated
resourcequota.yamlfile to your Kubernetes cluster using thekubectl applycommand:
kubectl apply -f resourcequota.yaml
- Verify that the ResourceQuota has been updated by running the following command:
kubectl describe resourcequotas example-resourcequota
You should see the updated CPU and memory limits in the output.
Summary
In this lab, you learned how to use Kubernetes ResourceQuota to set limits on resource consumption in a Kubernetes cluster. You started with creating a simple ResourceQuota, applying it to a namespace, and testing its enforcement by creating a pod that exceeds the resource limits. You also learned how to modify an existing ResourceQuota to update the resource limits. ResourceQuota is a powerful tool for managing resources in a Kubernetes cluster and ensuring that applications do not consume excessive resources.


