DevOps Blog - Nicolas Paris

Kubernetes namespaces

Kubernetes

This is a very quick note about kubernetes namespaces.

kubectl get namespace
k get ns

Set the current namespace

kubectl config set-context --current --namespace=<insert-namespace-name-here>

Delete a namespace will deletes everything under the namespace.

This alias can be used to swith namespac in kubernetes quickly. In the CKAD exam for example.

alias kns='kubectl config set-context --current --namespace '

It should be a good practice for the CKAD exam to switch namespace often as every exercice has it's own namespace. As part as the training, always create a namespace and jump into it quickly without thinking about it.

Let's check where does this come from with :

controlplane $ k config view
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://172.17.0.12:6443
name: kubernetes
contexts:
- context:
cluster: kubernetes
namespace: ckad
user: kubernetes-admin
name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: REDACTED
client-key-data: REDACTED

The idea is to change the namespace on this block :

contexts:
- context:
cluster: kubernetes
namespace: ckad
user: kubernetes-admin
name: kubernetes-admin@kubernetes

Krew Plugin

This is a much easier way to change context (cluster) or namespace, with krew, a plugin manager for kubernetes. Listing and switching namespace will be as easy as :

kns mynamespace