Kubernetes - How to Set Namespace So You Do Not Need to Mention it Again and Again in Kubectl Commands.

May 05, 2022


In some of the cases, we need to specify namespace name along with kubectl commands.

Example of get pods command with namespace:

kubectl --namespace my-namespace get pods

Its very tedius to specify these two strings every time you are running kubectl commands. Especially, when you have multiple namespaces. Its frustrating to copy-paste them each time.

How to Set Namespace With the Context

Assumming, the name of your namespace is my-namespace.

Command to set namespace with a context:

kubectl config set-context --current --namespace my-namespace

Now, if you do: kubectl config get-contexts

You will see a namespace name along with the context name, something like:

* cluster-name cluster-name xyz my-namespace

Run Commands without Specifying Namespace

Now, you can run normal kubectl commands without mentioning namespace.

kubectl config get-contexts
kubectl get pods
kubectl apply -f <path to yaml>

Thanks for reading…

Similar Posts

Latest Posts