Two clusters on EKS, how to switch between them
Asked Answered
P

5

11

I am not exactly sure what's going on which is why I am asking this question. When I run this command:

kubectl config get-clusters

I get:

arn:aws:eks:us-west-2:91xxxxx371:cluster/eks-cluster-1
arn:aws:eks:us-west-2:91xxxxx371:cluster/eks1

then I run:

kubectl config current-context

and I get:

arn:aws:eks:us-west-2:91xxxxx371:cluster/eks-cluster-1

and if I run kubectl get pods, I get the expected output. But how do I switch to the other cluster/context? what's the difference between the cluster and context? I can't figure out how these commands differ:

When I run them, I still get the pods from the wrong cluster:

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

so I really don't know how to properly switch between clusters or contexts and also switch the auth routine when doing so.

For example:

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
Parisparish answered 22/5, 2019 at 3:31 Comment(0)
F
19

To clarify on the difference between set-context and use-context

A context is a group of access parameters. Each context contains a Kubernetes cluster, a user, and a namespace. So when you do set-context, you just adding context details to your configuration file ~/.kube/config, but it doesn't switch you to that context, while use-context actually does.

Thus, as Vasily mentioned, in order to switch between clusters run

kubectl config use-context <CONTEXT-NAME>

Also, if you run kubectl config get-contexts you will see list of contexts with indication of the current one.

Foreconscious answered 22/5, 2019 at 9:42 Comment(1)
in addition, it is possible to rename context: kubectl config rename-context <a big old name from get-contexts> <a new short name that makes sense>Columbus
S
3

Use

kubectl config use-context arn:aws:eks:us-west-2:91xxxxx371:cluster/eks-cluster-1

and

kubectl config use-context arn:aws:eks:us-west-2:91xxxxx371:cluster/eks
Spontaneity answered 22/5, 2019 at 4:55 Comment(0)
T
3

Consider using kubectx for managing your contexts.


Usage

View all contexts (the current context is bolded):

$kubectx
    arn:aws:eks:us-east-1:12234567:cluster/eks_app
  ->gke_my_second_cluster
    my-rnd
    my-prod

Switch to other context:

$ kubectx my-rnd
Switched to context "my-rnd".

Bonus:
In the same link - check also the kubens tool.

Thunell answered 9/3, 2021 at 15:23 Comment(0)
F
1

This is the best command to switch between different EKS clusters.

I use it every day.

aws eks update-kubeconfig --name example

Documentation:

https://docs.aws.amazon.com/cli/latest/reference/eks/update-kubeconfig.html

Francium answered 13/9, 2021 at 23:41 Comment(0)
L
0

You may try Lens - https://k8slens.dev/ which would help managing multiple EKS clusters easier through its GUI

Lithoid answered 1/3 at 5:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.