【问题标题】:Unable to access Kubernetes cluster using the go client when CLOUDSDK_CONFIG is set设置 CLOUDSDK_CONFIG 时无法使用 go 客户端访问 Kubernetes 集群
【发布时间】:2019-03-13 05:28:04
【问题描述】:

我在 GKE 上有一个 Kubernetes 集群。即使在正确设置了 KUBECONFIG="/tmp/kubeconfigxvz" 之后,当我执行 kubectl get pods 时,命令也会失败并出现以下错误

bash-4.3# kubectl get pods
Unable to connect to the server: error executing access token command 
"/google-cloud-sdk/bin/gcloud config config-helper --format=json": err=exit 
status 1 output= stderr=ERROR: (gcloud.config.config-helper) You do not 
currently have an active account selected.
Please run:

  $ gcloud auth login

to obtain new credentials, or if you have already logged in with a
different account:

  $ gcloud config set account ACCOUNT

to select an already authenticated account to use.

当我设置CLOUDSDK_CONFIG=/tmp/customdir 时,命令开始工作。

如何使用 go 客户端实现相同的功能?

=== 更新 ===

创建 go 客户端时,我将正确的文件指针传递给此函数 clientcmd.BuildConfigFromFlags("", *tmpKubeConfigFile) 其中tmpKubeConfigFile 指向/tmp/kubeconfigxvz。 但我认为这还不够,go-client 还需要来自CLOUDSDK_CONFIG 目录的更多信息,我认为它需要会话信息或凭据之类的。

创建go-client时是否也可以传递这个CLOUDSDK_CONFIG?

BuildConfigFromFlags 接收指向 kubeconfig 文件的指针并返回一个 config 对象,该对象可以传递给创建客户端的 kubernetes.NewForConfig(config)。是否有可能或是否存在类似的功能来传递 CLOUDSDK_CONFIG 并返回 go-client 或创建配置?

【问题讨论】:

    标签: kubernetes kubernetes-go-client


    【解决方案1】:

    您基本上需要创建一个~/.kube/config 文件来直接访问您的GKE 集群。

    您可以看到in this go client example 正在从~/.kube/config 获取配置

    GKE 配置如下所示:

    apiVersion: v1
    clusters:
    - cluster:
        certificate-authority-data: [REDACTED]
        server: https://x.x.x.x
      name: gke_project_us-central1-a_your-first-cluster-1
    contexts:
    - context:
        cluster: gke_project_us-central1-a_your-first-cluster-1
        user: gke_project_us-central1-a_your-first-cluster-1
      name: gke_project_us-central1-a_your-first-cluster-1
    current-context: gke_project_us-central1-a_your-first-cluster-1
    kind: Config
    preferences: {}
    users:
    - name: gke_project_us-central1-a_your-first-cluster-1
      user:
        auth-provider:
          config:
            cmd-args: config config-helper --format=json
            cmd-path: /google/google-cloud-sdk/bin/gcloud
            expiry-key: '{.credential.token_expiry}'
            token-key: '{.credential.access_token}'
          name: gcp
    

    您必须使用以下内容更改用户部分:

    - name: myuser
      user:
         token: [REDACTED]
    

    该用户是一个带有令牌的服务帐户,如果您想添加该用户来管理集群中的所有内容,您可以将ClusterRoleBind它添加到admin角色。

    有关RBACServiceAccountsRolesClusterRolesUsers 的更多信息,您可以查看here

    顺便说一句,不幸的是,GKE 没有让您访问主节点,因此您无法创建证书身份验证,因为您无权访问 CA.key 文件。

    【讨论】:

      猜你喜欢
      • 2019-08-15
      • 2016-01-15
      • 2020-02-03
      • 2020-04-29
      • 2023-03-09
      • 2020-06-01
      • 2018-07-01
      • 2018-07-02
      • 2021-05-03
      相关资源
      最近更新 更多