简体   繁体   English

我可以在 Kube.netes 中设置默认命名空间吗?

[英]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.或者,您可以为此使用kubectx

You can also use a temporary linux alias:您还可以使用临时 linux 别名:

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.我曾经使用如下所示的别名并将变量N 设置为要使用的名称空间。

# 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;切换到my-apps命名空间; I'd use:我会用:

N=-nmy-apps

After this the commands:在此之后的命令:

kg pods

actually runs kubectl get -nmy-apps pods .实际上运行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.注意:如果未设置 bash 变量N ,该命令仍然有效,并且默认情况下会像 kubectl 一样运行。

To override the namespace set in N variable simply add the --namespace option like -nAnotherNamespace and the last namespace defined will be used.要覆盖N变量中设置的命名空间,只需添加--namespace选项,如-nAnotherNamespace ,最后定义的命名空间将被使用。

Of course to more permanently (in the current shell) switch, I'd simply set the N variable as shown:当然,为了更永久地(在当前 shell 中)切换,我只是简单地设置N变量,如下所示:

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)虽然上面的工作有效,但我了解了kubens (与kubectx捆绑在一起,请参阅github ),它可以更永久地工作,因为它更新了我的$HOME/.kube/config文件,其中一行指定了用于当前 k8s 集群的命名空间使用(下面示例中的 dev)

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:但是kubeens所做的一切都是已经内置到 kubectl 中的:

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).所以实际上一个更容易输入的简单别名也能正常工作,所以我选择了ksn作为 (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 !所以现在要切换上下文,我只是使用kubectl内置的东西! To switch to the namespace AnotherNamespace , I use:要切换到命名空间AnotherNamespace ,我使用:

ksn AnotherNamespace

Tada.多田。 The simplest "built in" solution.最简单的“内置”解决方案。

Summary概括

For bash users, add the following to your $HOME/.bashrc file.对于bash用户,将以下内容添加到$HOME/.bashrc文件中。

    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).这使您可以设置命名空间,查看您的命名空间是什么或删除默认命名空间(使用 --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:另请参阅: https://kube.netes.io/docs/concepts/overview/working-with-objects/namespaces/查看当前命名空间的命令:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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