gpt4 book ai didi

kubernetes - 如何使用 kubectl 正确访问多个 kubernetes 集群

转载 作者:行者123 更新时间:2023-12-03 21:09:53 25 4
gpt4 key购买 nike

我有两个集群,配置文件存储在 .kube .我正在导出 KUBECONFIG如下

export KUBECONFIG=/home/vagrant/.kube/config-cluster1:/home/vagrant/.kube/config-cluster2
检查上下文
kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* cluster-1 cluster-1 kubernetes-admin
cluster-2 cluster-2 kubernetes-admin
但是当我选择 cluster-2 作为我当前的上下文时,我收到一个错误
kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* cluster-1 cluster-1 kubernetes-admin
cluster-2 cluster-2 kubernetes-admin

kubectl config use-context cluster-2
Switched to context "cluster-2".


kubectl get pods -A
error: You must be logged in to the server (Unauthorized)

如果我只导出 cluster-2 的配置并尝试运行 kubectl它工作正常。
我的问题是我是正确导出配置文件还是应该做更多的事情。

最佳答案

您需要将 AUTHINFO 分开(配置文件中的 context.user)具有各自凭据的每个集群。
例如:

apiVersion: v1
clusters:
- cluster:
server: https://192.168.10.190:6443
name: cluster-1
- cluster:
server: https://192.168.99.101:8443
name: cluster-2
contexts:
- context:
cluster: cluster-1
user: kubernetes-admin-1
name: cluster-1
- context:
cluster: cluster-2
user: kubernetes-admin-2
name: cluster-2
kind: Config
preferences: {}
users:
- name: kubernetes-admin-1
user:
client-certificate: /home/user/.minikube/credential-for-cluster-1.crt
client-key: /home/user/.minikube/credential-for-cluster-1.key
- name: kubernetes-admin-2
user:
client-certificate: /home/user/.minikube/credential-for-cluster-2.crt
client-key: /home/user/.minikube/credential-for-cluster-2.key

您可以在以下文章中找到更多有用的提示:
Using different kubectl versions with multiple Kubernetes clusters :

When you are working with multiple Kubernetes clusters, it’s easy tomess up with contexts and run kubectl in the wrong cluster. Beyondthat, Kubernetes has restrictions for versioning mismatch between theclient (kubectl) and server (kubernetes master), so running commandsin the right context does not mean running the right client version.


为了克服这个:
  • 使用 asdf 管理多个kubectl版本
  • Set the KUBECONFIG env var 在多个 kubeconfig 之间改变文件
  • 使用 kube-ps1 跟踪您当前的上下文/命名空间
  • 使用 kubectx and kubens 在集群/命名空间之间快速更改
  • 使用别名将它们组合在一起

  • 我还推荐以下阅读:
  • Mastering the KUBECONFIG file作者:Ahmet Alp Balkan(Google 工程师)
  • How Zalando Manages 140+ Kubernetes Clusters作者:Henning Jacobs (Zalando Tech)
  • 关于kubernetes - 如何使用 kubectl 正确访问多个 kubernetes 集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64559766/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com