How to create a kubectl config file for serviceaccount
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Creating a kubectl configuration file for a service account is an important task for Kubernetes administrators who wish to grant applications, scripts, or users specific access to cluster resources. This process involves generating a kubeconfig file that specifies details like API server endpoints and authentication credentials. Here's a step-by-step guide to accomplish this.
Prerequisites
Before you start, ensure the following:
- You have a functional Kubernetes cluster.
- You have administrative access to the cluster.
- You have
kubectlinstalled and configured on your system.
Step-by-Step Instructions
Step 1: Create a Service Account
First, you need to create a Kubernetes service account. A service account provides an identity for processes that run in a Pod.
For example, to create a service account my-serviceaccount in the default namespace, use:
Step 2: Bind the Service Account to a Role
Next, bind the service account to a specific role or cluster role to define what resources it can access.
Option A: RoleBinding
Option B: ClusterRoleBinding
Example to bind a predefined view role:
Step 3: Extract the Service Account Token
Retrieve the secret name associated with the service account:
Then, extract the token from the secret:
Step 4: Configure the Kubeconfig File
Manually configure or script the creation of a kubeconfig file. Here is an example script that does this:
Step 5: Verify Access
To ensure that the kubeconfig is correctly set up, use the following command:
You should see a list of pods that corresponds to the specified permissions.
Key Points Summary
| Step | Description |
| Create SA | Create a service account in the desired namespace. |
| Role/ClusterRole | Bind the service account to an appropriate role or cluster role for permission management. |
| Token Retrieval | Extract the token associated with the service account to be used for authentication. |
| Kubeconfig File | Manually create a kubeconfig file specifying cluster, user tokens, and server details for interaction via kubectl. |
| Access Verify | Validate access by listing resources to confirm proper configurations and permissions. |
Additional Considerations
Security Practices
- Minimal Permissions: Always assign the minimal necessary permissions when binding roles.
- Rotate Tokens: Regularly update service account tokens to enhance security.
- Namespace Isolation: Consider namespace-specific configurations to manage and isolate resources.
Scripting Automation
For automated environments, it might be beneficial to script these steps to ensure consistency and repeatability. Tools like helm or CI/CD pipelines can automate these processes as part of a deployment system.
Custom Configurations
Depending on your use case, you may need additional configurations in your kubeconfig file such as client certificates, custom API server endpoints, or specific proxy settings.
This comprehensive guide explains how to create a kubectl configuration file for a service account in Kubernetes, equipping you with the necessary knowledge to manage access to your cluster effectively.
Related reading
- How to create a local development environment for Kubernetes?
- How to create a namespace if it doesn't exists from HELM templates?
- How to create a post-init container in Kubernetes?
- How to create a ServiceMonitor for prometheus-operator?
- How to create Kafka user and consumer group for ACLs in a running cluster?
- How to create Kubernetes Namespace if it does not Exist?
- How to create multinode kubernetes clusters
- How to create one user which have access to all the namespaces except one in kubernetes

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.