DEV Community

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

Posted on Edited 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"
        }
    }
}
🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini 🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini

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
🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini 🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini

This command will actually create the namespace.

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

kubectl get namespaces --show-labels
🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini 🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini

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

kubectl config view
🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini 🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini

Get the current context:

kubectl config current-context
🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini 🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini

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
🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini 🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini

Switch to the new context:

kubectl config use-context dev
🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini 🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini

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.

Top comments (2)

🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini
 
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!

🔥 Jadwal & Hasil: adipolak/define your kubernetes namespaces now no - Pertandingan Hari Ini
 
adipolak profile image
Adi Polak •

Thank you, Rohan!