简体   繁体   中英

Can I set a default namespace in Kubernetes?

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.

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.

You can also use a temporary linux alias:

alias k='kubectl -n kube-system '

Then use it like

k get pods

That's it ;)

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://kube.netes.io/docs/concepts/overview/working-with-objects/namespaces/ for the command to view the current namespace:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM