DEV Community

Cover image for Define your Kubernetes namespaces now!
Adi Polak
Adi Polak

Posted on • Updated on

Define your Kubernetes namespaces now!

Yeah, but why?

Namespaces help us divide the cluster resources between multiple users, as well as help split the resource quote.
This is important for when we want to use Kubernetes in production and share the cluster with many products/teams.

How do we do it?

Prerequisites:

  1. Kubernetes cluster - Quick start with aks
  2. Kubectl command line installed

Define the namespace using a YAML file -:

{
  "apiVersion": "v1",
  "kind": "Namespace",
  "metadata": {
    "name": "development",
    "labels": {
        "name": "development"
        }
    }
}

In the example, we call the namespace - development.
Name the file - namespace-dev.json

From the command line, run:

kubectl create -f namespace-dev.json

This command will actually create the namespace.

Check yourself with this command - it will show you all the namespace:

kubectl get namespaces --show-labels

Let's spin pods. But first, check what already exists:

kubectl config view

Get the current context:

kubectl config current-context

Let's say we got back dev_cluster as the current context.
This will return the cluster context name and we'll use it to define the development namespace for our context:

Use set-context to set the context with new name dev under the namespsace of development:

kubectl config set-context dev --namespace=development \
  --cluster=dev_cluster \
  --user=dev_cluster

Switch to the new context:

kubectl config use-context dev

Now everything you'll do will take place in this context with the defined namespace.

This was K8s namespaces in under 2 min, part of K8s bitesize series.

Discussion (2)

Collapse
rohansawant profile image
Rohan Sawant

Yep! This helps keep my OCDs in check!

It's similar to docker-composes, container names. I am pretty finicky about those as well. 😅

An awesome read!

Collapse
adipolak profile image
Adi Polak Author

Thank you, Rohan!