Skip to content

kubectl namespace context configuration

kubectl uses a context to determine the cluster, user, and default namespace for command-line operations. By default, if no namespace is specified, the default namespace is used.^[400-devops__06-Kubernetes__k8s-ithelp__Day22__README.md] To avoid repeatedly passing the --namespace flag, you can configure the current context to permanently set a preferred namespace for subsequent commands^[400-devops__06-Kubernetes__k8s-ithelp__Day22__README.md].

Setting a Persistent Namespace

To change the default namespace for the current context, use the kubectl config set-context command with the --namespace flag^[400-devops__06-Kubernetes__k8s-ithelp__Day22__README.md]. Applying the --current flag updates the active context immediately^[400-devops__06-Kubernetes__k8s-ithelp__Day22__README.md].

Command Syntax

[kubectl](<./kubectl.md>) config set-context --current --namespace=<namespace-name>

Verification To verify that the configuration has been applied, you can use the config view command^[400-devops__06-Kubernetes__k8s-ithelp__Day22__README.md].

[kubectl](<./kubectl.md>) config view --minify | grep namespace:

Alternative: Request-Level Specification

If a persistent change is not desired, the namespace can be specified for individual requests using the --namespace parameter^[400-devops__06-Kubernetes__k8s-ithelp__Day22__README.md].

[kubectl get pods](<./kubectl-get-pods.md>) --namespace=demo-namespace
[kubectl](<./kubectl.md>) run nginx --image=nginx --namespace=demo-namespace

Sources

^[400-devops__06-Kubernetes__k8s-ithelp__Day22__README.md]