A service account kubeconfig is a kubeconfig file whose user entry authenticates with a ServiceAccount token instead of a personal login. You build it in five moves: create the ServiceAccount, bind a narrow RBAC role, issue a token with kubectl create token, collect the cluster CA and API URL, then assemble the file with kubectl config commands. This guide gives every step, the errors you will hit, and how to keep the finished file from becoming a liability. We build Kubezilla Desktop, which stores kubeconfigs locally on your machine, so storage gets as much attention here as creation.
TL;DR
- A service account kubeconfig lets automation or a teammate reach one cluster through a dedicated identity with only the permissions you grant.
- Since Kubernetes v1.24, long-lived token secrets are no longer created automatically, so you issue short-lived tokens with
kubectl create token. - Bind a Role, not cluster-admin, then verify with
kubectl auth can-iand the--kubeconfigflag before you hand the file over. - Treat the file like a secret: keep it local, keep tokens short-lived, and delete the ServiceAccount to revoke access.
- Kubezilla Desktop keeps kubeconfigs in secure local storage with no cloud sync or telemetry, at €5 per month.
Table of Contents
- What a service account kubeconfig is
- Prerequisites before you start
- How to create a service account kubeconfig, step by step
- Test the file before you hand it over
- Store, limit, rotate and revoke
- Managing these files across many clusters
- Where Kubezilla Desktop fits
- Our perspective on service account credentials
- FAQ
- Sources
What a service account kubeconfig is
Kubernetes has two kinds of identity: humans and ServiceAccounts. A ServiceAccount provides identity for processes running in a Pod, and it is a namespaced object managed through the Kubernetes API. Pods get their credentials automatically. Since v1.22, the TokenRequest API supplies short-lived, rotating tokens mounted as projected volumes, replacing the older secret-based mechanism.
A service account kubeconfig is something different. It is a file you create for use outside the Pod: a CI runner, a deployment script, a monitoring job, or a contractor who needs scoped access. The file has the same three parts as any kubeconfig (clusters, users, contexts), but the user entry carries a ServiceAccount token.
Why not just share your own admin kubeconfig? The Kubernetes project separates human accounts from service accounts so actions can be audited better, and it lets cluster users create service accounts for specific tasks on demand, which makes least privilege easier to apply. A dedicated identity means a dedicated audit trail and a credential you can kill without touching anyone else's access.
Prerequisites before you start
You need three things:
- kubectl installed and already working against the cluster with your own credentials.
- Permission to manage ServiceAccounts, and to get Secrets, in the target namespace. Creating Roles and RoleBindings also requires RBAC rights, and Kubernetes will refuse to let you grant permissions you do not hold yourself.
- A clear list of what the account must do. "Deploy to the
appsnamespace" is a list. "Everything" is not.
The third point matters most. Permissions reach a ServiceAccount through Roles or ClusterRoles bound with RoleBindings or ClusterRoleBindings, and the guiding rule is least privilege. Decide the verbs and resources before you type a command.
Also check your cluster version. If you run v1.24 or later, the steps below apply as written. On older clusters, tokens may already exist as secrets, but you should still plan to move to short-lived tokens.
How to create a service account kubeconfig, step by step
The flow below follows the standard pattern also described in Giant Swarm's automation access tutorial: create the account, define RBAC, generate a token, gather the CA and API URL, then build the file with kubectl config. We use a ServiceAccount named ci-deployer in the namespace apps as the example.

