Kubernetes RBAC basics: least-privilege access for a service account
RBAC controls who may do what in a cluster with Roles, ClusterRoles and bindings. This guide grants a CI service account deploy rights in one namespace and verifies them.
Not checked by a trusted editor yet Last edited Outdated?
Role-based access control (RBAC) decides whether a request to the API server is allowed. Permissions are purely additive: there are no deny rules, so anything not explicitly allowed is refused.
The four objects
- Role: a list of rules (API groups, resources, verbs) that applies inside one namespace.
- ClusterRole: the same, but cluster-wide, or reusable in many namespaces. Also needed for cluster-scoped resources such as nodes.
- RoleBinding: grants a Role or a ClusterRole to subjects (users, groups, service accounts) within one namespace.
- ClusterRoleBinding: grants a ClusterRole across the whole cluster. Use sparingly.
Subjects come in three kinds. Users and groups come from your authentication setup (certificates, OIDC); Kubernetes has no user objects. Service accounts are namespaced objects meant for software, and their username has the form system:serviceaccount:<namespace>:<name>.
Step 1: create a service account
kubectl create serviceaccount ci-deployer -n shop
Step 2: define a Role with only what is needed
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: deployer
namespace: shop
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
- apiGroups: [""]
resources: ["pods", "services", "configmaps"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["pods/log"]
verbs: ["get"]
apiGroups: [""] is the core group (Pods, Services, ConfigMaps, Secrets). Subresources such as logs are named resource/subresource. This Role deliberately leaves out Secrets and delete.
Step 3: bind it
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: ci-deployer
namespace: shop
subjects:
- kind: ServiceAccount
name: ci-deployer
namespace: shop
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: deployer
kubectl apply -f role.yaml -f rolebinding.yaml
You cannot change the roleRef of an existing binding; delete and recreate the binding instead. For quick grants, kubectl can create bindings to the built-in ClusterRoles view, edit and admin, which then apply only inside the binding’s namespace:
kubectl create rolebinding ci-view --clusterrole=view --serviceaccount=shop:ci-deployer --namespace=shop
Step 4: verify with kubectl auth can-i
kubectl auth can-i patch deployments -n shop --as=system:serviceaccount:shop:ci-deployer
kubectl auth can-i get secrets -n shop --as=system:serviceaccount:shop:ci-deployer
kubectl auth can-i delete deployments -n shop --as=system:serviceaccount:shop:ci-deployer
kubectl auth can-i --list -n shop --as=system:serviceaccount:shop:ci-deployer
The first should print yes and the others no. Impersonation with --as itself requires permission, which cluster administrators normally have.
Traps to avoid
- Anyone who can create Pods in a namespace can mount any Secret and service account token in it. Treat
create pods(and create on Deployments, Jobs and similar) as close toget secrets. - The
escalate,bindandimpersonateverbs and wildcards (*) let a subject give itself more rights. Do not grant them to automation. - Avoid ClusterRoleBindings to
cluster-adminfor applications or CI. Bind namespaced Roles instead. - Workloads that do not call the API should not get a token: set
automountServiceAccountToken: falsein the Pod spec or on the service account.
When a request is denied, the API server answers with a Forbidden error naming the user, verb, resource and namespace. Feed exactly those values into kubectl auth can-i to confirm the fix.
Official documentation
See also
- 0131 Kubernetes architecture: control plane, nodes, kubelet and etcd A Kubernetes cluster is a control plane that stores and decides the desired state, plus worker nodes that run your containers. This… Beginner · Concept · Linux Article Concepts 15 Oct 2026
- 0132 Pods, ReplicaSets and Deployments explained A Pod runs one or more containers together, a ReplicaSet keeps a fixed number of identical Pods alive, and a Deployment manages… Beginner · Concept · Linux Article Concepts · Workloads & services 15 Oct 2026
- 0134 ConfigMaps and Secrets: configuration outside the image ConfigMaps hold plain settings and Secrets hold sensitive values, injected into Pods as environment variables or files. Secrets are only base64-encoded, not… Intermediate · Concept · Linux Article Config, secrets & storage 15 Oct 2026
- 0136 Persistent storage in Kubernetes: PersistentVolumes, claims and StorageClasses How to give a Pod storage that survives restarts: request it with a PersistentVolumeClaim, let a StorageClass provision it, and mount it.… Intermediate · How-to · Linux Article Config, secrets & storage · Workloads & services 15 Oct 2026
- 0140 kubectl cheat sheet: the commands you use every day A compact reference of everyday kubectl commands: contexts and namespaces, listing and inspecting objects, logs and exec, applying changes, rollouts, scaling and… Beginner · Reference · Any system Article Concepts · Troubleshooting 15 Oct 2026
- 0149 OpenShift vs Kubernetes vs OKD: what OpenShift adds OpenShift is Red Hat's Kubernetes platform and OKD is its community distribution. Both add projects, routes, builds, image streams, Operators and stricter… Beginner · Concept · Linux Article Concepts · Concepts 15 Oct 2026
Repos in the wild
- kubernetes/kubernetes kubernetes/kubernetes The main source repository of Kubernetes: the API server, scheduler, controller manager, kubelet, kube-proxy and kubectl, plus release notes and issue tracking. GitHub
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Commitpedia +3,870 chars