The kubectl config set-context --current --namespace <namespace>
command will set the current namespace.
However, I like to use a little bash function (kn
) which updates the namespace and also displays all the available namespaces on the Kubernetes cluster:
function kn () {
kubectl get ns ; echo
if [[ "$#" -eq 1 ]]; then
kubectl config set-context --current --namespace $1 ; echo
fi
echo "Current namespace [ $(kubectl config view --minify | grep namespace | cut -d " " -f6) ]"
}
This can live inside your ~/.bash_profile
or ~/.zprofile
(Z Shell on Macs) file.
Setting the namespace as follows:
$ kn postgres
NAME STATUS AGE
default Active 27d
kube-node-lease Active 27d
kube-public Active 27d
kube-system Active 27d
postgres Active 2d2h
Context "docker-desktop" modified.
Current namespace [ postgres ]
The Context "docker-desktop" modified.
indicates that the current namespace was updated.
The function also works with no parameters and will display all the namespaces + current namespace.
$ kn
NAME STATUS AGE
default Active 27d
kube-node-lease Active 27d
kube-public Active 27d
kube-system Active 27d
postgres Active 2d2h
Current namespace [ postgres ]
kubectl config set-context --help
might be you are looking for – Different