Step 1: Create the ServiceAccount
kubectl create serviceaccount ci-deployer --namespace apps
This fails if the namespace does not exist. If you manage many namespaces, our namespace management guide covers keeping them tidy so service accounts do not pile up unnoticed.
Step 2: Grant only the permissions it needs
Create a Role that allows what the job does. For a deployment pipeline that updates Deployments and reads Pods:
kubectl create role ci-deployer-role --namespace apps \
--verb=get,list,watch,create,update,patch \
--resource=deployments,pods
kubectl create rolebinding ci-deployer-binding --namespace apps \
--role=ci-deployer-role \
--serviceaccount=apps:ci-deployer
The common failure here is reaching for cluster-admin because it makes errors disappear. It also makes a leaked file a full cluster compromise. A namespaced Role limits the damage to one namespace. If the job really needs cluster-wide reads, use a ClusterRole with only read verbs.
Step 3: Issue a token
kubectl create token ci-deployer --namespace apps --duration=1h
This calls the TokenRequest API and prints a signed token that expires. Here is why it matters: as of v1.24, long-lived token secrets are no longer created automatically for new service accounts. Kubernetes also documents the LegacyServiceAccountTokenNoAutoGeneration feature gate, which covered v1.24 to v1.26 and stopped automatic token creation. If you search older tutorials that read a token out of an auto-created secret, that is why they fail on a modern cluster.
The security reasoning is simple: expiring tokens significantly lower the risk of credential leakage compared with manually created, long-lived secrets. Pick the shortest duration your workflow tolerates. The cluster may cap the maximum lifetime, so an overly long request can be shortened.
If you truly need a non-expiring credential for a system that cannot refresh tokens, you must create a Secret of type kubernetes.io/service-account-token by hand. Treat that as the exception, record who holds it, and plan its deletion.
Step 4: Collect the cluster CA and API server URL
If your current kubeconfig points at the right cluster, read both values from it:
API_SERVER=$(kubectl config view --minify --raw -o jsonpath='{.clusters[0].cluster.server}')
CA_DATA=$(kubectl config view --minify --raw -o jsonpath='{.clusters[0].cluster.certificate-authority-data}')
The --raw flag matters. Without it, kubectl redacts the certificate data and you build a file that cannot verify the server. If your API server sits behind a bastion, the URL you copy may be a local tunnel address such as https://127.0.0.1:6443. In that case the file only works while the tunnel is up, which our SSH tunnel guide explains, including the certificate name errors that tunnels cause.
Step 5: Assemble the kubeconfig
Write to a new file so you never touch your default config:
export KUBECONFIG=./ci-deployer.kubeconfig
TOKEN=$(kubectl --kubeconfig ~/.kube/config create token ci-deployer -n apps --duration=1h)
echo "$CA_DATA" | base64 -d > /tmp/ca.crt
kubectl config set-cluster prod-apps \
--server="$API_SERVER" \
--certificate-authority=/tmp/ca.crt --embed-certs=true
kubectl config set-credentials ci-deployer --token="$TOKEN"
kubectl config set-context ci-deployer@prod-apps \
--cluster=prod-apps --user=ci-deployer --namespace=apps
kubectl config use-context ci-deployer@prod-apps
unset KUBECONFIG
rm /tmp/ca.crt
Setting KUBECONFIG for this session directs the kubectl config commands at the new file. If the variable behaves unexpectedly, our guide to the KUBECONFIG environment variable shows how files merge and how to debug the result.
You now have a working file. It is also a credential that expires, so the next question is how it gets refreshed.
Test the file before you hand it over
Never test with your admin config loaded, because a success there proves nothing. Point kubectl at the new file explicitly. Using the --kubeconfig flag avoids changing your default environment, and kubectl auth can-i verifies exact permissions:
kubectl --kubeconfig ./ci-deployer.kubeconfig get pods
kubectl --kubeconfig ./ci-deployer.kubeconfig auth can-i create deployments -n apps
kubectl --kubeconfig ./ci-deployer.kubeconfig auth can-i delete nodes
kubectl --kubeconfig ./ci-deployer.kubeconfig auth can-i get secrets -n kube-system
The first two should succeed. The last two should print no. A permission that should be denied but is allowed means a binding is wider than you intended.
Errors you will probably see
| Error | Likely cause | Fix |
|---|---|---|
Unauthorized | Token expired, malformed, or ServiceAccount deleted | Issue a new token and update the user entry |
Forbidden (user system:serviceaccount:apps:ci-deployer) | Role or binding missing the verb or resource | Adjust the Role, then re-run auth can-i |
x509: certificate signed by unknown authority | Missing or wrong CA data | Rebuild with --raw and --embed-certs=true |
x509: certificate is valid for ..., not 127.0.0.1 | Connecting through a tunnel to a name not on the certificate | Set tls-server-name or fix the tunnel target |
connection refused or timeout | API URL unreachable from where the file is used | Check network path, VPN or tunnel |
A Forbidden message names the exact identity, which is the quickest way to confirm the file is using the account you meant.
For external systems that need to validate these credentials, Kubernetes recommends the TokenReview API, which invalidates tokens automatically when associated objects are deleted.
Store, limit, rotate and revoke
Creating the file is the easy half. The risks come afterward.
Storage. Kubernetes documentation warns that storing passwords in a client config is risky, and a credential plugin can keep them separately. It also advises treating kubeconfig files with the same care as shell scripts, since a crafted file from an untrusted source can lead to code execution or file exposure. Never paste a token into a ticket or chat. Do not commit the file to a repository. If you are unsure where files live on your system, see our kubeconfig location guide for Windows, macOS and Linux.
Limits. Keep the Role narrow, scope it to one namespace, and give the token a short life. For a pipeline, issue the token at the start of each run instead of storing one for months.
Rotation. With short-lived tokens, rotation is just issuing a new one. Put the create token call in the job that needs it, so no human copies tokens around.
Revocation. Delete the RoleBinding to remove permissions at once, or delete the ServiceAccount to invalidate its tokens:
kubectl delete serviceaccount ci-deployer --namespace apps
Because each automation has its own account, revoking one never disrupts another. That is the practical payoff of not sharing a single admin file. For broader hardening of the API endpoint itself, our Kubernetes API access security guide goes further.

