背景:当我在EKS上有多个集群的时候,而我只想在一个EC2-CONSOLE的跳板机进行控制。
注意:确保 EC2 实例具有管理权限
一、为每个集群使用不同的配置文件,可以使用 --kubeconfig
参数指定输出文件的路径:
aws eks update-kubeconfig --name eks-a --region us-west-2 --kubeconfig ~/.kube/eks-a-config
aws eks update-kubeconfig --name eks-b --region us-west-2 --kubeconfig ~/.kube/eks-b-config
aws eks update-kubeconfig --name eks-c --region us-west-2 --kubeconfig ~/.kube/eks-c-config
注意:如果EC2上面绑定了多个AWS凭证账号,比如:
AWS CLI 使用一系列配置文件(通常是 ~/.aws/credentials
和 ~/.aws/config
)来管理凭证和配置。每个配置文件可以包含多个配置和凭证集。
[profile profile1]
region = us-west-2
[profile profile2]
region = us-west-2
在 aws eks update-kubeconfig
命令中,您可以使用 --profile
参数指定使用哪个配置文件。例如,如果 profile1
对应 eks-a
集群的凭证:
aws eks update-kubeconfig --name eks-a --region us-west-2 --kubeconfig ~/.kube/eks-a-config --profile profile1
aws eks update-kubeconfig --name eks-b --region us-west-2 --kubeconfig ~/.aws/eks-b-config --profile profile2
aws eks update-kubeconfig --name eks-c --region us-west-2 --kubeconfig ~/.aws/eks-c-config --profile profile3
二、添加以下别名定义到您的配置文件中:
alias kc='kubectl --kubeconfig ~/.kube/eks-c-config'
alias kb='kubectl --kubeconfig ~/.kube/eks-b-config'
alias ka='kubectl --kubeconfig ~/.kube/eks-a-config'
三、为了使这些更改生效,您需要重新加载配置文件。在终端中运行以下命令:
source ~/.bashrc # 如果您使用的是 bash
# 或者
source ~/.zshrc # 如果您使用的是 zsh
四、一旦别名设置完成,您就可以使用这些别名来快速切换到不同的集群了。例如:
kc get nodes # 使用 eks-c 集群的配置文件
kb get nodes # 使用 eks-b 集群的配置文件
ka get nodes # 使用 eks-a 集群的配置文件