Kubernetes security: RBAC, ServiceAccount, authentication, authorization, and role management
Why this lesson is crucial
Security is not a single switch: it’s a set of practices, roles, controls, and processes that work together to prevent unauthorized access and incidents. In modern environments, especially on microservices platforms and orchestrated clusters, correctly understanding and applying authentication, authorization, RBAC, and ServiceAccounts makes the difference between an exposed system and a resilient one. This lesson takes you from basic concepts to practical configurations, so you can set up robust, verifiable controls.
Learning objectives
By the end, you’ll be able to distinguish between authentication and authorization, map identities to ServiceAccounts and users, design roles according to the principle of least privilege, and implement RBAC with concrete examples. You’ll also see how to quickly verify permissions and organize role management across different teams and environments.
Authentication vs Authorization
Authentication answers the question: “Who are you?”. It’s the process by which a system verifies the identity of an entity (human user, service, workload). It can rely on certificates, OIDC tokens, passwords, or keys. Authorization, instead, answers: “What are you allowed to do?”. It determines the actions permitted on specific resources, typically by applying policies and roles.
Application and human identities
In a cluster, identities fall into two broad categories: users (operators, developers, CI/CD) and machine-to-machine identities, often embodied by ServiceAccounts. Users authenticate via external providers, client certificates, or SSO; running processes instead use identities associated with the workload, avoiding the use of hardcoded credentials.
RBAC: the model that governs permissions
RBAC (Role-Based Access Control) defines permissions through roles and bindings. A role lists which actions (verbs) are allowed on which resources and API groups. Bindings connect roles to specific subjects (users, groups, or ServiceAccounts). Granularity can be per-namespace or cluster-wide.
Key components
Role: defines permissions within a single namespace. ClusterRole: defines global permissions or ones reusable across multiple namespaces. RoleBinding: assigns a Role or ClusterRole to subjects in a namespace. ClusterRoleBinding: assigns a ClusterRole to subjects at the cluster level. The combination of these objects lets you model exactly who can do what and where.
ServiceAccount: identities for workloads
A ServiceAccount is an identity dedicated to running applications. Each Pod can run with a specific ServiceAccount; the system provides a token and the credentials needed to authenticate to the API. Separating identities per service enables accurate auditing and minimal permissions: a microservice that reads logs in the “dev” namespace should not be able to delete ConfigMaps in “prod”.
Recommended practices for ServiceAccounts
Create one ServiceAccount per service or component; don’t reuse the default one. Apply tight roles: only the verbs and resources that are strictly necessary. Avoid granting cluster-level permissions unless absolutely required. Rotate tokens periodically and use secure projection mechanisms. Associate any imagePullSecrets in a targeted way, without excessive privileges.
Role management: design and lifecycle
Role management is an ongoing process. Start with function mapping: for each team or service, define tasks, responsibilities, and domains. Design roles with minimal verbs (get, list, watch) for read-only access; add create, update, patch, delete only where indispensable. Separate administration from operations: those who view logs should not be able to modify resources. Prefer per-namespace roles to isolate environments (dev, test, prod) and reduce the risk of mistakes.
Policy as code, auditing, and review
Version roles and bindings in Git repositories, apply them with CI/CD pipelines, and submit them for review. Enable API audit logs to track who does what and when. Schedule periodic permission reviews: remove unused bindings, reduce temporary privileges, and introduce expirations for elevated access. Integrate with compliance policies and tagging: label roles and bindings by domain, environment, and owner.
Practical example: least permissions to read Pods in a namespace
Goal: create a ServiceAccount that can read Pods in the “dev” namespace and nothing else, demonstrating how to test and validate permissions.
Creating the namespace and the ServiceAccount
kubectl create namespace dev kubectl -n dev create serviceaccount reader
Defining a read-only Role
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: pod-reader namespace: dev rules: - apiGroups: [""] resources: ["pods"] verbs: ["get", "list", "watch"] - apiGroups: [""] resources: ["pods/log"] verbs: ["get"]
Apply the role:
kubectl apply -f role-pod-reader.yaml
Binding the Role to the ServiceAccount
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: read-pods namespace: dev subjects: - kind: ServiceAccount name: reader namespace: dev roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io
Apply the binding:
kubectl apply -f rolebinding-read-pods.yaml
Quick verification with can-i
If you have impersonation permissions, you can test like this:
kubectl auth can-i get pods --as system:serviceaccount:dev:reader -n dev kubectl auth can-i delete pods --as system:serviceaccount:dev:reader -n dev
The first command should answer “yes”, the second “no”, confirming the correct application of the principle of least privilege.
Using the ServiceAccount in a Pod
apiVersion: v1
kind: Pod
metadata:
name: logger
namespace: dev
spec:
serviceAccountName: reader
containers:
- name: busybox
image: busybox:1.36
command: ["sh", "-c", "sleep 3600"]
The “logger” Pod will inherit the permissions of the “reader” ServiceAccount: it will be able to read Pods and their logs in “dev”, but not modify them or operate in other namespaces.
Common mistakes to avoid
Granting cluster-admin for convenience: it’s the most dangerous shortcut. Use targeted roles and only where needed. Reusing the default ServiceAccount: it makes auditing difficult and increases risks. Lack of separation between environments: define roles per namespace or per environment. No periodic review: permissions tend to grow over time; schedule audits and cleanup. Forgetting monitoring: integrate logging and alerting on authorization events and 403 errors.
Further reading and advanced strategies
For complex environments, consider using groups and roles inheritable via ClusterRole to standardize common permissions (for example, reading Pods in all namespaces), then bind them per team with RoleBindings. Integrate authentication with OIDC providers to centralize identity, use MFA, and apply session policies. Automate permission granting and revocation with approved workflows and expirations, reducing the risk of permanent privileges.
Operational checklist
Define identities: users, groups, ServiceAccounts per service. Design minimal roles per domain and environment. Apply Role/ClusterRole and Bindings as code. Verify with kubectl auth can-i and functional tests. Monitor and log: enable audit logs and alerts on anomalies. Review periodically and reduce unused privileges.
Conclusion
A solid model of authentication, authorization, RBAC, and ServiceAccount management creates reliable, scalable security foundations. Focus on least privilege, separation of roles, and automation of the permission lifecycle: the benefits will show in fewer incidents, faster response times, and a clear audit trail.
Put it into practice right away
Apply the example in your test environment, run the checks with can-i, and then extend the roles to your real cases. If you want a review of your roles or step-by-step guidance on how to migrate to well-designed RBAC and ServiceAccounts, contact us: together we will build pragmatic, verifiable security ready to grow with your system.

Comments