How to configure a non-default serviceAccount on a deployment
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Overview
Kubernetes service accounts offer a mechanism for processes running in a pod to interact with the Kubernetes API. By default, a pod utilizes the system's default service account; however, there are situations where specifying a non-default service account is necessary. This article guides you through configuring a non-default service account on a deployment, explaining when and why it might be needed and illustrating the process with examples.
Key Concepts
Service Account
A service account in Kubernetes is an identity assigned to pods that enables them to authenticate with the Kubernetes API and perform actions based on its associated permissions.
Non-Default Service Account
While each namespace has a default service account, creating and using a non-default service account grants specific permissions tailored to the application's requirements. This approach aligns with the principle of least privilege, ensuring pods have only the necessary permissions.
Why Use a Non-Default Service Account?
- Security: Restrict a pod's permissions to only what is necessary, minimizing potential attack surfaces.
- Compliance: Enforce stringent access controls using Role-Based Access Control (RBAC) policies.
- Isolation: When different parts of an application stack require varying levels of permissions, separate service accounts enhance security and manageability.
Configuring a Non-Default Service Account
Prerequisites
Ensure you have a running Kubernetes cluster and appropriate access permissions to create resources such as service accounts, roles, and role bindings.
Steps
- Create a Service AccountExecute the following command to create a non-default service account named
custom-sa:
- Define PermissionsUse RBAC to define what this service account can do. For instance, a role that allows reading pods:
Apply the role using:
- Bind the Role to the Service AccountCreate a RoleBinding to associate the role with the
custom-saservice account:
Apply the role binding using:
- Update the DeploymentModify the deployment manifest to specify the
custom-saservice account:
Apply the changes with:
Verification
To confirm that the pods are using the specified service account, run:
The output should display custom-sa, indicating successful configuration.
Summary Table
| Step | Description | Commands/Configurations |
| 1 | Create a service account | kubectl create serviceaccount custom-sa |
| 2 | Define role permissions | Role yaml with resources: pods, verbs: get, watch, list |
| 3 | Bind role to service account | RoleBinding yaml associating pod-reader to custom-sa |
| 4 | Update deployment to use the service account | Modify deployment.yaml to include
serviceAccountName: custom-sa |
| 5 | Verify service account usage | kubectl get pods --output=jsonpath='{..spec.serviceAccountName}' |
Additional Considerations
- Namespace Isolation: Ensure that the service account and related roles are created in the correct namespace, as permissions are namespace-specific unless using ClusterRoles.
- Secrets and ConfigMaps: If your application requires access to these resources, update the role accordingly.
- Audit and Logging: Implement checks to log API requests made by the service account for compliance and auditing purposes.
Conclusion
Configuring a non-default service account enhances your Kubernetes deployment's security posture by adhering to the principle of least privilege. This guide provides a foundational understanding and actionable steps to achieve a secure and compliant setup, tailored to specific organizational needs.
Related reading
- How to configure Apache Tika in a kube environment to obtain maximum throughput when parsing a massive number of documents?
- How to configure custom themes for keycloak on kubernetes
- How to configure external IP address of minikube dashboard?
- How to configure fluentd daemonset for RBAC
- How to configure Jetty in spring-boot easily?
- How to configure log-driver in kubernetes pods file?
- How to configure ingress gateway in istio?
- How to configure Ingress request timeouts on GKE

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.