CodeBlanks Logo Icon CodeBlanks

Default Namespace

Learn the default and kube-system Namespaces.

The default namespace

To avoid typing -n constantly, the active context's default namespace is changed. This setting is written into the kubeconfig and affects all later commands:

kubectl config set-context --current --namespace=dev
kubectl config view --minify -o jsonpath='{..namespace}'
# dev
kubectl get pods     # now the pods in dev

Small tools like kubens reduce this to one command. Warning: forgetting which namespace you are in on a production cluster is dangerous; showing the context and namespace name at the start of the shell line is a common precaution.

Verifying the active namespace

Two ways to see which namespace you are in: config get-contexts shows the NAMESPACE column; config view --minify prints only the active context:

$ kubectl config get-contexts
CURRENT   NAME       CLUSTER    AUTHINFO   NAMESPACE
*         minikube   minikube   minikube   dev
$ kubectl config set-context --current --namespace=default   # go back

Practice Change the default

Write three commands: set the active context's namespace to dev; list the contexts; list the pods without writing -n.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl config set-context --current --namespace=dev
kubectl config get-contexts
kubectl get pods

Expected output

Context "minikube" modified.
CURRENT   NAME       CLUSTER    AUTHINFO   NAMESPACE
*         minikube   minikube   minikube   dev
NAME                  READY   STATUS    RESTARTS   AGE
web-6b9c8d7f5-xyzab   1/1     Running   0          1h

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does set-context --current --namespace=dev change?

  • It deletes the default namespace in the cluster
  • Every user's namespace
  • It moves pods to dev
  • The active context's default namespace (in the kubeconfig) Correct answer

Explanation: The setting only affects your kubeconfig file.

2 Change the active context's namespace.

Question
kubectl config set-context ____ --namespace=dev
Answer
kubectl config set-context --current --namespace=dev

Explanation: --current selects the active context.

3 Match.

Term Meaning
set-context --current Change the active context
config view --minify Only the active context
kubens Namespace switching tool
NAMESPACE column get-contexts output

All lessons in Intermediate