Managing these files across many clusters
One service account file per cluster, per environment, per purpose adds up quickly. Kubernetes handles this with contexts, which let you switch between cluster, namespace and user combinations with kubectl config use-context. That works, but merging files by hand, remembering which context you are in, and avoiding a command against production are all on you. The risks and habits are covered in our kubeconfig context management guide.
When you evaluate a kubeconfig manager, the criteria that follow from the documentation are practical:
- Credentials stay out of plain client config. Look for credential-plugin support and separate credential storage.
- Local storage. Files should stay on your machine, with no cloud sync that copies cluster credentials elsewhere.
- Clear context switching. The active cluster and namespace should be visible at all times.
- Multi-cluster access with tunnels. Private clusters behind bastion hosts should be reachable without hand-built port forwards.
- Light footprint. A manager you keep open all day should not eat memory.
If your clusters use single sign-on for people and service accounts for machines, you will keep both kinds of entries. Our OIDC kubeconfig guide covers the human side, and for teams spread across providers, the multi-cloud management guide shows how to avoid hopping between consoles.
Where Kubezilla Desktop fits
Kubezilla Desktop is a native app for Linux, macOS and Windows that gives you one interface for cluster and resource operations. It stores kubeconfigs securely on your own machine, with no cloud sync or telemetry. It connects to multiple clusters, supports SSH tunnels and direct remote cluster management, and is built in Rust for low memory use and fast startup. Once your service account file exists, you can load it next to your other contexts, then watch live logs, check metrics, open pod terminals, edit YAML and scale workloads in the same window, so you can confirm what the account can actually see without juggling terminals and browser tabs.
The app also covers Helm and Argo CD operations. If that is your next step after access works, the Argo CD installation tutorial walks through it.
Kubezilla Desktop costs €5 per month. You can see the app on the Kubernetes desktop app page.

Our perspective on service account credentials
Most credential leaks we hear about in Kubernetes are not clever attacks. They are old files in the wrong place: a kubeconfig in a shared folder, a token pasted into a chat, an admin file reused because it was convenient.
That is why we built Kubezilla Desktop local-first. Your kubeconfigs are stored securely on your machine, there is no cloud sync and no telemetry, so the files that hold your cluster access are not copied to a service you do not control.
We also think the manager you use every day should disappear into the background. A Rust architecture gives low RAM use and fast startup, and one interface for multiple clusters, SSH tunnels, logs, metrics and YAML means fewer places for a credential to end up.
Our advice is the same as the Kubernetes documentation's: one narrow identity per job, short-lived tokens, and a tool that keeps the files where you can see them.
Margus
FAQ
Can I create a kubeconfig from a service account without a secret?
Yes. Run kubectl create token <name> -n <namespace> and place the result in the user entry of a new kubeconfig. Since v1.24, long-lived token secrets are no longer created automatically, so this is the standard route on current clusters.
How long should a service account token last?
As short as your workflow allows. For a pipeline, one hour or the length of the run is common, and the job can request a fresh token each time. The cluster may limit the maximum lifetime you can request.
How do I revoke a service account kubeconfig?
Delete the ServiceAccount, or delete its RoleBinding to remove its permissions immediately. Deleting the account invalidates tokens tied to it. Because the identity is dedicated, no other user or automation loses access.
What is the difference between a service account kubeconfig and the token inside a Pod?
The Pod token is mounted automatically for processes inside the cluster, and it rotates through the TokenRequest API. A service account kubeconfig is a file you build for use outside the cluster, so you carry the token yourself and must store it with care.
What should I look for in a kubeconfig manager?
Look for local credential storage, support for credential plugins, clear context switching and multi-cluster access. Kubezilla Desktop stores kubeconfigs securely on your machine and connects to multiple clusters, including through SSH tunnels.
Sources
- Service Accounts | Kubernetes
- Managing Service Accounts | Kubernetes
- Configure Access to Multiple Clusters | Kubernetes
- Configure Access to Multiple Clusters | Kubernetes v1.34
- Access your workload cluster API via automation
- Creating Restricted and Revocable Kubeconfigs
