☑️Day 39: Exploring Resource Quotas in Kubernetes🚀

☑️Day 39: Exploring Resource Quotas in Kubernetes🚀

🔹Table of Contents :

  • What is a Resource Quota?

  • Components of a Resource Quota

    • Object Count

    • Compute Resource

  • Task 1: Object Count Quota

  • Task 2: Compute Resources Quota

  • Detailed Step-by-Step Commands

  • Real-World Use Cases

  • Key Learnings


1. What is a Resource Quota?

A Resource Quota in Kubernetes ensures that a given namespace does not exceed specified resource consumption limits. It allows cluster administrators to enforce constraints like the number of pods, CPU requests, memory usage, etc., preventing overconsumption of resources in multi-tenant environments.


2. Components of a Resource Quota

There are two main components of a resource quota:

  • Object Count: This limits the number of Kubernetes objects (like Pods, Services, etc.) within a namespace.

  • Compute Resource: This controls the CPU and memory that a namespace can request and consume.


3. Task 1: Object Count Quota

In this task, I learned how to limit the number of pods in a namespace. Here’s a breakdown:

Steps:

  • First, check the nodes available in the cluster:

      kubectl get nodes
    
  • Create a new namespace called sit:

      kubectl create ns sit
    
  • Describe the newly created namespace:

      kubectl describe ns sit
    
  • Create a ResourceQuota YAML file to limit the pod count to 2:

      # object_count_quota.yaml
      apiVersion: v1
      kind: ResourceQuota
      metadata:
        name: pod-count-quota
        namespace: sit
      spec:
        hard:
          pods: "2"
    
  • Apply the resource quota:

      kubectl apply -f object_count_quota.yaml
    
  • Create the first pod (po1.yaml):

      # po1.yaml
      apiVersion: v1
      kind: Pod
      metadata:
        name: pod1
        namespace: sit
      spec:
        containers:
        - name: nginx
          image: nginx:1.17
    
  • Apply the pod configuration:

      kubectl apply -f po1.yaml
    
  • Create the second pod (pod2.yaml) with the same specifications:

      kubectl apply -f pod2.yaml
    
  • Try creating a third pod (pod3.yaml), but it should throw an error as the pod count exceeds the limit:

      kubectl apply -f pod3.yaml
    

Outcome: We successfully applied the object count limit, and trying to create the third pod resulted in an error.


4. Task 2: Compute Resources Quota

In this task, I learned how to restrict CPU and memory resources for the namespace.

Steps:

  • Create a ResourceQuota YAML file to limit CPU and memory usage:

      # compute_resources_quota.yaml
      apiVersion: v1
      kind: ResourceQuota
      metadata:
        name: compute-resources-quota
        namespace: sit
      spec:
        hard:
          requests.cpu: "0.5"
          requests.memory: "500Mi"
          limits.cpu: "1"
          limits.memory: "1Gi"
    
  • Apply the resource quota:

      kubectl apply --dry-run=client -f compute_resources_quota.yaml
    
  • Create the first pod (pod1.yaml) with specific CPU and memory requests/limits:

      # pod1.yaml
      apiVersion: v1
      kind: Pod
      metadata:
        name: pod1
        namespace: sit
      spec:
        containers:
        - name: nginx
          image: nginx:1.17
          resources:
            requests:
              memory: "250Mi"
              cpu: "0.1"
            limits:
              memory: "500Mi"
              cpu: "0.5"
    
  • Apply the pod configuration:

      kubectl apply -f pod1.yaml
    
  • Try creating the second pod with the same specifications (pod2.yaml) and apply it:

      kubectl apply -f pod2.yaml
    
  • Try creating a third pod (pod4.yaml), but it should throw an error due to resource quota limits:

      kubectl apply -f pod4.yaml
    

Outcome: The third pod creation failed as it exceeded the defined CPU and memory limits.


5. Detailed Step-by-Step Commands

Here are all the commands used to work with Resource Quotas:

  1. Get nodes:

     kubectl get nodes
    
  2. Create namespace:

     kubectl create ns sit
    
  3. Describe namespace:

     kubectl describe ns sit
    
  4. Apply Object Count Resource Quota:

     kubectl apply -f object_count_quota.yaml
    
  5. Apply Compute Resource Quota:

     kubectl apply --dry-run=client -f compute_resources_quota.yaml
    
  6. Apply Pod Configurations:

     kubectl apply -f po1.yaml
     kubectl apply -f pod2.yaml
    
  7. Check Pods in Namespace:

     kubectl get pods -n sit
    
  8. Delete Pods and Resource Quota:

     kubectl delete -f pod1.yaml
     kubectl delete -f object_count_quota.yaml
    

6. Real-World Use Cases

  • Managing Resource Allocation: Resource quotas are vital in production environments with shared clusters where multiple teams or projects run their applications. They prevent one project from consuming all the resources and ensure balanced resource distribution.

  • Preventing Pod Over-Provisioning: When deploying applications that require a large number of pods, setting a pod count quota can help administrators avoid runaway scenarios.

  • Scaling and Resource Optimization: Compute resource quotas help manage and optimize CPU and memory usage, ensuring applications do not overconsume and affect the performance of other workloads.


7. Key Learnings

Learning Resource Quotas provided me with insights into how Kubernetes manages resource allocation and control in a multi-tenant environment. By practicing both object count and compute resource quotas, I understood how to limit resources for namespaces and prevent resource hogging, ensuring fair usage for all workloads. Stay tuned for more Kubernetes learnings!


🚀Thanks for joining me on Day 39! Let’s keep learning and growing together!

Happy Learning! 😊

#90DaysOfDevOps

💡
Follow for more updates on LinkedIn , Github and Twitter(X)