kubernetes Kubectl命令抛出错误:无法连接到服务器:获取凭证:exec:退出状态2

4c8rllxm  于 2022-11-21  发布在  Kubernetes
关注(0)|答案(9)|浏览(294)

我正在进行EKS/Kubectl的实验室设置,在完成集群构建后,我运行以下命令:

> kubectl get node

And I get the following error:
Unable to connect to the server: getting credentials: exec: exit status 2

此外,我确信这是一个配置问题,

kubectl version
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help
aws: error: argument operation: Invalid choice, valid choices are:

create-cluster                           | delete-cluster                          
describe-cluster                         | describe-update                         
list-clusters                            | list-updates                            
update-cluster-config                    | update-cluster-version                  
update-kubeconfig                        | wait                                    
help                                    
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.1", GitCommit:"d224476cd0730baca2b6e357d144171ed74192d6", GitTreeState:"clean", BuildDate:"2020-01-14T21:04:32Z", GoVersion:"go1.13.5", Compiler:"gc", Platform:"darwin/amd64"}
Unable to connect to the server: getting credentials: exec: exit status 2

请建议故障排除的后续步骤。

i7uaboj4

i7uaboj41#

是否已准备好kubectl配置文件?
通常,我们将其放在~/.kube/config下,该文件包括集群端点、证书、上下文、管理员用户等。
进一步阅读本文档:https://docs.aws.amazon.com/eks/latest/userguide/create-kubeconfig.html

yvfmudvl

yvfmudvl2#

您可以检查您的~/.kube/config文件吗?
假设您已使用minikube启动本地集群,如果您的配置可用,则不会收到服务器错误。

示例配置文件

apiVersion: v1
    clusters:
    - cluster:
        certificate-authority: /Users/singhvi/.minikube/ca.crt
        server: https://127.0.0.1:32772
      name: minikube
    contexts:
    - context:
        cluster: minikube
        user: minikube
      name: minikube
    current-context: minikube
    kind: Config
    preferences: {}
    users:
    - name: minikube
      user:
        client-certificate: /Users/singhvi/.minikube/profiles/minikube/client.crt
        client-key: /Users/singhvi/.minikube/profiles/minikube/client.key
zphenhs4

zphenhs43#

您需要更新/重新创建您的本地kubeconfig。在我的例子中,我删除了整个~/.kube/config,并遵循了以下教程:
https://docs.aws.amazon.com/eks/latest/userguide/create-kubeconfig.html

1rhkuytd

1rhkuytd4#

对于我来说,运行kubectl get nodeskubectl cluster-info会出现以下错误。

Unable to connect to the server: getting credentials: exec: executable kubelogin not found

It looks like you are trying to use a client-go credential plugin that is not installed.

To learn more about this feature, consult the documentation available at:
      https://kubernetes.io/docs/reference/access-authn-authz/authentication/#client-go-credential-plugins

我执行了以下操作来解决此问题。
1.删除了~/.kube/中的所有内容。在我的例子中,它是一个windows机器,所以它的C:\Users\nis. kube。这里nis是我登录的用户名。
1.按如下所示运行get credentials命令。

az aks get-credentials --resource-group terraform-aks-dev --name terraform-aks-dev-aks-cluster --admin

最后请注意--admin。如果没有它,它会给我同样的错误。
现在以上两个命令都起作用了。
参考:https://blog.baeke.info/2021/06/03/a-quick-look-at-azure-kubelogin/

8ehkhllq

8ehkhllq6#

我也遇到了同样的问题,问题是在我的.aws/credentials文件中有多个用户,而在EKS集群上拥有权限的用户(admin_test)不是默认用户。因此,在我的示例中,我使用环境变量在CLI中将“admin_test”用户设置为默认用户:

export $AWS_PROFILE='admin_test'

之后,我使用以下命令检查默认用户:

aws sts get-caller-identity

最后,我能够使用 kubectl get nodes 命令获取节点。
参考:https://docs.aws.amazon.com/eks/latest/userguide/create-kubeconfig.html

tzcvj98z

tzcvj98z7#

在EKS中,可以使用以下命令检索kubectl凭据:

% aws eks update-kubeconfig --name cluster_name
Updated context arn:aws:eks:eu-west-1:xxx:cluster/cluster_name in /Users/theofpa/.kube/config

您可以使用以下命令检索群集名称:

% aws eks list-clusters
{
    "clusters": [
        "cluster_name"
    ]
}
cgh8pdjw

cgh8pdjw8#

删除和添加~/.aws/credentials文件为我解决了这个问题。

rm ~/.aws/credentials
touch ~/.aws/credentials
1wnzp6jl

1wnzp6jl9#

请删除存在于中该高速缓存文件夹

~/.aws/客户端/缓存

相关问题