Kubernetes Resource Quota Management

KubernetesKubernetesBeginner
Practice Now

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.


Skills Graph

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

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:

  1. Create a file named resourcequota.yaml with 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
  1. Apply the resourcequota.yaml file to your Kubernetes cluster using the kubectl apply command:
kubectl apply -f resourcequota.yaml
  1. 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:

  1. Create a file named namespace.yaml with the following contents:
apiVersion: v1
kind: Namespace
metadata:
  name: example-namespace

This namespace definition creates a namespace named example-namespace.

  1. Apply the namespace.yaml file to your Kubernetes cluster using the kubectl apply command:
kubectl apply -f namespace.yaml
  1. Apply the ResourceQuota to the example-namespace namespace using the kubectl apply command:
kubectl apply -f resourcequota.yaml -n example-namespace
  1. 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:

  1. Create a file named 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 ResourceQuota created in Step:Creating a ResourceQuota (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": 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:

  1. Edit the resourcequota.yaml file 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).

  1. Apply the updated resourcequota.yaml file to your Kubernetes cluster using the kubectl apply command:
kubectl apply -f resourcequota.yaml
  1. 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.

Other Kubernetes Tutorials you may like