您的 GKE 集群可以通过 Workload Identity Federation for GKE 访问 Spanner API。Workload Identity Federation for GKE 支持集群中的 Kubernetes 服务账号充当 IAM 服务账号。IAM 服务账号会为您的 Pod 提供应用默认凭证,因此您无需配置每个 Pod 即可使用您的个人用户凭证。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-05。"],[],[],null,["# Connect Spanner with a GKE cluster\n\nThis page describes how to grant your [Google Kubernetes Engine (GKE)](/kubernetes-engine/docs/concepts/kubernetes-engine-overview)\ncluster permissions to access your Spanner database.\n\nGKE is a managed Kubernetes service that makes it easy to deploy\nand manage containerized applications. By using GKE and\nSpanner together, you can take advantage of scalability,\nreliability, security, and high availability in your application layer and in\nyour database layer.\n\nYour GKE cluster can access the Spanner API through\n[Workload Identity Federation for GKE](/kubernetes-engine/docs/concepts/workload-identity).\nWorkload Identity Federation for GKE allows a Kubernetes service account in your cluster to act\nas an IAM service account. The IAM service account provides [Application Default\nCredentials](/docs/authentication/application-default-credentials)\nfor your pods, so that you don't need to configure each pod to use your personal\nuser credential.\n\nAfter you configure your applications to authenticate using Workload Identity Federation for GKE,\nyou can use [Spanner client libraries](/spanner/docs/reference/libraries)\nto query your Spanner databases. You can also [migrate your\napplications to your GKE node pools](/kubernetes-engine/docs/how-to/workload-identity#migrate_applications_to).\n\nTo create a connection in a sample environment, try the\n[Connecting Spanner with GKE Autopilot codelab](https://codelabs.developers.google.com/codelabs/cloud-spanner-gke-autopilot).\n\nEnable Workload Identity Federation for GKE\n-------------------------------------------\n\nIf you haven't done so already, enable Workload Identity Federation for GKE for your\nGKE cluster. You can enable Workload Identity Federation for GKE on a new\ncluster by creating a [new node pool](/kubernetes-engine/docs/how-to/workload-identity#enable_on_cluster)\nor you can enable Workload Identity Federation for GKE on an [existing node pool](/kubernetes-engine/docs/how-to/workload-identity#enable-existing-cluster).\n[GKE autopilot clusters](/kubernetes-engine/docs/concepts/autopilot-overview)\nhave Workload Identity Federation for GKE enabled by default. For more information, see\n[Enable Workload Identity Federation for GKE](/kubernetes-engine/docs/how-to/workload-identity#enable).\n\nAuthenticate connection to Spanner with Workload Identity Federation for GKE\n----------------------------------------------------------------------------\n\nConfigure your applications to authenticate to Google Cloud by using\nWorkload Identity Federation for GKE.\n\n1. Ensure that your GKE Pod uses a Kubernetes `ServiceAccount`\n object, as described in [Configure authorization and principals](/kubernetes-engine/docs/how-to/workload-identity#configure-authz-principals).\n\n2. Create an IAM allow policy that grants the necessary\n Spanner IAM roles to the Kubernetes\n `ServiceAccount` object. The following example grants the Spanner Database\n User (`roles/spanner.databaseUser`) role:\n\n gcloud projects add-iam-policy-binding \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e \\\n --member=\"principal://iam.googleapis.com/projects/\u003cvar translate=\"no\"\u003ePROJECT_NUMBER\u003c/var\u003e/locations/global/workloadIdentityPools/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e.svc.id.goog/subject/ns/\u003cvar translate=\"no\"\u003eNAMESPACE\u003c/var\u003e/sa/\u003cvar translate=\"no\"\u003eKSA_NAME\u003c/var\u003e \\\n --role=roles/spanner.databaseUser \\\n --condition=None\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: The project ID of the GKE cluster.\n - \u003cvar translate=\"no\"\u003ePROJECT_NUMBER\u003c/var\u003e: The numerical Google Cloud project number.\n - \u003cvar translate=\"no\"\u003eNAMESPACE\u003c/var\u003e: The Kubernetes namespace that contains the ServiceAccount.\n - \u003cvar translate=\"no\"\u003eKSA_NAME\u003c/var\u003e: The name of the ServiceAccount.\n\nConnect Spanner databases\n-------------------------\n\nAfter your application Pod is authenticated, you can use one of the\n[Spanner client libraries](/spanner/docs/reference/libraries)\nto query your Spanner database.\n\nWhat's next\n-----------\n\n- Learn how to [deploy your application to the GKE cluster](/kubernetes-engine/docs/deploy-app-cluster).\n- Learn how to [deploy your application using GKE Autopilot\n and Spanner](/kubernetes-engine/docs/tutorials/gke-spanner-integration).\n- Learn more about how to [migrate existing workloads to Workload Identity Federation for GKE](/kubernetes-engine/docs/how-to/workload-identity#migrate_applications_to).\n- Learn more about [SQL best practices](/spanner/docs/sql-best-practices).\n- Integrate Spanner with other ORMs including [Hibernate ORM](/spanner/docs/use-hibernate), [gorm](/spanner/docs/use-gorm), and [Django ORM](/spanner/docs/django-orm)."]]