簡體   English   中英

Kubectl 命令拋出錯誤:無法連接到服務器:獲取憑據:執行:退出狀態 2

[英]Kubectl command throwing error: Unable to connect to the server: getting credentials: exec: exit status 2

我正在進行 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

請告知故障排除的后續步驟。

請刪除緩存文件夾中存在的文件夾

~/.aws/cli/cache

你准備好kubectl配置文件了嗎?

通常我們將它放在~/.kube/config ,該文件包括集群端點、證書、上下文、管理員用戶等。

此外,請閱讀此文檔: https : //docs.aws.amazon.com/eks/latest/userguide/create-kubeconfig.html

你能檢查一下你的 ~/.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

您需要更新/重新創建本地 kubeconfig。 就我而言,我刪除了整個~/.kube/config並按照本教程進行操作:

https://docs.aws.amazon.com/eks/latest/userguide/create-kubeconfig.html

對我來說,運行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

kubectl 獲取節點給出錯誤

我做了以下事情來解決這個問題。

  1. 刪除了~/.kube/中的所有內容。 在我的例子中,它是一台 windows 機器,所以它的 C:\Users\nis.kube。 這里 nis 是我登錄的用戶名。

  2. 按如下方式運行獲取憑據命令。

    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/

我有同樣的問題,問題是在 my.aws/credentials 文件中有多個用戶,並且對 EKS (admin_test) 集群具有權限的用戶不是默認用戶。 因此,就我而言,我使用環境變量將“admin_test”用戶設置為 CLI 中的默認用戶:

export $AWS_PROFILE='admin_test'

之后,我使用以下命令檢查了默認用戶:

aws sts get-caller-identity

最后,我能夠使用kubectl get nodes命令獲取節點。

參考: https://docs.aws.amazon.com/eks/latest/userguide/create-kubeconfig.html

在 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"
    ]
}

就我而言,因為我使用的是 azure(不是 aws),所以我必須安裝“kubelogin”來解決問題。

“kubelogin”是一個實現 azure 身份驗證的 client-go credential (exec) 插件。 這個插件提供了 kubectl 中沒有的特性。 它在 kubectl v1.11+ 上受支持

我有同樣的錯誤並通過將我的 awscli 升級到最新版本解決了它。

刪除和添加~/.aws/credentials文件為我解決了這個問題。

rm ~/.aws/credentials
touch ~/.aws/credentials

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM