Sub topic 12
Workload identity on GKE
This chapter assumes you know ServiceAccounts, RBAC, and projected tokens from earlier chapters. It also assumes you have a Google Cloud project and the gcloud and kubectl tools.
How it works
Workload Identity Federation for GKE lets a Pod use Google Cloud APIs without a downloaded key file. GKE acts as the identity provider for the cluster. Google Cloud trusts its tokens through a workload identity pool named PROJECT_ID.svc.id.goog.
Applications use the standard Google client libraries and call the metadata server as usual. On nodes with this feature enabled, the GKE metadata server answers those calls. It reads the Pod's Kubernetes ServiceAccount, exchanges that identity for Google credentials, and returns a short-lived access token. The application code does not change.
Prerequisites on the cluster
- Workload Identity Federation must be enabled on the cluster. It is on by default in Autopilot. On Standard clusters, create the cluster with
--workload-pool=PROJECT_ID.svc.id.goog. - Node pools must use the GKE metadata server (
--workload-metadata=GKE_METADATA). Without it, Pods fall back to the node's own identity.
Link a ServiceAccount to a Google service account
The first approach uses an annotation on the Kubernetes ServiceAccount plus an IAM binding on a Google service account (GSA). The IAM binding lets the Kubernetes identity impersonate the GSA.
kubectl annotate serviceaccount app-reader -n demo \
iam.gke.io/[email protected]
gcloud iam service-accounts add-iam-policy-binding \
[email protected] \
--role roles/iam.workloadIdentityUser \
--member "serviceAccount:my-project.svc.id.goog[demo/app-reader]"The --member value has the form PROJECT_ID.svc.id.goog[NAMESPACE/SERVICEACCOUNT]. This is where trust is scoped. Only the app-reader ServiceAccount in the demo namespace can impersonate the GSA. A ServiceAccount with the same name in another namespace cannot.
Then grant the GSA only the permissions the application needs. For example, give it roles/storage.objectViewer on one bucket instead of a project-wide role.
Skipping the Google service account
You can also grant roles directly to the Kubernetes identity. The principal is written as a principal:// URI that includes the project number, the pool, and the subject ns/NAMESPACE/sa/NAME. This removes one object to manage. Some Google services do not accept federated principals yet, so check the documentation for the service you use. In those cases, use the GSA impersonation method shown above.
Verify
Run a Pod that uses the ServiceAccount and ask which identity it has:
kubectl run -it --rm wi-test -n demo \
--image=google/cloud-sdk:slim \
--overrides='{"spec":{"serviceAccountName":"app-reader"}}' \
-- gcloud auth listThe active account should be the GSA, [email protected]. If it shows a Compute Engine default service account, the Pod is still using the node identity.
Common failures
| Symptom | Likely cause |
|---|---|
| Pod shows the node's identity | Node pool is not using `GKE_METADATA` |
| Permission denied on impersonation | The `--member` namespace or name is wrong, or the binding is missing |
| Works in one namespace, fails in another | The binding names a different namespace |
| Annotation set but no effect | The Pod does not use that ServiceAccount through `serviceAccountName` |
Exercise
A Pod in namespace prod uses ServiceAccount app-reader, and it is annotated with a GSA. The IAM binding member is my-project.svc.id.goog[demo/app-reader]. Will the Pod get the GSA's credentials? Why?
Show answer
No. The binding names namespace demo, but the Pod runs in prod. The subject is prod/app-reader, which does not match, so impersonation is denied. Add a binding with member my-project.svc.id.goog[prod/app-reader] if that access is intended.