繁体   English   中英

无法从 client-go 连接到 kubectl - /serviceaccount/token: no such file

[英]Fail to connect to kubectl from client-go - /serviceaccount/token: no such file

我正在使用 golang lib client-go连接到正在运行的本地 kubrenets。 首先,我从示例中获取了代码: out-of-cluster-client-configuration

运行这样的代码: $ KUBERNETES_SERVICE_HOST=localhost KUBERNETES_SERVICE_PORT=6443 go run ./main.go导致以下错误:

panic: open /var/run/secrets/kubernetes.io/serviceaccount/token: no such file or directory

goroutine 1 [running]:

/var/run/secrets/kubernetes.io/serviceaccount/

我不太确定我缺少哪一部分配置。 我研究了以下链接:

但没有运气。 我想我需要让客户端知道要使用哪个令牌/服务帐户,或者以每个人都可以连接到其 api 的方式配置 kubectl。

这是我的 kubectl 的状态,尽管一些命令会产生结果:

$ kubectl 配置视图

apiVersion: v1
clusters:
- cluster:
    insecure-skip-tls-verify: true
    server: https://localhost:6443
  name: docker-for-desktop-cluster
contexts:
- context:
    cluster: docker-for-desktop-cluster
    user: docker-for-desktop
  name: docker-for-desktop
current-context: docker-for-desktop
kind: Config
preferences: {}
users:
- name: docker-for-desktop
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

$ kubectl get serviceAccounts

NAME        SECRETS   AGE
default     1         3d
test-user   1         1d

$ kubectl describe serviceaccount test-user

Name:                test-user
Namespace:           default
Labels:              <none>
Annotations:         <none>
Image pull secrets:  <none>
Mountable secrets:   test-user-token-hxcsk
Tokens:              test-user-token-hxcsk
Events:              <none>

$ kubectl get secret test-user-token-hxcsk -o yaml

apiVersion: v1
data:
  ca.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0......=
  namespace: ZGVmYXVsdA==
  token: ZXlKaGJHY2lPaUpTVXpJMU5pSX......=
kind: Secret
metadata:
  annotations:
    kubernetes.io/service-account.name: test-user
    kubernetes.io/service-account.uid: 984b359a-6bd3-11e8-8600-XXXXXXX
  creationTimestamp: 2018-06-09T10:55:17Z
  name: test-user-token-hxcsk
  namespace: default
  resourceVersion: "110618"
  selfLink: /api/v1/namespaces/default/secrets/test-user-token-hxcsk
  uid: 98550de5-6bd3-11e8-8600-XXXXXX
type: kubernetes.io/service-account-token

这个答案可能有点过时,但我会尝试为未来遇到相同/类似问题的读者提供更多观点/基线。

TL; 博士

以下错误:

panic: open /var/run/secrets/kubernetes.io/serviceaccount/token: no such file or directory

很可能与使用in-cluster-client-configuration/var/run/secrets/kubernetes.io/serviceaccount位置缺少token有关。 此外,这可能与in-cluster-client-configuration外使用in-cluster-client-configuration代码的事实有关(例如,直接在笔记本电脑或纯 Docker 容器中运行此代码)。

您可以检查以下命令以进一步解决您的问题(假设此代码在Pod内运行):

  • $ kubectl get serviceaccount X -o yaml :
    • 寻找: automountServiceAccountToken: false
  • $ kubectl describe pod XYZ
    • 查找:安装Secret containers.mounts volumeMountsvolumeMounts

引用官方文档:

在集群内部进行身份验证

此示例向您展示如何使用 client-go 配置客户端以从在 Kubernetes 集群内运行的应用程序向 Kubernetes API 进行身份验证

当使用rest.InClusterConfig()时,client-go 使用挂载在 Pod 内/var/run/secrets/kubernetes.io/serviceaccount路径的服务帐户令牌

-- Github.com:Kubernetes:client-go:示例:在集群客户端配置中

如果您使用~/.kube/config对 Kubernetes API 进行身份验证,则应该使用out-of-cluster-client-configuration


附加信息:

当代码在Pod内运行时,我添加了更多信息以提供有关进一步故障排除的更多参考。

  • automountServiceAccountToken: false

在 1.6+ 版本中,您可以通过在服务帐户上设置 automountServiceAccountToken: false 来选择退出服务帐户的自动挂载 API 凭据:

 apiVersion: v1 kind: ServiceAccount metadata: name: go-serviceaccount automountServiceAccountToken: false

在 1.6+ 版本中,您还可以选择不为特定 pod 自动挂载 API 凭证:

 apiVersion: v1 kind: Pod metadata: name: sdk spec: serviceAccountName: go-serviceaccount automountServiceAccountToken: false

-- Kubernetes.io: Docs: Tasks: 配置 pod 容器: 配置服务帐号

  • $ kubectl describe pod XYZ

挂载servicAccount令牌后, Pod定义应如下所示:

<-- OMITTED --> 
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from go-serviceaccount-token-4rst8 (ro)
<-- OMITTED --> 
Volumes:
  go-serviceaccount-token-4rst8:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  go-serviceaccount-token-4rst8
    Optional:    false

如果不是:

<-- OMITTED --> 
    Mounts:         <none>
<-- OMITTED --> 
Volumes:            <none>

其他资源:

简单说一下,以防它可以帮助您进一步调试:问题与 Go 或您的代码无关,而与 Kubernetes 节点无法从 Kubernetes 主节点获取令牌有关。

kubectl config viewclusters.cluster.server应该指向节点可以访问的 IP 地址。
它需要访问 CA,即主服务器,以提供该令牌,我猜它因为这个原因而失败。
kubectl describe <your_pod_name>可能会告诉您获取令牌的问题是什么。

由于您认为问题出在 Go/您的代码上并专注于此,因此您忽略了提供有关 Kubernetes 设置的更多信息,这使我更难为您提供比我上面的猜测更好的答案;-)

但我希望它有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM