61

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.

Isma
  • 14,604
  • 5
  • 37
  • 51
mac
  • 1,479
  • 3
  • 11
  • 21
  • 1
    Are you looking for environment variable? [Create env. variable](https://askubuntu.com/questions/58814/how-do-i-add-environment-variables) – alberand Feb 27 '19 at 10:44
  • Kubectx and kubens both provides great flexibility together – Omer Sen Jan 11 '20 at 23:08
  • 'default' Namespace is literally a namespace and what you are basically asking for is how to set "current" namespace to save typing efforts everytime, for my bash session or inside kubectlConfig, which has been already answered. – Karan Kaw Jan 18 '23 at 04:27

3 Answers3

111

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.

Morten Siebuhr
  • 6,068
  • 4
  • 31
  • 43
Michael Hausenblas
  • 13,162
  • 4
  • 52
  • 66
17

You can also use a temporary linux alias:

alias k='kubectl -n kube-system '

Then use it like

k get pods

That's it ;)

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
1

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.

Summary

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:

PatS
  • 8,833
  • 12
  • 57
  • 100