EKS 上的两个集群,如何在它们之间切换

5 amazon-web-services kubernetes kubectl amazon-eks

我不确定发生了什么,这就是我问这个问题的原因。当我运行此命令时:

kubectl config get-clusters
Run Code Online (Sandbox Code Playgroud)

我得到:

arn:aws:eks:us-west-2:91xxxxx371:cluster/eks-cluster-1
arn:aws:eks:us-west-2:91xxxxx371:cluster/eks1
Run Code Online (Sandbox Code Playgroud)

然后我运行:

kubectl config current-context
Run Code Online (Sandbox Code Playgroud)

我得到:

arn:aws:eks:us-west-2:91xxxxx371:cluster/eks-cluster-1
Run Code Online (Sandbox Code Playgroud)

如果我运行kubectl get pods,我会得到预期的输出。但是如何切换到其他集群/上下文?集群和上下文有什么区别?我无法弄清楚这些命令有何不同:

当我运行它们时,我仍然从错误的集群中获取 pod:

root@4c2ab870baaf:/# kubectl config set-context arn:aws:eks:us-west-2:913617820371:cluster/eks1

Context "arn:aws:eks:us-west-2:913617820371:cluster/eks1" modified.

root@4c2ab870baaf:/#
root@4c2ab870baaf:/# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
apache-spike-579598949b-5bjjs   1/1     Running   0          14d
apache-spike-579598949b-957gv   1/1     Running   0          14d
apache-spike-579598949b-k49hf   1/1     Running   0          14d

root@4c2ab870baaf:/# kubectl config set-cluster arn:aws:eks:us-west-2:91xxxxxx371:cluster/eks1

Cluster "arn:aws:eks:us-west-2:91xxxxx371:cluster/eks1" set.

root@4c2ab870baaf:/# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
apache-spike-579598949b-5bjjs   1/1     Running   0          14d
apache-spike-579598949b-957gv   1/1     Running   0          14d
apache-spike-579598949b-k49hf   1/1     Running   0          14d
Run Code Online (Sandbox Code Playgroud)

所以我真的不知道如何在集群或上下文之间正确切换以及在这样做时切换身份验证例程。

例如:

contexts:
- context:
    cluster: arn:aws:eks:us-west-2:91xxxxx371:cluster/ignitecluster
    user: arn:aws:eks:us-west-2:91xxxx371:cluster/ignitecluster
  name: arn:aws:eks:us-west-2:91xxxxx371:cluster/ignitecluster
- context:
    cluster: arn:aws:eks:us-west-2:91xxxx371:cluster/teros-eks-cluster
    user: arn:aws:eks:us-west-2:91xxxxx371:cluster/teros-eks-cluster
  name: arn:aws:eks:us-west-2:91xxxxx371:cluster/teros-eks-cluster
Run Code Online (Sandbox Code Playgroud)

A_S*_*Suh 13

澄清set-context和之间的区别use-context

上下文是一组访问参数。每个上下文都包含一个 Kubernetes 集群、一个用户和一个命名空间。因此,当您执行set-context 时,您只需将上下文详细信息添加到您的配置文件中~/.kube/config,但它不会将您切换到该上下文,而use-context实际上会。

因此,正如 Vasily 提到的,为了在集群之间切换运行

kubectl config use-context <CONTEXT-NAME>
Run Code Online (Sandbox Code Playgroud)

此外,如果您运行,kubectl config get-contexts您将看到带有当前上下文指示的上下文列表。


Vas*_*pov 3

使用

kubectl config use-context arn:aws:eks:us-west-2:91xxxxx371:cluster/eks-cluster-1
Run Code Online (Sandbox Code Playgroud)

kubectl config use-context arn:aws:eks:us-west-2:91xxxxx371:cluster/eks
Run Code Online (Sandbox Code Playgroud)