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"]],["最終更新日 2025-09-05 UTC。"],[],[],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)."]]