Kubernetes
kubectl
serviceaccount
config file
tutorial

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.

Practice system design

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 kubectl installed 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.

bash
kubectl create serviceaccount <serviceaccount-name> --namespace <namespace>

For example, to create a service account my-serviceaccount in the default namespace, use:

bash
kubectl create serviceaccount my-serviceaccount --namespace default

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

bash
kubectl create rolebinding <rolebinding-name> --role=<role-name> --serviceaccount=<namespace>:<serviceaccount-name> --namespace=<namespace>

Option B: ClusterRoleBinding

bash
kubectl create clusterrolebinding <clusterrolebinding-name> --clusterrole=<clusterrole-name> --serviceaccount=<namespace>:<serviceaccount-name>

Example to bind a predefined view role:

bash
kubectl create clusterrolebinding my-binding --clusterrole=view --serviceaccount=default:my-serviceaccount

Step 3: Extract the Service Account Token

Retrieve the secret name associated with the service account:

bash
SECRET_NAME=$(kubectl get sa <serviceaccount-name> --namespace <namespace> -o jsonpath='{.secrets[0].name}')

Then, extract the token from the secret:

bash
TOKEN=$(kubectl get secret $SECRET_NAME --namespace <namespace> -o jsonpath='{.data.token}' | base64 --decode)

Step 4: Configure the Kubeconfig File

Manually configure or script the creation of a kubeconfig file. Here is an example script that does this:

bash
1cat <<EOF > /path/to/kubeconfig
2apiVersion: v1
3kind: Config
4clusters:
5- cluster:
6    certificate-authority-data: $(kubectl config view --raw -o jsonpath="{.clusters[0].cluster.certificate-authority-data}")
7    server: $(kubectl config view -o jsonpath="{.clusters[0].cluster.server}")
8  name: <cluster-name>
9contexts:
10- context:
11    cluster: <cluster-name>
12    namespace: <namespace>
13    user: <user-name>
14  name: <context-name>
15current-context: <context-name>
16users:
17- name: <user-name>
18  user:
19    token: $TOKEN
20EOF

Step 5: Verify Access

To ensure that the kubeconfig is correctly set up, use the following command:

bash
KUBECONFIG=/path/to/kubeconfig kubectl get pods --namespace <namespace>

You should see a list of pods that corresponds to the specified permissions.

Key Points Summary

StepDescription
Create SACreate a service account in the desired namespace.
Role/ClusterRoleBind the service account to an appropriate role or cluster role for permission management.
Token RetrievalExtract the token associated with the service account to be used for authentication.
Kubeconfig FileManually create a kubeconfig file specifying cluster, user tokens, and server details for interaction via kubectl.
Access VerifyValidate 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.