Pods in GKE return "error looking up service account" - 如何在GKE中正确使用GCP服务账号?

Pods in GKE return "error looking up service account" - how to correctly use a GCP service account in GKE?

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

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

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

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

1.Created 一个名为“serviceaccount”的 GCP 服务帐户

2.I 创建并下载 JSON 密钥作为 key.json。

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

4.Added 我部署了以下项目:

    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

我不确定还需要做什么才能使它正常工作,我已经按照指南进行操作,没有发现遗漏的任何内容。

如有任何帮助,我们将不胜感激!

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

我们可以通过使用新命名空间对服务帐户进行角色绑定来解决此错误。如果现有服务帐户位于默认命名空间中,那么您可以将此 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

有关详细信息,请参阅此

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

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