Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Create a Kubernetes Namespace

Split a cluster into separate areas with namespaces, using kubectl or a YAML file.

VPS and Cloud Servers2 min read4 steps4 screenshots

What a namespace is

A namespace is a virtual cluster inside a real cluster. It groups resources, such as pods and services, and keeps teams and projects apart. Namespaces give you:

  • Isolation. Teams work without touching each other's resources.
  • Quotas. You can limit what each namespace may use.
  • Access control. RBAC rules can apply per namespace.

Kubernetes starts with four namespaces:

  1. default for objects with no other namespace.
  2. kube-system for objects made by Kubernetes itself.
  3. kube-public which every user can read, even without sign-in.
  4. kube-node-lease which holds lease objects for node health.

Create one with kubectl

You need a running cluster. For tests, you can use Minikube on your own machine.

kubectl create namespace my-namespace
Creating a namespace with kubectl

List the namespaces to check:

kubectl get namespaces
The list of namespaces

Create one with a YAML file

A file is easier to keep and share. Save this as namespace.yaml:

apiVersion: v1
kind: Namespace
metadata:
  name: my-namespace
  labels:
    name: my-namespace
The namespace YAML file

Apply it:

kubectl apply -f namespace.yaml
Applying the YAML file

Work with namespaces

Delete a namespace (this also deletes everything inside it):

kubectl delete namespace my-namespace

Use a namespace for a command:

kubectl get pods -n my-namespace

Switch the default namespace of your context:

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

Set a resource quota. Save this as quota.yaml:

apiVersion: v1
kind: ResourceQuota
metadata:
  name: compute-quota
  namespace: my-namespace
spec:
  hard:
    requests.cpu: "2"
    requests.memory: 4Gi
    limits.cpu: "4"
    limits.memory: 8Gi
    pods: "10"
kubectl apply -f quota.yaml
Warning: Deleting a namespace removes all the pods, services and data in it. Check the name twice.

Quick recap

  • kubectl create namespace name makes a namespace.
  • Use -n name or set-context to work inside it.
  • Add quotas to keep one project from using everything.