Authenticate to Google Cloud APIs from GKE workloads Stay organized with collections Save and categorize content based on your preferences.
This page shows you how to more securely access Google Cloud APIs fromyour workloads that run in Google Kubernetes Engine (GKE) clusters by usingWorkload Identity Federation for GKE.
This page is for Identity and account admins, Operators, andDevelopers who create and manage policies related to userpermissions. To learn more about common roles and example tasks that wereference in Google Cloud content, seeCommon GKE user roles and tasks.
Before reading this page, ensure that you're familiar withWorkload Identity Federation for GKE concepts.
Before you begin
Before you start, make sure that you have performed the following tasks:
- Enable the Google Kubernetes Engine API. Enable Google Kubernetes Engine API
- If you want to use the Google Cloud CLI for this task,install and theninitialize the gcloud CLI. If you previously installed the gcloud CLI, get the latest version by running the
gcloud components updatecommand. Earlier gcloud CLI versions might not support running the commands in this document.Note: For existing gcloud CLI installations, make sure to set thecompute/regionproperty. If you use primarily zonal clusters, set thecompute/zoneinstead. By setting a default location, you can avoid errors in the gcloud CLI like the following:One of [--zone, --region] must be supplied: Please specify location. You might need to specify the location in certain commands if the location of your cluster differs from the default that you set.
- Ensure that the Google Cloud APIs that you want to access are supportedby Workload Identity Federation for GKE. For a list of supported APIs, seeSupported products and limitations.If the API isn't supported or if your use case is blocked by thelimitations of Workload Identity Federation for that service, seeAlternative: link Kubernetes ServiceAccounts to IAMin this page.
Ensure that you have enabled the IAM Service Account Credentials API.
Ensure that you have the followingIAM roles:
roles/container.adminroles/iam.serviceAccountAdmin
Ensure that you understand thelimitations of Workload Identity Federation for GKE.
Ensure that you have an existing Autopilot or Standardcluster. To create a new cluster, seeCreate an Autopilot cluster.
Enable Workload Identity Federation for GKE on clusters and node pools
In Autopilot, Workload Identity Federation for GKE is always enabled. Skip to theConfigure applications to use Workload Identity Federation for GKE section.
In Standard, you enable Workload Identity Federation for GKE on clusters and nodepools using the Google Cloud CLI or the Google Cloud console.Workload Identity Federation for GKEmust be enabled at the cluster level before you canenable Workload Identity Federation for GKE on node pools.
You can enable Workload Identity Federation for GKE on an existing Standard cluster byusing the gcloud CLI or the Google Cloud console. Existing node poolsare unaffected, but any new node pools in the cluster use Workload Identity Federation for GKE.
gcloud
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, aCloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
To enable Workload Identity Federation for GKE on an existing cluster, run the followingcommand:
gcloudcontainerclustersupdateCLUSTER_NAME\--location=LOCATION\--workload-pool=PROJECT_ID.svc.id.googReplace the following:
CLUSTER_NAME: the name of your existing cluster.LOCATION: theCompute Engine locationof your cluster.PROJECT_ID: your Google Cloud project ID.
Console
To enable Workload Identity Federation for GKE on an existing cluster, do the following:
Go to theGoogle Kubernetes Engine page in Google Cloud console.
In the cluster list, click the name of the cluster you want to modify.
On the cluster details page, in theSecurity section, clickEdit Workload Identity.
In theEdit Workload Identity dialog, select theEnable Workload Identity checkbox.
ClickSave changes.
Migrate existing workloads to Workload Identity Federation for GKE
After you enable Workload Identity Federation for GKE on an existing cluster, you might want tomigrate your running workloads to use Workload Identity Federation for GKE. Select themigration strategy that is ideal for your environment. You can create new node pools with Workload Identity Federation for GKEenabled, or update existing node pools to enable Workload Identity Federation for GKE.
You can only enable Workload Identity Federation for GKE on a node pool if Workload Identity Federation for GKEis enabled on the cluster.
Create new node pools if you also need to modify yourapplications to be compatible with Workload Identity Federation for GKE.
Create a new node pool
All new node pools that you create default to using Workload Identity Federation for GKE if thecluster has Workload Identity Federation for GKE enabled. To create a new node pool withWorkload Identity Federation for GKE enabled, run the following command:
gcloudcontainernode-poolscreateNODEPOOL_NAME\--cluster=CLUSTER_NAME\--location=CONTROL_PLANE_LOCATION\--workload-metadata=GKE_METADATAReplace the following:
NODEPOOL_NAME: the name of the new node pool.CLUSTER_NAME: the name of the existing cluster that hasWorkload Identity Federation for GKE enabled.CONTROL_PLANE_LOCATION: the Compute Enginelocation of the control plane of yourcluster. Provide a region for regional clusters, or a zone for zonal clusters.
The--workload-metadata=GKE_METADATA flag configures the node pool to usethe GKE metadata server.
Include the flagso that node pool creation fails if Workload Identity Federation for GKE is not enabled on thecluster.
Update an existing node pool
You can manually enable Workload Identity Federation for GKE on existing node pools after youenable Workload Identity Federation for GKE on the cluster.
Caution: Modifying the node pool immediately enables Workload Identity Federation for GKE forany workloads running in the node pool. This prevents the workloads from usingthe service account that your nodes use and might result in disruptions.gcloud
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, aCloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
To modify an existing node pool to use Workload Identity Federation for GKE, run the followingcommand:
gcloudcontainernode-poolsupdateNODEPOOL_NAME\--cluster=CLUSTER_NAME\--location=CONTROL_PLANE_LOCATION\--workload-metadata=GKE_METADATAIf a cluster has Workload Identity Federation for GKE enabled, you can selectively disable it on aspecific node pool by explicitly specifying
--workload-metadata=GCE_METADATA. SeeProtecting clustermetadata for moreinformation.
Console
To modify an existing node pool to use Workload Identity Federation for GKE, perform the followingsteps:
Go to theGoogle Kubernetes Engine page in Google Cloud console.
In the cluster list, click the name of the cluster that you want to modify.
Click theNodes tab.
In theNode Pools section, click the name of the node pool that youwant to modify.
On theNode pool details page, clickEdit.
On theEdit node pool page, in theSecurity section, select theEnable GKE Metadata Server checkbox.
ClickSave.
Configure applications to use Workload Identity Federation for GKE
To let your GKE applications authenticate to Google CloudAPIs using Workload Identity Federation for GKE, you create IAM policies forthe specific APIs. The principal in these policies is an IAMprincipal identifier that corresponds to the workloads, namespaces, orKubernetes ServiceAccounts. This process returns a federated access token thatyour workload can use in API calls.
Alternatively, you can configure Kubernetes ServiceAccounts to impersonateIAM service accounts, which configures GKE to exchange thefederated access token for an access token from the IAMService Account Credentials API. For details, see theAlternative: link Kubernetes ServiceAccounts to IAMsection.
Configure authorization and principals
Get credentials for your cluster:
gcloudcontainerclustersget-credentialsCLUSTER_NAME\--location=CONTROL_PLANE_LOCATIONReplace the following:
CLUSTER_NAME: the name of your clusterthat has Workload Identity Federation for GKE enabled.CONTROL_PLANE_LOCATION: the Compute Enginelocation of the control plane of yourcluster. Provide a region for regional clusters, or a zone for zonal clusters.
Create a namespace to use for the Kubernetes service account. You can alsouse the
defaultnamespace or any existing namespace.kubectlcreatenamespaceNAMESPACECreate a Kubernetes ServiceAccount for your application to use. You canalso use any existing Kubernetes ServiceAccount in any namespace. If youdon't assign a ServiceAccount to your workload, Kubernetes assigns the
defaultServiceAccount in the namespace.kubectlcreateserviceaccountKSA_NAME\--namespaceNAMESPACEReplace the following:
KSA_NAME: the name of your new KubernetesServiceAccount.NAMESPACE: the name of the Kubernetes namespacefor the ServiceAccount.
Create an IAM allow policy that references the KubernetesServiceAccount. As a good practice, grant permissions to specificGoogle Cloud resources that your application needs to access. You musthave relevant IAM permissions to create allow policies inyour project.
For example, the following command grants theKubernetes Engine Cluster Viewer(
roles/container.clusterViewer) role to the ServiceAccount that youcreated:gcloudprojectsadd-iam-policy-bindingprojects/PROJECT_ID\--role=roles/container.clusterViewer\--member=principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/KSA_NAME\--condition=NoneReplace the following:
PROJECT_ID: your Google Cloud project ID.PROJECT_NUMBER: your numerical Google Cloudproject number.
You can grant roles on any Google Cloud resource that supportsIAM allow policies. The syntax of the principal identifierdepends on the Kubernetes resource. For a list of supported identifiers, seePrincipal identifiers for Workload Identity Federation for GKE.
Optional: Configure service mesh options
If you use Istio or Cloud Service Mesh to manage your environment, add the followingannotation to themetadata.annotations field in your Pod specification:
metadata: annotations:proxy.istio.io/config: '{ "holdApplicationUntilProxyStarts": true }'This annotation prevents your containers from starting until the service meshproxy is ready to redirect traffic from your applications.
Verify the Workload Identity Federation for GKE setup
In this section, you create a Cloud Storage bucket and grant view access onthe bucket to the Kubernetes ServiceAccount that you created in the previoussection. You then deploy a workload and test that the container can listclusters in the project.
Create an empty Cloud Storage bucket:
gcloudstoragebucketscreategs://BUCKETReplace
BUCKETwith a name for your new bucket.Grant theStorage Object Viewer(
roles/storage.objectViewer) role to the ServiceAccount that youcreated:gcloudstoragebucketsadd-iam-policy-bindinggs://BUCKET\--role=roles/storage.objectViewer\--member=principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/KSA_NAME\--condition=NoneReplace the following:
PROJECT_ID: your Google Cloud project ID.PROJECT_NUMBER: your numerical Google Cloudproject number.NAMESPACE: the Kubernetes namespace that containsthe ServiceAccount.KSA_NAME: the name of the ServiceAccount.
Save the following manifest as
test-app.yaml:apiVersion:v1kind:Podmetadata:name:test-podnamespace:NAMESPACEspec:serviceAccountName:KSA_NAMEcontainers:-name:test-podimage:google/cloud-sdk:slimcommand:["sleep","infinity"]resources:requests:cpu:500mmemory:512Miephemeral-storage:10MiIn Standard clusters only, add the following to the
template.specfield to place the Pods on node pools that use Workload Identity Federation for GKE.Skip this step in Autopilot clusters, which reject this nodeSelectorbecause every node uses Workload Identity Federation for GKE.
spec:nodeSelector:iam.gke.io/gke-metadata-server-enabled:"true"Apply the configuration to your cluster:
kubectlapply-ftest-app.yamlWait for the Pod to become ready. To check the status of the Pod, run thefollowing command:
kubectlgetpods--namespace=NAMESPACEWhen the Pod is ready, the output is similar to the following:
NAME READY STATUS RESTARTS AGEtest-pod 1/1 Running 0 5m27sOpen a shell session in the Pod:
kubectlexec-itpods/test-pod--namespace=NAMESPACE--/bin/bashGet a list of objects in the bucket:
curl-XGET-H"Authorization: Bearer$(gcloudauthprint-access-token)"\"https://storage.googleapis.com/storage/v1/b/BUCKET/o"The output is the following:
{ "kind": "storage#objects"}This output shows that your Pod can access objects in the bucket.
Alternative: link Kubernetes ServiceAccounts to IAM
Use IAM principal identifiers to configure Workload Identity Federation for GKE. However, this federated identity has specificlimitations for each supported Google Cloud API. If these limitations apply to you, use the following steps to configure access to those APIs from your GKE workloads.
Create a Kubernetes namespace:
kubectlcreatenamespaceNAMESPACECreate a Kubernetes ServiceAccount:
kubectlcreateserviceaccountKSA_NAME\--namespace=NAMESPACECreate an IAM service account. You can also use any existingIAM service account in any project in your organization.
gcloudiamservice-accountscreateIAM_SA_NAME\--project=IAM_SA_PROJECT_IDReplace the following:
IAM_SA_NAME: a name for your new IAMservice account.IAM_SA_PROJECT_ID: the project ID for yourIAM service account.
For information on authorizing IAM service accounts to access Google CloudAPIs, seeUnderstanding service accounts.
Grant your IAM service account the roles that it needs onspecific Google Cloud APIs:
gcloudprojectsadd-iam-policy-bindingIAM_SA_PROJECT_ID\--member"serviceAccount:IAM_SA_NAME@IAM_SA_PROJECT_ID.iam.gserviceaccount.com"\--role"ROLE_NAME"Replace
ROLE_NAMEwith the name of the role, likeroles/spanner.viewer.Create an IAM allow policy that gives the KubernetesServiceAccount access to impersonate the IAM service account:
gcloudiamservice-accountsadd-iam-policy-bindingIAM_SA_NAME@IAM_SA_PROJECT_ID.iam.gserviceaccount.com\--roleroles/iam.workloadIdentityUser\--member"serviceAccount:PROJECT_ID.svc.id.goog[NAMESPACE/KSA_NAME]"The member name must include the namespace and Kubernetes ServiceAccountname. For example,
serviceAccount:example-project.svc.id.goog[example-namespace/example-serviceaccount].Annotate the Kubernetes ServiceAccount so that GKE sees thelink between the service accounts:
kubectlannotateserviceaccountKSA_NAME\--namespaceNAMESPACE\iam.gke.io/gcp-service-account=IAM_SA_NAME@IAM_SA_PROJECT_ID.iam.gserviceaccount.comBoth the IAM allow policyand the annotation arerequired when you use this method.
Optional: Annotate the Kubernetes ServiceAccount so that your applicationsget the identifier in the IAM principal identifier syntax:
kubectlannotateserviceaccountKSA_NAME\--namespace=NAMESPACE\iam.gke.io/return-principal-id-as-email="true"
Use Workload Identity Federation for GKE from your code
Authenticating to Google Cloud services from your code is the same processas authenticating using theCompute Engine metadataserver. When you use Workload Identity Federation for GKE, yourrequests to the instance metadata server are routed to theGKE metadata server.Existing code that authenticates using the instance metadata server (like codeusing theGoogle Cloud client libraries)should work without modification.
Use quota from a different project with Workload Identity Federation for GKE
On clusters running GKE version 1.24 or later, you can optionally configure your Kubernetes service account to use quotafrom a different Google Cloud project when making calls totheGenerateAccessToken and theGenerateIdToken methods in theIAM Service Account Credentials API. This lets you avoid using the entire quota inyour main project, and instead use quota from other projects forthese services in your cluster.
To configure aquota project with Workload Identity Federation for GKE, do thefollowing:
Grant the
serviceusage.services.usepermission on the quota projectto the Kubernetes service account.gcloudprojectsadd-iam-policy-bindingQUOTA_PROJECT_ID\--role=roles/serviceusage.serviceUsageConsumer\--member='principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/KSA_NAME'\Replace
QUOTA_PROJECT_IDwith the project IDof the quota project.Annotate the Kubernetes service account with the quota project:
kubectlannotateserviceaccountKSA_NAME\--namespaceNAMESPACE\iam.gke.io/credential-quota-project=QUOTA_PROJECT_ID
To verify the configuration works correctly, do the following:
Create a Pod and start a shell session. See the Kubernetes documentationtoGet a Shell to a Running Container.
Make a request to the metadata server:
curl-H"Metadata-Flavor: Google"http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/tokenGo to theIAM Service Accounts Credentials API pagein the Google Cloud console for your quota project:
Check for changes in traffic.
Clean up
To stop using Workload Identity Federation for GKE, revoke access to the IAMservice account and disable Workload Identity Federation for GKE on the cluster.
Revoke access
To revoke access to the principal, remove the IAM allow policythat you created in theConfigure applications to use Workload Identity Federation for GKE section.
For example, to revoke access to an Artifact Registry repository, run the followingcommand:
gcloudartifactsrepositoriesremove-iam-policy-bindingREPOSITORY_NAME\--location=REPOSITORY_LOCATION\--member='principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/KSA_NAME'\--role='roles/artifactregistry.reader'\--allDisable Workload Identity Federation for GKE
You can only disable Workload Identity Federation for GKE on Standardclusters.
gcloud
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, aCloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Disable Workload Identity Federation for GKE on each node pool:
gcloudcontainernode-poolsupdateNODEPOOL_NAME\--cluster=CLUSTER_NAME\--location=CONTROL_PLANE_LOCATION\--workload-metadata=GCE_METADATARepeat this command for every node pool in the cluster.
Disable Workload Identity Federation for GKE in the cluster:
gcloudcontainerclustersupdateCLUSTER_NAME\--location=CONTROL_PLANE_LOCATION\--disable-workload-identity
Console
Go to theGoogle Kubernetes Engine page in Google Cloud console.
In the cluster list, click the name of the cluster that you want to modify.
Click theNodes tab.
To disable Workload Identity Federation for GKE on each node pool, do the following foreach node pool in theNode Pools section:
- Click the name of the node pool that you want to modify.
- On theNode pool details page, clickEdit.
- On theEdit node pool page, in theSecurity section, clear theEnable GKE Metadata Server checkbox.
- ClickSave.
To disable Workload Identity Federation for GKE for the cluster, do the following:
- Click theDetails tab.
- In theSecurity section, next toWorkload Identity, clickEdit.
- In theEdit Workload Identity dialog, clear theEnable Workload Identity checkbox.
- ClickSave changes.
Disable Workload Identity Federation for GKE in your organization
The steps in thelink Kubernetes ServiceAccounts to IAMsection let Kubernetes ServiceAccounts impersonate the identity of the linkedIAM service account. Long-lived API tokens for the KubernetesServiceAccounts can be exchanged for the corresponding IAMservice account access tokens.
You might want to disable Workload Identity Federation for GKE for clusters in yourorganization, folder, or project when you want to isolate workloads fromIAM service accounts. For example, if you plan todisable service account creationordisable service account key creation,disabling Workload Identity Federation for GKE prevents the exchange of ServiceAccount tokensfor IAM service account access tokens.
For more information, seeDisable workload identity cluster creation.
Troubleshooting
For troubleshooting information, refer toTroubleshooting Workload Identity Federation for GKE.
What's next
- Learn more aboutWorkload Identity Federation for GKE.
- Read theGKE security overview.
- Learn aboutProtecting Cluster Metadata.
- Learn aboutIAM service accounts.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.