简体   繁体   English

动态设置 KUBECONFIG 环境变量

[英]Set KUBECONFIG environment variable dynamically

I am quite new to Bash and working on multiple kubernetes cluster.我对 Bash 还很陌生,并且在多个 kubernetes 集群上工作。 I wanted some kind of utility wherein I can set my KUBECONFIG variable dynamically as I work on multiple clusters.我想要某种实用程序,当我在多个集群上工作时,我可以在其中动态设置我的 KUBECONFIG 变量。 My kubeconfig file lies in multiple folders.我的 kubeconfig 文件位于多个文件夹中。 What I want is to find all the kubeconfig files , get the paths and concatenate the paths with colon , set it to KUBECONFIG variable and export it in bashrc file.我想要的是找到所有 kubeconfig 文件,获取路径并将路径与冒号连接,将其设置为 KUBECONFIG 变量并将其导出到 bashrc 文件中。

I know pieces of command but not able to frame full code.我知道一些命令,但无法构建完整的代码。

find /Users/anandabhishe/gitlab/ -name kubeconfig.yaml -exec echo {} \; 

I want to concatenate the output of find command and set my KUBECONFIG.我想连接 find 命令的输出并设置我的 KUBECONFIG。 like this "像这样 ”

export KUBECONFIG=/Users/anandabhishe/gitlab/work2/kubeconfigs/scdc1-staging-hrwork-dev/kubeconfig.yaml:/Users/anandabhishe/gitlab/work2/kubeconfigs/scdc2-prod-hrwork-uat/kubeconfig.yaml:/Users/anandabhishe/gitlab/work2/kubeconfigs/scdc2-prod-hrwork/kubeconfig.yaml " export KUBECONFIG=/Users/anandabhishe/gitlab/work2/kubeconfigs/scdc1-staging-hrwork-dev/kubeconfig.yaml:/Users/anandabhishe/gitlab/work2/kubeconfigs/scdc2-prod-hrwork-uat/kubeconfig.yaml:/Users/anandabhishe/gitlab/work2/kubeconfigs/scdc2-prod-hrwork/kubeconfig.yaml "

你可以尝试这样的事情:

export KUBECONFIG=$(for i in $(find /Users/anandabhishe/gitlab/ -iname 'kubeconfig.yaml') ; do echo -n ":$i"; done | cut -c 2-)

you can merge ( https://stackoverflow.com/a/63177022/5525824 ) multiple kubeconfig files in single file set it in .kube folder and list Kubernets cluster using您可以在单个文件中合并( https://stackoverflow.com/a/63177022/5525824 )多个 kubeconfig 文件,将其设置在.kube文件夹中,并使用列出 Kubernets 集群

kubectl config get-contexts

change default Kubernetes cluster config by通过以下方式更改默认的 Kubernetes 集群配置

kubectl config use-context

the above command will change default Kubernetes cluster also for helm as well.上面的命令也会为helm更改默认的 Kubernetes 集群。

For more details, you can follow: https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/更多详情可以关注: https : //kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/

Update :更新

you can do something like你可以做类似的事情

find ./wotnot -name kubeconfig.yaml -print0

output would be something like输出将类似于

/Users/anandabhishe/gitlab/work2/kubeconfigs/scdc1-staging-hrwork-dev/kubeconfig.yaml./Users/anandabhishe/gitlab/work2/kubeconfigs/scdc2-prod-hrwork-uat/kubeconfig.yaml./Users/anandabhishe/gitlab/work2/kubeconfigs/scdc2-prod-hrwork/kubeconfig.yaml

save output in one variable将输出保存在一个变量中

replace path ./ with : using sed将路径./替换为:使用sed

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

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