Reputation: 1691
Can I set the default namespace? That is:
$ kubectl get pods -n NAMESPACE
It saves me having to type it in each time especially when I'm on the one namespace for most of the day.
Upvotes: 73
Views: 28964
Reputation: 11554
I used to use the aliases shown below and set the variable N
to the namespace to use.
# Set N=-nNamespace if N isn't set then no harm, no namespace will be used
alias k='kubectl $N'
alias kg='kubectl get $N'
alias ka='kubectl apply $N'
alias kl='kubectl logs $N'
To switch to the my-apps
namespace; I'd use:
N=-nmy-apps
After this the commands:
kg pods
actually runs kubectl get -nmy-apps pods
.
NOTE: If the bash variable N
is not set, the command still works and runs as kubectl would by default.
To override the namespace set in N
variable simply add the --namespace
option like-nAnotherNamespace
and the last namespace defined will be used.
Of course to more permanently (in the current shell) switch, I'd simply set the N
variable as shown:
N=-nAnotherNamespace
kg pods
While the above works, I learned about kubens (bundled with kubectx, See github) which works more permanently because it updates my $HOME/.kube/config
file with a line that specifies the namespace to use for the current k8s cluster I'm using (dev in the example below)
contexts:
- context:
cluster: dev
namespace: AnotherNamesapce <<< THIS LINE IS ADDED by kubens
user: user1
name: dev
current-context: dev
But all kubeens does is what is already built into kubectl using:
kubectl config set-context --current --namespace=AnotherNamespace
So really a simple alias that is easier to type works just as well, so I picked ksn
for (kubectl set namespace).
function ksn(){
kubectl config set-context --current --namespace=$@
}
So now to switch context, I'm just using what is built into kubectl
!
To switch to the namespace AnotherNamespace
, I use:
ksn AnotherNamespace
Tada! The simplest "built in" solution.
For bash users, add the following to your $HOME/.bashrc
file.
function ksn(){
if [ "$1" = "" ]
then
kubectl config view -v6 2>&1 | grep 'Config loaded from file:' | sed -e 's/.*from file: /Config file:/'
echo Current context: $(kubectl config current-context)
echo Default namespace: $(kubectl config view --minify | grep namespace: | sed 's/.*namespace: *//')
elif [ "$1" = "--unset" ]
then
kubectl config set-context --current --namespace=
else
kubectl config set-context --current --namespace=$1
fi
}
This lets you set a namespace, see what your namespace is or remove a default namespace (using --unset). See three commands below:
# Set namespace
ksn AnotherNamespace
# Display the selected namespace
ksn
Config file: /home/user/.kube/config
Current context: dev
Default namespace: AnotherNamespace
# Unset/remove a default namespace
ksn --unset
See also: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/ for the command to view the current namespace:
Upvotes: 4
Reputation: 14031
Yes, you can set the namespace as per the docs like so:
$ kubectl config set-context --current --namespace=NAMESPACE
Alternatively, you can use kubectx for this.
Upvotes: 131
Reputation: 327
You can also use a temporary linux alias:
alias k='kubectl -n kube-system '
Then use it like
k get pods
That's it ;)
Upvotes: 21