【问题标题】:Pods in GKE return "error looking up service account" - how to correctly use a GCP service account in GKE?GKE 中的 Pod 返回“错误查找服务帐户” - 如何在 GKE 中正确使用 GCP 服务帐户?
【发布时间】:2021-12-14 21:06:30
【问题描述】:

我已经创建了一个 GKE 服务帐号。

我一直在尝试在 GKE 中使用它,但出现错误:

pods "servicepod" is forbidden: error looking up service account service/serviceaccount: serviceaccount "serviceaccount" not found

我已按照documentation 中的设置指南进行操作。

1.创建名为“serviceaccount”的 GCP 服务帐号

2.我创建了 JSON 密钥并将其下载为 key.json。

3.kubectl create secret generic serviceaccountkey --from-file key.json -n service

4.在我的部署中添加了以下项目:

    spec:
      volumes:
      - name: serviceaccountkey
        secret:
          secretName: serviceaccountkey
      containers:
        volumeMounts:
        - name: serviceaccountkey
          mountPath: /var/secrets/google
        env:
        - name: GOOGLE_APPLICATION_CREDENTIALS
          value: /var/secrets/google/key.json

当我部署它时,我得到: pods "service-7cdbcc67b9-" is forbidden: error looking up service account service/serviceaccount: serviceaccount "serviceaccount" not found

我不知道还需要做什么才能使这项工作正常进行,我已按照指南进行操作,但看不到任何遗漏的内容。

对此的任何帮助将不胜感激!

【问题讨论】:

  • Serviceaccount和pods一样是一个命名空间的服务,是不是在同一个命名空间?
  • 您的部署模板规范有误。检查教程以获取正确的架构。 containers 数组需要一个 name 和一个 image
  • 这种方法是一种很好的“任何 Kubernetes”挂载服务帐户的方法。如果您致力于 GKE,则有一种称为Workload Identity 的适当方法

标签: kubernetes google-cloud-platform google-kubernetes-engine service-accounts


【解决方案1】:

出现此错误的原因之一可能是您在一个命名空间中创建了一个服务帐户,并尝试将该服务帐户仅用于另一个命名空间。

我们可以通过使用新命名空间对服务帐户进行角色绑定来解决此错误。如果现有服务帐户在默认命名空间中,那么您可以将此 YAML 文件与新命名空间一起用于角色绑定。

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: 
  name: kubernetes-enforce-default 
  namespace: <new-namespace>
roleRef: 
  apiGroup: rbac.authorization.k8s.io 
  kind: ClusterRole
  name: kubernetes-enforce 
subjects:
- kind: ServiceAccount
  name: kubernetes-enforce
  namespace: kube-system

请参阅此similar issue 了解更多信息。

【讨论】:

    【解决方案2】:

    已通过使用 Workload Identity 将 K8s 服务帐户绑定到 GCP IAM 服务帐户来解决此问题:

    https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      • 1970-01-01
      • 2021-01-29
      • 2022-06-11
      相关资源
      最近更新 更多