Kubernetes namespaces: What they are and how to use them

Learn how Kubernetes namespaces work with practical kubectl examples. Covers creating namespaces, resource quotas, switching context, and environment separation patterns.

4 lessons · 11 min · Intermediate

2 minutes reading time

Written by

Civo Team
Civo Team

Marketing Team at Civo

Namespaces provide logical separation within a Kubernetes cluster. They allow multiple teams or environments to share the same physical cluster while keeping their resources isolated from each other. You can apply resource quotas and access policies per namespace, making it practical to run dev, staging, and production workloads in the same cluster without them interfering with each other.

What are Kubernetes namespaces?

A namespace is a virtual cluster within a physical cluster. Resources inside one namespace are invisible to resources in another unless you explicitly reference them by their full DNS name. Every Kubernetes cluster starts with four namespaces:

  • default : where your resources go if you do not specify a namespace
  • kube-system : cluster components like the API server, scheduler, and CoreDNS
  • kube-public : publicly readable resources, rarely used directly
  • kube-node-lease : node heartbeat objects used for availability detection

Viewing and creating namespaces

List all namespaces in the cluster:

kubectl get ns

Expected output:

NAME STATUS AGE
default Active 5d
kube-node-lease Active 5d
kube-public Active 5d
kube-system Active 5d

Create a new namespace:

kubectl create namespace dev

Verify it was created:

kubectl get ns dev

Expected output:

NAME STATUS AGE
dev Active 5s

Running resources in a specific namespace

Create a pod in the default namespace:

kubectl run nginx --image=nginx

Create a pod with the same name in the dev namespace:

kubectl run nginx --image=nginx -n dev

Verify the pod is running in the dev namespace:

kubectl get pods -n dev

Expected output:

NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 10s

Both pods can exist simultaneously because they are in different namespaces. This is useful when running different versions of the same application in separate environments.

Namespace-per-environment pattern

civo-namespace-per-environment

The most common real-world use of namespaces is separating environments within the same cluster. Rather than running separate clusters for dev, staging, and production, teams create one namespace per environment:

kubectl create namespace dev
kubectl create namespace staging
kubectl create namespace production

This approach keeps infrastructure costs low while still maintaining separation. The key is combining namespaces with ResourceQuotas to prevent one environment from consuming all cluster resources.

Limiting resources with ResourceQuota

A ResourceQuota sets hard limits on how much CPU, memory, and how many objects a namespace can use. Without quotas, a runaway process in dev could starve production of resources.

Apply a ResourceQuota to the dev namespace:

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

Verify the quota is applied:

kubectl get resourcequota -n dev

Expected output:

NAME AGE REQUEST LIMIT
dev-quota 5s pods: 0/20, requests.cpu: 0/2, ... limits.cpu: 0/4, ...

Warning: always apply ResourceQuotas to shared namespaces in production clusters. Without them, a single namespace can consume all available cluster resources and starve other workloads.

Switching namespace context

By default, kubectl commands operate on the default namespace. Specifying -n <namespace> every time gets repetitive. Set a default namespace for your current context:

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

Confirm the change:

kubectl config view --minify | grep namespace

Expected output:

namespace: dev

Now kubectl get pods will show pods from the dev namespace without needing -n dev.

Deleting a namespace

Warning: deleting a namespace deletes every resource inside it — pods, services, deployments, ConfigMaps, secrets, and PVCs. This cannot be undone. Always check what is running before deleting.

Check what is in a namespace before deleting:

kubectl get all -n dev

Delete the namespace:

kubectl delete namespace dev
Civo Team
Civo Team

Marketing Team at Civo

Civo is the Sovereign Cloud and AI platform designed to help developers and enterprises build without limits. We bridge the gap between the openness of the public cloud and the rigorous security of private environments, delivering full cloud parity across every deployment. As a team, we are dedicated to providing scalable compute, lightning-fast Kubernetes, and managed services that are ready in minutes. Through CivoStack Enterprise and our FlexCore appliance, we empower organizations to maintain total data sovereignty on their own hardware.

Our mission is to make the cloud faster, simpler, and fairer. By providing enterprise-grade NVIDIA GPUs and streamlined model management, we ensure that high-performance AI and machine learning are accessible to everyone. Built for transparency and performance, the Civo Team is here to give you total control over your infrastructure, your data, and your spend.

View author profile