Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questa pagina descrive come concedere al tuo cluster Google Kubernetes Engine (GKE) le autorizzazioni per accedere al database Spanner.
GKE è un servizio Kubernetes gestito che semplifica il deployment
e la gestione delle applicazioni containerizzate. Utilizzando GKE e
Spanner insieme, puoi sfruttare la scalabilità,
l'affidabilità, la sicurezza e l'alta disponibilità nel livello di applicazione e nel
livello del database.
Il cluster GKE può accedere all'API Spanner tramite
Workload Identity Federation for GKE.
La federazione delle identità per i carichi di lavoro per GKE consente a un account di servizio Kubernetes nel tuo cluster di agire come account di servizio IAM. L'account di servizio IAM fornisce le credenziali predefinite per l'applicazione per i pod, in modo da non dover configurare ogni pod per utilizzare le tue credenziali utente personali.
Se non l'hai già fatto, abilita Workload Identity Federation for GKE per il tuo
cluster GKE. Puoi abilitare Workload Identity Federation for GKE in un nuovo
cluster creando un nuovo pool di nodi
o puoi abilitare Workload Identity Federation for GKE in un pool di nodi esistente.
I cluster GKE Autopilot
hanno la federazione delle identità per i carichi di lavoro per GKE abilitata per impostazione predefinita. Per maggiori informazioni, consulta
Abilitare Workload Identity Federation per GKE.
Autentica la connessione a Spanner con Workload Identity Federation for GKE
Configura le tue applicazioni per autenticarle Google Cloud utilizzando
Workload Identity Federation for GKE.
Crea un criterio di autorizzazione IAM che conceda i ruoli IAM Spanner necessari all'oggetto KubernetesServiceAccount. Nell'esempio seguente viene concesso il ruolo Utente database Spanner (roles/spanner.databaseUser):
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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)."]]