(Day 37) Task : Kubernetes Namespaces, Limits, and Requests :-

Aditya SharmaAditya Sharma
3 min read

Today, we’re diving into a very important Kubernetes concept — Namespaces, Resource Limits, and Requests. These concepts help in organizing, managing, and optimizing resource allocation in a Kubernetes cluster — crucial for ensuring multi-team collaboration, cost efficiency, and system reliability.

What Are Kubernetes Namespaces?

Namespaces in Kubernetes provide logical isolation between environments and applications. Think of them as separate rooms in the same house — you can keep things organized and independent.

Why Use Namespaces?

  • You want multiple teams working on the same cluster.

  • You need to separate environments: dev, staging, production.

  • You want to enforce resource constraints per team/project.

Creating a Namespace

apiVersion: v1
kind: Namespace
metadata:
  name: dev
kubectl apply -f dev-namespace.yaml

Or directly via CLI:

kubectl create namespace dev

How to Use Namespaces

Switching Between Namespaces:

kubectl config set-context --current --namespace=dev

Creating Resources Within a Namespace:

kubectl create deployment nginx --image=nginx -n dev

Requests and Limits in Kubernetes

Kubernetes allows you to control CPU and Memory for Pods using Requests and Limits.

ResourceTypePurpose
requestsMinimum resource guaranteeScheduler uses this to place pods
limitsMaximum resource capContainer cannot exceed this

Example: CPU/Memory Request & Limit in Pod :

apiVersion: v1
kind: Pod
metadata:
  name: limited-nginx
  namespace: dev
spec:
  containers:
  - name: nginx
    image: nginx
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"

Why Requests and Limits Matter

  • Avoid resource starvation — ensures everyone gets their fair share.

  • Stability — no one pod hogs the node’s resources.

  • Predictability — allows better scheduling and autoscaling.

  • Cost Optimization — avoids over-provisioning in cloud environments.

ResourceQuota for Namespaces

You can enforce policies for each namespace using a ResourceQuota. This ensures that even collectively, pods can’t exceed certain boundaries.

Sample ResourceQuota:

apiVersion: v1
kind: ResourceQuota
metadata:
  name: dev-quota
  namespace: dev
spec:
  hard:
    pods: "10"
    requests.cpu: "2"
    requests.memory: "4Gi"
    limits.cpu: "4"
    limits.memory: "8Gi"

This limits:

  • A maximum of 10 pods

  • 2 CPUs & 4Gi memory for requests

  • 4 CPUs & 8Gi memory for limits

Real-World DevOps Scenario :

Let’s say We’re working on a shared Kubernetes cluster used by:

  • Team A (Namespace: dev)

  • Team B (Namespace: prod)

We want to ensure:

  • Team A doesn’t consume more than 20% of total cluster CPU.

  • Each pod by Team A can only use max 500m CPU and 256Mi memory.

We’d use:

  • Namespace dev for Team A.

  • ResourceQuota for namespace-wide caps.

  • Requests and Limits in Pod definitions.

0
Subscribe to my newsletter

Read articles from Aditya Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Sharma
Aditya Sharma

DevOps Enthusiast | Python | Chef | Docker | GitHub | Linux | Shell Scripting | CI/CD & Cloud Learner | AWS