How to Set Up RBAC (Role-Based Access Control) in Kubernetes

Kubernetes RBAC controls who can perform what actions on which cluster resources — essential for any cluster with multiple users or automated systems needing scoped access. This guide covers configuring RBAC properly.

The Core RBAC Concepts

ResourcePurpose
RoleDefines permissions within a specific namespace
ClusterRoleDefines permissions cluster-wide (or reusable across namespaces)
RoleBindingGrants a Role to a specific user/group/service account within a namespace
ClusterRoleBindingGrants a ClusterRole cluster-wide

Creating a Basic Role

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: production
  name: pod-reader
rules:
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "list", "watch"]

Defines read-only access to pods within the production namespace specifically — no ability to create, modify, or delete, and no access to other resource types.

Binding the Role to a User

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods
  namespace: production
subjects:
  - kind: User
    name: jane
    apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

Creating a Role for a Service Account (Common for Automated Systems)

apiVersion: v1
kind: ServiceAccount
metadata:
  name: ci-deployer
  namespace: production
subjects:
  - kind: ServiceAccount
    name: ci-deployer
    namespace: production

Automated systems (CI/CD pipelines, for example, see How to Manage Secrets in a CI/CD Pipeline for the credential-handling side) should use scoped service accounts with specific, minimal permissions rather than broad cluster-admin access.

Applying Least Privilege Principles

See How to Implement the Principle of Least Privilege on a Linux VPS for the underlying principle — grant only the specific verbs (get, list, create, delete) and resources genuinely needed for each role's purpose, avoiding blanket wildcard permissions.

Common Predefined ClusterRoles

kubectl get clusterroles

Kubernetes ships with several predefined ClusterRoles (view, edit, admin, cluster-admin) representing common permission levels — often a reasonable starting point rather than defining every role from scratch.

Testing What a Specific User/Service Account Can Do

kubectl auth can-i create pods --as=jane --namespace=production

Verify your RBAC configuration produces the actual intended access level, rather than assuming correctness from the YAML definitions alone.

Auditing Existing RBAC Configuration

kubectl get rolebindings,clusterrolebindings -A

Periodically review existing bindings for overly broad or no-longer-needed access — RBAC configuration, like any access control system, benefits from regular audit rather than only being reviewed at initial setup.

Avoiding Overuse of cluster-admin

The predefined cluster-admin role grants essentially unrestricted access — reserve this for genuinely necessary cases (initial cluster setup, break-glass emergency access); most users and service accounts should have much more narrowly scoped permissions.

Common Errors

User reports permission denied despite an apparently correct RoleBinding — verify the RoleBinding is in the same namespace as the Role it references (for namespaced Roles), and double-check the exact resource/verb combination matches what the user is actually attempting.

Continue Reading

Browse more articles in Kubernetes & Container Orchestration.

  • kubernetes rbac setup, kubernetes role rolebinding, kubernetes service account permissions, kubectl auth can-i
  • 0 utilizatori au considerat informația utilă
Răspunsul a fost util?

Articole similare

What Is Kubernetes and When Do You Need It on a VPS?

Kubernetes is a container orchestration platform — it automates deploying, scaling, and...

How to Install a Single-Node Kubernetes Cluster with k3s

k3s is a lightweight, certified Kubernetes distribution designed to run efficiently on modest...

How to Install kubeadm and Set Up a Multi-Node Kubernetes Cluster

kubeadm is the official tool for bootstrapping a standard, full-featured Kubernetes cluster. This...

Kubernetes Pods, Deployments & Services Explained

Understanding these three core Kubernetes objects — Pods, Deployments, and Services —...

How to Expose Applications with a Kubernetes Ingress Controller (Nginx Ingress)

An Ingress lets you route external HTTP/HTTPS traffic to multiple services within your cluster...