Kubernetes ResourceQuotas Tutorial

Kubernetes ResourceQuotas Tutorial

This tutorial demonstrates how to use Kubernetes ResourceQuotas to limit the resource consumption (CPU and memory) of pods within a specific namespace. We’ll progressively increase the complexity, exploring common issues and their solutions. This focuses solely on Kubernetes and its resource management capabilities.

Example 1: Initial Setup and Basic Quota

Let’s start with a simple ResourceQuota. This will limit total CPU and memory usage within the ‘development’ namespace to 2 CPUs and 4GiB. We’ll create a YAML file and apply it to our cluster.


apiVersion: v1
kind: ResourceQuota
metadata:
  name: development-quota
  namespace: development
spec:
  hard:
    cpu: "2"
    memory: "4Gi"

Now, let’s apply the ResourceQuota:

kubectl apply -f development-quota.yaml

Verify the quota is in place:

kubectl get resourcequota development-quota -o yaml

Observe the output. The spec.hard section defines the limits. This quota is now active in the ‘development’ namespace.

Example 2: Introducing a Pod that Exceeds the Quota

Let’s create a simple Deployment that attempts to violate our ResourceQuota. This demonstrates the immediate effect of exceeding the defined limits.


apiVersion: apps/v1
kind: Deployment
metadata:
  name: quota-violation-app
  namespace: development
spec:
  replicas: 1
  selector:
    matchLabels:
      app: quota-violation
  template:
    metadata:
      labels:
        app: quota-violation
    spec:
      containers:
      - name: quota-violation-container
        image: busybox:latest
        resources:
          requests:
            cpu: "1"
            memory: "1Gi"
          limits:
            cpu: "2"
            memory: "2Gi"

Apply the Deployment:

kubectl apply -f quota-violation-deployment.yaml

Check the Deployment status:

kubectl get deployment quota-violation-app -o yaml

Examine the events:

kubectl get events --sort-by='.lastTimestamp' -n development

The output will show an event related to the Deployment failing to meet the ResourceQuota. This indicates that the Deployment is being refused to schedule because it attempts to use more resources than allowed.

Example 3: Addressing the Issue – Modifying the Deployment and ResourceQuotas

The Deployment violated the quota. We need to adjust the Deployment’s resource requests and/or the ResourceQuota itself to allow the Deployment to succeed.

First, let’s modify the Deployment to reduce its resource consumption:


apiVersion: apps/v1
kind: Deployment
metadata:
  name: quota-violation-app
  namespace: development
spec:
  replicas: 1
  selector:
    matchLabels:
      app: quota-violation
  template:
    metadata:
      labels:
        app: quota-violation
    spec:
      containers:
      - name: quota-violation-container
        image: busybox:latest
        resources:
          requests:
            cpu: "0.5"
            memory: "512Mi"
          limits:
            cpu: "1"
            memory: "1Gi"

Now apply the updated Deployment:

kubectl apply -f quota-violation-deployment.yaml

Check the Deployment status again:

kubectl get deployment quota-violation-app -o yaml

Observe the events again:

kubectl get events --sort-by='.lastTimestamp' -n development

You should now see events indicating that the Deployment was scheduled successfully. The reduced resource requests satisfied the quota.

To further illustrate the relationship, let’s adjust the ResourceQuota to be more permissive:


apiVersion: v1
kind: ResourceQuota
metadata:
  name: development-quota
  namespace: development
spec:
  hard:
    cpu: "4"
    memory: "8Gi"

Apply the updated quota:

kubectl apply -f development-quota.yaml

Confirm that the quota has been updated:

kubectl get resourcequota development-quota -o yaml

Now, the Deployment should schedule without issues.

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies and similar technologies to enhance your experience on wobizdu.com, analyze site traffic, personalize content, and deliver relevant ads. Some cookies are essential for the site to function, while others help us improve performance and user experience. You may accept all cookies, decline optional ones, or customize your settings. Review our Privacy Policy to learn more.