kubectl: set a given namespace as your default

2 min read | by Jordi Prats

When you need to work on some specific namespace it is quite annoying yo have to specify at each kubectl command the same namespace over and over again. It's much easier to change the current context to use another namespace by default.

Using kubectl config set-context we can configure which namespace we want to use if we are not specifying one. For example, to set it to the demo namespace for the current context that we are using we can use the following command:

$ kubectl config set-context --current --namespace demo
Context "arn:aws:eks:us-west-2:615418998474:cluster/demo" modified.

Under the hood what it does is to modify the config file to set the default namespace. We can see it looking at the output of kubectl config view or directly looking at our config file, most likely is going to be ~/.kube/config:

(...)
- context:
    cluster: arn:aws:eks:us-west-2:615418998474:cluster/demo
    namespace: spinnaker
    user: arn:aws:eks:us-west-2:615418998474:cluster/demo
  name: arn:aws:eks:us-west-2:615418998474:cluster/demo
(...)

Posted on 25/01/2021