如何使用 .conf 文件中的集群信息配置 kubectl?

How to configure kubectl with cluster information from a .conf file?

我有一个 admin.conf 文件,其中包含有关集群的信息,因此以下命令可以正常工作:

kubectl --kubeconfig ./admin.conf get nodes

我如何 config kubectl 在一个命令中使用此文件中的集群、用户和身份验证作为默认值?我只看到单独的 set-cluster、set-credentials、set-context、use-context 等。当我只是 运行:

时,我想获得相同的输出
kubectl get nodes

kubectl 使用 ~/.kube/config 作为默认配置文件。所以你可以把你的 admin.conf 复制到它上面。

这里有关于如何配置kubectl的官方文档

http://kubernetes.io/docs/user-guide/kubeconfig-file/

你有几个选项,专门针对这个问题,你可以将你的admin.conf复制到~/.kube/config

我发现的最好方法是使用环境变量:

export KUBECONFIG=/path/to/admin.conf

我只是通过 .bashrc 将 kubectl 命令别名为我的开发和生产环境的单独命令

alias k8='kubectl'
alias k8prd='kubectl --kubeconfig ~/.kube/config_prd.conf'

我更喜欢这种方法,因为它需要我为每个命令定义环境。而使用环境变量可能会导致您运行错误环境中的命令

我将所有集群配置命名为 .kubeconfig,它位于项目目录中。

然后在 .bashrc.bash_profile 我有以下出口:

export KUBECONFIG=.kubeconfig:$HOME/.kube/config

这样当我在项目目录中时 kubectl 将加载本地 .kubeconfig。 希望有帮助

Before answers have been very solid and informative, I will try to add my 2 cents here

配置 kubeconfig 文件知道其优先级

如果您正在使用 kubectl,这是在确定使用哪个 kubeconfig 文件时生效的首选项。

  1. 使用 --kubeconfig 标志,如果指定的话
  2. 使用 KUBECONFIG 环境变量,如果指定的话
  3. 使用$HOME/.kube/config文件

有了这个,您可以轻松地覆盖您根据 kubectl 命令使用的 kubeconfig 文件:

#
# using --kubeconfig flag
#
kubectl get pods --kubeconfig=file1
kubectl get pods --kubeconfig=file2

#
# or 
# using `KUBECONFIG` environment variable
#
KUBECONFIG=file1 kubectl get pods
KUBECONFIG=file2 kubectl get pods

#
# or 
# merging your kubeconfig file w/ $HOME/.kube/config (w/ cp backup)
#
cp $HOME/.kube/config $HOME/.kube/config.backup.$(date +%Y-%m-%d.%H:%M:%S)
KUBECONFIG= $HOME/.kube/config:file2:file3 kubectl config view --merge --flatten > \
~/.kube/merged_kubeconfig && mv ~/.kube/merged_kubeconfig ~/.kube/config
kubectl get pods --context=cluster-1
kubectl get pods --context=cluster-2

注意: --minify 标志允许我们仅提取有关该上下文的信息,而 --flatten 标志允许我们保持凭证未编辑。

以你为例

kubectl get pods --kubeconfig=/path/to/admin.conf

#
# or:
#
KUBECONFIG=/path/to/admin.conf kubectl get pods

#
# or: 
#
cp $HOME/.kube/config $HOME/.kube/config.backup.$(date)
KUBECONFIG= $HOME/.kube/config:/path/to/admin.conf kubectl config view --merge --flatten > \
~/.kube/merged_kubeconfig && mv ~/.kube/merged_kubeconfig ~/.kube/config
kubectl get pods --context=cluster-1
kubectl get pods --context=cluster-2

尽管此优先级列表未在文档中正式指定,但它是 codified here. If you’re developing client tools for Kubernetes, you should consider using cli-runtime 库,它将为您的程序带来标准 --kubeconfig 标志和 $KUBECONFIG 检测。

参考文章: https://ahmet.im/blog/mastering-kubeconfig/

因为目前没有内置的kubectl config merge命令(遵循this)你可以将这个功能添加到你的.bashrc(或.zshrc):

function kmerge() {
  if [ $# -eq 0 ]
   then
     echo "Please pass the location of the kubeconfig you wish to merge"
  fi
  KUBECONFIG=~/.kube/config: kubectl config view --flatten > ~/.kube/mergedkub && mv ~/.kube/mergedkub ~/.kube/config
}

然后你可以运行来自终端:

kmerge /path/to/admin.conf

配置文件将合并到~/.kube/config.

您现在可以切换到新上下文:

kubectl config use-context <new-context-name>

或者,如果您使用 kubectx(推荐),您可以 运行:kubectx <new-context-name>


kmerge 函数基于@MichaelSp 在此post 的回答)。

Kubernetes 在$KUBECONFIG

中保留搜索配置文件的路径

如果您想在现有 KUBECONFIG 之上添加 one more config path 而不覆盖它(并保持 ~/.kube/config 作为默认搜索路径)。

每次要将 conf 文件添加到 KUBECONFIG 路径时,只需 运行 以下内容

export KUBECONFIG=${KUBECONFIG:-~/.kube/config}:/path/to/admin.conf

您可以通过列出可用的上下文来检查它是否有效

kubectl config get-contexts

然后select您要使用的那个

kubectl config use-context <context-name>

正确管理您的配置文件,将其放在您的配置文件中,获取 .profile / .bash_profile

for kconfig in $HOME/.kube/config $(find $HOME/.kube/ -iname "*.config")
do
         if [ -f "$kconfig" ];then
                 export KUBECONFIG=$KUBECONFIG:$kconfig
         fi
 done

从 kubectl 切换上下文