Using GKE service account credentials with kubectl
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Using kubectl against a GKE cluster with a service account is really an authentication-and-authorization problem with two identity layers. First, kubectl must authenticate to GKE using Google Cloud credentials, and then that authenticated identity must be authorized to perform Kubernetes actions through IAM and often RBAC.
Do Not Confuse Google Cloud and Kubernetes Service Accounts
The first important distinction is between:
- a Google Cloud service account, used to authenticate to Google Cloud and GKE
- a Kubernetes service account, used by workloads running inside the cluster
When people ask about using service account credentials with kubectl, they usually mean a Google Cloud service account, not a Kubernetes service account. kubectl runs outside the cluster, so it needs Google Cloud identity first.
Install the GKE Auth Plugin
Current GKE access for kubectl depends on the gke-gcloud-auth-plugin. Without it, kubectl can fail even if your Google Cloud credentials are otherwise correct.
Check whether it is installed:
If needed, install it through the Google Cloud CLI component flow:
This plugin is what lets kubectl obtain the credentials needed to talk to the GKE API server.
Authenticate with a Google Cloud Service Account
If you are using a JSON key, activate the service account in gcloud first:
Then fetch cluster credentials into your kubeconfig:
After that, ordinary kubectl commands use the credentials configured by the GKE auth plugin:
For automation, this is the standard pattern when a key-based service account is the chosen credential source.
Prefer Impersonation or Attached Identity When Possible
JSON keys work, but they are not the most secure long-term option. If your environment supports it, service account impersonation or a workload-attached identity is usually better because you avoid distributing long-lived keys.
A simple impersonation flow looks like this:
That way, gcloud uses short-lived credentials derived from the service account instead of relying on a raw key file sitting on disk.
Authentication Is Not Authorization
Successfully running get-credentials does not guarantee the service account can do anything useful in the cluster. The identity still needs permission.
At the Google Cloud level, the account needs enough IAM permission to retrieve cluster credentials and access the cluster endpoint. At the Kubernetes level, it may also need RBAC bindings for the operations you want to run.
A minimal RBAC example might look like this:
Then apply it with:
The important detail is that the subject name is the Google identity email seen by the cluster.
Verify Which Identity kubectl Is Using
When debugging access issues, inspect the current kubeconfig context and test a read-only command first:
If authentication works but can-i returns no, the problem is authorization, not credentials.
Common Pitfalls
One common mistake is confusing Google Cloud service accounts with Kubernetes service accounts. They solve different identity problems and are not interchangeable in kubectl setup.
Another is forgetting the gke-gcloud-auth-plugin. Modern GKE client access depends on it, so old kubeconfig assumptions can break unexpectedly.
Developers also sometimes authenticate successfully with gcloud but forget that the service account still needs IAM and possibly Kubernetes RBAC permissions to do anything meaningful.
Finally, avoid spreading raw JSON keys unless you truly need them. Service account impersonation or attached service-account identity is usually a cleaner and safer automation path.
Summary
- '
kubectlaccess to GKE uses Google Cloud identity, not an in-cluster Kubernetes service account.' - Install and use the
gke-gcloud-auth-pluginfor current GKE client authentication. - Authenticate with a Google Cloud service account, then run
gcloud container clusters get-credentials. - Grant both cloud-side access and cluster-side authorization where needed.
- Prefer impersonation or attached identity over long-lived JSON keys when possible.

