DEV Community

CliffordIsaboke
CliffordIsaboke

Posted on

Using RBAC in Kubernetes: Role-Based Access Control Demystified

Kubernetes is a powerful system—but with great power comes great responsibility. As clusters grow and more users or services interact with them, access control becomes a critical aspect of your security posture.

This is where RBAC (Role-Based Access Control) comes into play. In this article, we’ll demystify Kubernetes RBAC, explain how it works, and walk through practical examples to help you implement it effectively.

What Is RBAC in Kubernetes?
RBAC is a method of regulating access to Kubernetes resources based on the roles of individual users or service accounts. Introduced as a built-in feature in Kubernetes 1.6, it allows you to:

  • Grant read-only, read-write, or admin access to specific resources.
  • Limit users or services to namespaces or cluster-wide resources.
  • Enforce the principle of least privilege.

RBAC Key Components
There are four main components in Kubernetes RBAC:

  • Role:-Defines a set of permissions within a namespace.
  • ClusterRole: Like Role, but applicable cluster-wide.
  • RoleBinding: Grants a Role to a user/service account within a namespace.
  • ClusterRoleBinding: Grants a ClusterRole across the entire cluster.

How RBAC Works: A Simple Flow

-You define a Role or ClusterRole with a list of allowed actions.

  • You bind that role to a user, group, or service account using a RoleBinding or ClusterRoleBinding.
  • Kubernetes enforces these permissions every time the subject tries to access the API.

Example 1: Read-Only Access to Pods in a Namespace

Step 1: Create a Role
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: dev
name: pod-reader
rules:

  • apiGroups: [""] resources: ["pods"] verbs: ["get", "list", "watch"]

Step 2: Bind the Role to a User
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods-binding
namespace: dev
subjects:

  • kind: User name: isaboke@example.com apiGroup: rbac.authorization.k8s.io roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io

This setup allows the user isaboke@example.com to list, get, and watch pods in the dev namespace only.

Example 2: Cluster-Wide Admin Access for a Service Account

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: cluster-admin
rules:

  • apiGroups: [""] resources: [""] verbs: ["*"]

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-binding
subjects:

  • kind: ServiceAccount name: my-service-account namespace: kube-system roleRef: kind: ClusterRole name: cluster-admin apiGroup: rbac.authorization.k8s.io

Use this sparingly. Granting cluster-wide permissions to service accounts should be tightly controlled.

Test Your RBAC Rules

Use the kubectl auth can-i command to verify if a user or service account has the necessary permissions:

kubectl auth can-i create deployments --as=isaboke@example.com -n dev

This is a great tool for debugging access issues.

Best Practices

  • Use namespaces to isolate workloads and limit access scope.
  • Prefer Roles over ClusterRoles when possible. -Audit access logs to detect unusual access patterns. -Avoid wildcards in production RBAC rules unless absolutely necessary. -Use groups to manage access for teams more easily.

Tools That Help
rakkess – Shows what actions users can perform in your cluster.
kubeaudit – Checks RBAC configurations for misconfigurations.
OPA Gatekeeper – Enforces custom policies on RBAC and other configurations.

Conclusion
RBAC in Kubernetes is a cornerstone of cluster security and governance. While it can seem intimidating at first, understanding its components and applying real-world examples makes it far more approachable.

Start small—grant minimal permissions, test thoroughly, and scale access based on real needs. By doing this, you’ll be on your way to building a more secure and manageable Kubernetes environment.

Top comments (0)