
Namespaces and resource isolation
Build a multi-tenant empire. Learn how to divide your cluster into logical virtual clusters and enforce strict resource boundaries between teams.
Namespaces and Resource Isolation: Building a Multi-Tenant Fortress
So far, we have looked at a Kubernetes cluster as a single, unified entity. But in the real world—inside a company with multiple teams, hundreds of developers, and dozens of different products—you cannot just throw everyone into the same bucket.
Imagine If the "Marketing Team" accidentally deleted the "Banking Team's" production database because they both named their pod database. Or if the "Testing Team" ran a massive load test that consumed all the cluster's CPU, causing the "Payment Service" to crash.
This is why Namespaces and Resource Isolation are critical. They allow you to turn one physical Kubernetes cluster into dozens of Virtual Clusters. In this lesson, we will explore the mechanisms of isolation: Namespaces, ResourceQuotas, and LimitRanges. We will learn how to design a multi-tenant environment that is secure, fair, and scalable.
1. The Namespace: A Virtual Cluster
A Namespace is a logical partition in a Kubernetes cluster. Every object you create in K8s (Pods, Services, Deployments) belongs to a namespace. If you don't specify one, it goes into the default namespace.
The Problem: Global Conflict
Kubernetes objects must have unique names. You cannot have two pods named api-server in the same namespace.
Namespaces Provide a Scope. You can have a prod namespace and a dev namespace, and both can have a pod named api-server without any conflict.
The Default Namespaces (Hands-Off!):
- default: Where your stuff goes if you’re lazy.
- kube-system: Where the Control Plane components (Brain) live.
- kube-public: Reachable by everyone (internal only).
- kube-node-lease: Used for node heartbeats.
2. When to Use Namespaces
How should you divide your "World"? There are two common strategies:
- Environment-Based:
dev,staging,production. - Team/Product-Based:
payments-team,inventory-team,ai-research-team.
The Hierarchy of Access
Namespaces are the primary boundary for RBAC (Role-Based Access Control). You can give a developer "Admin" access to the dev namespace, but "View Only" access to the production namespace. This is the single most important safety feature for a growing engineering team.
3. ResourceQuotas: The "Budget" Enforcer
One team should not be allowed to starve the rest of the cluster of resources. To prevent this, we use the ResourceQuota object.
A ResourceQuota sets a "Hard Limit" on the total amount of resources a single namespace can consume.
What can you limit?
- Compute: Total CPU and RAM (e.g., "This team can only use 10 CPUs total").
- Storage: Total disk space for PersistentVolumes.
- Object Count: How many Pods, Services, or ConfigMaps can exist in this namespace.
Example: The "Dev" Team Budget
apiVersion: v1
kind: ResourceQuota
metadata:
name: dev-quota
namespace: dev # Apply this to the development namespace
spec:
hard:
requests.cpu: "4"
requests.memory: 8Gi
limits.cpu: "8"
limits.memory: 16Gi
pods: "10"
services: "5"
If a developer in the dev namespace tries to create their 11th pod, the API Server will reject the request with a "Forbidden" error. This prevents accidental runaway costs.
4. LimitRanges: Setting the "Defaults"
What if a developer forgets to set "Requests" and "Limits" (Lesson 3) for their pod? By default, K8s would let that pod take up as much space as it wants.
A LimitRange solves this by setting automated defaults for any pod created in a specific namespace.
Functions of a LimitRange:
- Default Values: If a user doesn't specify limits, K8s injects these values automatically.
- Max/Min Boundaries: Prevents a user from requesting a pod that is "too small" or "too big" for the hardware.
- Ratio Enforcement: Forcing a specific ratio between CPU and RAM.
Example: Protecting the Namespace
apiVersion: v1
kind: LimitRange
metadata:
name: dev-limits
namespace: dev
spec:
limits:
- default: # Default LIMITS
cpu: 500m
memory: 512Mi
defaultRequest: # Default REQUESTS
cpu: 100m
memory: 256Mi
type: Container
5. Visualizing the Isolation Layers
graph TD
subgraph "Kubernetes Physical Cluster"
subgraph "Namespace: Production"
Deploy1["Main App"]
Quota1["ResourceQuota (Unlimited)"]
end
subgraph "Namespace: Marketing-Dev"
Deploy2["Static Site"]
Quota2["ResourceQuota (Strict: 2CPU/2GB)"]
Limit1["LimitRange (Default: 0.1CPU)"]
end
end
ResourcePool["Total Cluster CPU: 64 Core"]
ResourcePool --- Quota1
ResourcePool --- Quota2
6. Communication Across Namespaces
Does a namespace act as a firewall? No.
By default, a Pod in the dev namespace can ping a Pod in the production namespace.
Cross-Namespace DNS
To talk to a service in another namespace, you simply add the namespace name to the DNS call:
- Same namespace:
http://my-db - Other namespace:
http://my-db.production.svc.cluster.local
To actually Block traffic between namespaces, you must use Network Policies (Coming in Module 5).
7. AI Implementation: Multi-Tenant AI Experimentation
Imagine you have many teams experimenting with LangChain and OpenAI/Bedrock keys. You want to give each team a playground where they can't break anything.
- Namespace: Create
ai-lab-team-a. - Secret: Put Team A's Bedrock API keys into a Secret inside that namespace. Pods in
ai-lab-team-bcannot see this secret. - Quota: Set a limit on GPUs so one team doesn't hog all the expensive hardware for a failed experiment.
- Taints/Tolerations: (Lesson 1) Ensure only these AI pods can land on the expensive "GPU-optimized" nodes.
This combination of Namespaces, Quotas, and Taints allows you to build a secure "Self-Service" AI experimentation platform for your entire company.
8. Summary and Key Takeaways
- Namespaces: Logical boundaries for naming and RBAC. They allow multiple teams to co-exist.
- ResourceQuotas: Enforce a "Hard Budget" on a team's total consumption.
- LimitRanges: Provide "Safety Rails" by setting default values and ensuring pod sizes are reasonable.
- Isolation: Provides security and "Fair Share" scheduling in a shared cluster.
Congratulations!
You have completed Module 2: Kubernetes Architecture. You have successfully looked under the hood and understood the Brain, the Body, the Nervous System, and the Laws of the Cluster.
Next Stop: In Module 3: Kubernetes Objects, we will stop looking at the "Engine" and start looking at the "Parts." We will master the YAML manifests for Pods, Deployments, Services, and Persistent Storage.
9. SEO Metadata & Keywords
Focus Keywords: Kubernetes Namespaces tutorial, K8s ResourceQuotas vs LimitRanges, multi-tenant Kubernetes architecture, cross-namespace communication K8s, Kubernetes isolation best practices, scaling AI teams on K8s.
Meta Description: Master the art of multi-tenancy in Kubernetes. Learn how to use Namespaces, ResourceQuotas, and LimitRanges to build a secure, isolated, and high-performance environment for multiple teams and production workloads.