peter (admin)   -  



Kubernetes Access Control with RBAC

In this section, we will learn the basics of access control in Kubernetes and understand how permissions determine who can view, create, modify, or manage cluster resources.

Kubernetes uses Role-Based Access Control (RBAC) to define permissions in a structured and secure way.

The main idea is simple:

Give users, applications, and services only the permissions they actually need.

This is known as the principle of least privilege.


Create a Development Namespace

First, we create a namespace called:

dev

A namespace provides logical separation inside a Kubernetes cluster.

You can think of a namespace as a separate room inside the same building. Each room can contain its own applications and resources without interfering with others.

Create the namespace with:

kubectl create namespace dev

The dev namespace will be used for development workloads.

This helps keep the cluster organized and separates development resources from other environments.


Create a Service Account

Next, we create a service account called:

dev-reader

A ServiceAccount is an identity that can be used by Pods, applications, and other processes running inside Kubernetes.

Create it with:

kubectl create serviceaccount dev-reader -n dev

The -n dev option tells Kubernetes to create the service account inside the dev namespace.

We can verify it with:

kubectl get serviceaccounts -n dev

The service account itself does not automatically have permission to read or modify resources.

We still need to define those permissions.


Create a Role

Next, we create a Role called:

pod-reader

A Role defines which actions are permitted inside a particular namespace.

In this example, we want the dev-reader service account to be able to read Pods but not modify them.

We can create the Role with:

kubectl create role pod-reader 
  --verb=get 
  --verb=list 
  --verb=watch 
  --resource=pods 
  -n dev

This Role permits three actions:

get
list
watch

Understanding get, list, and watch

get

The get permission allows a specific resource to be viewed.

For example:

kubectl get pod nginx -n dev

list

The list permission allows multiple resources to be displayed.

For example:

kubectl get pods -n dev

watch

The watch permission allows a client to monitor resources for changes.

Together, these permissions provide read-only access to Pods.

The Role does not include permissions such as:

create
update
patch
delete

Therefore, someone using this Role cannot create, modify, or remove Pods.


Understanding the Role

Our configuration now looks like this:

Namespace: dev
     |
     +---- ServiceAccount: dev-reader
     |
     +---- Role: pod-reader
              |
              +---- get pods
              +---- list pods
              +---- watch pods

At this stage, the ServiceAccount and Role exist separately.

The ServiceAccount still cannot use the Role.

To connect them, we need a RoleBinding.


Create a RoleBinding

A RoleBinding assigns permissions from a Role to a user, group, or ServiceAccount.

Create the RoleBinding with:

kubectl create rolebinding dev-reader-binding 
  --role=pod-reader 
  --serviceaccount=dev:dev-reader 
  -n dev

Let's break this down.

The RoleBinding is called:

dev-reader-binding

The Role being assigned is:

pod-reader

The identity receiving the permissions is:

dev:dev-reader

This means:

Namespace:       dev
ServiceAccount:  dev-reader

How the RBAC Objects Work Together

The complete relationship is:

ServiceAccount
   dev-reader
       |
       v
  RoleBinding
dev-reader-binding
       |
       v
     Role
  pod-reader
       |
       +---- get pods
       +---- list pods
       +---- watch pods

The Role answers:

What is allowed?

The ServiceAccount answers:

Who is asking?

The RoleBinding connects the two.

Without the RoleBinding, the Role would exist but the ServiceAccount would not receive its permissions.


Verify the Role

We can check the Role with:

kubectl get role pod-reader -n dev

For more information:

kubectl describe role pod-reader -n dev

This allows us to see exactly which resources and actions are permitted.


Verify the RoleBinding

Check the RoleBinding with:

kubectl get rolebinding dev-reader-binding -n dev

For more detailed information:

kubectl describe rolebinding dev-reader-binding -n dev

This shows which Role is being used and which ServiceAccount receives the permissions.


Test the Permissions

Now we can verify that the RBAC configuration is working correctly.

kubectl provides a useful command:

kubectl auth can-i

This asks Kubernetes whether a particular identity is allowed to perform a specific action.


Can the Service Account Read Pods?

Run:

kubectl auth can-i get pods 
  -n dev 
  --as=system:serviceaccount:dev:dev-reader

This tells Kubernetes:

Pretend I am the dev-reader ServiceAccount in the dev namespace. Am I allowed to get Pods?

The expected response is:

yes

This confirms that the Role and RoleBinding are working correctly.


Understanding --as

The important part is:

--as=system:serviceaccount:dev:dev-reader

The standard Kubernetes ServiceAccount identity format is:

system:serviceaccount::

In our example:

system:serviceaccount:dev:dev-reader

means:

Namespace:       dev
ServiceAccount:  dev-reader

kubectl is impersonating that ServiceAccount for the permission check.


Test a Restricted Action

Now let's verify that the ServiceAccount cannot perform an action that was not included in the Role.

Run:

kubectl auth can-i delete pods 
  -n dev 
  --as=system:serviceaccount:dev:dev-reader

The expected response is:

no

This confirms that the ServiceAccount has read access but does not have permission to delete Pods.


Why This Is Important

We have now demonstrated both sides of access control.

The ServiceAccount can:

get pods
list pods
watch pods

but cannot:

delete pods
create pods
update pods

unless those permissions are explicitly added.

This is an important Kubernetes security principle.

Instead of granting broad administrative access, we grant only the specific permissions required.


RBAC Permission Flow

The permission-checking process can be visualized as:

ServiceAccount
     |
     v
dev-reader
     |
     v
RoleBinding
     |
     v
pod-reader Role
     |
     +---- get
     +---- list
     +---- watch
     |
     v
Kubernetes API Server
     |
     +---- Allowed action ----> YES
     |
     +---- Missing action ----> NO

When the ServiceAccount makes a request, Kubernetes checks whether the required permission has been granted.


Namespace-Scoped Permissions

One important thing to understand is that a Role is namespace-scoped.

Our Role exists inside:

dev

Therefore, its permissions apply to resources in the dev namespace.

For example, our permission test:

kubectl auth can-i get pods 
  -n dev 
  --as=system:serviceaccount:dev:dev-reader

returns:

yes

But if we test another namespace:

kubectl auth can-i get pods 
  -n production 
  --as=system:serviceaccount:dev:dev-reader

we would normally expect:

no

unless additional permissions have been configured.

This allows access to be restricted to specific environments.


Role vs ClusterRole

Kubernetes provides two main types of RBAC role.

Role

A Role applies to a specific namespace.

Example:

dev namespace
     |
     v
pod-reader Role

ClusterRole

A ClusterRole can define permissions that apply across the cluster or to cluster-scoped resources.

Examples of cluster-scoped resources include:

nodes
persistentvolumes
namespaces

A ClusterRole can also be bound within individual namespaces depending on how it is used.

For this example, a normal Role is appropriate because we only want Pod permissions inside the dev namespace.


The Principle of Least Privilege

Our example demonstrates an important security principle called:

Least privilege

The idea is that an account should receive only the permissions required for its job.

If the purpose of dev-reader is simply to inspect Pods, there is no reason to give it permission to:

  • Delete Pods

  • Create Deployments

  • Modify Services

  • Manage Secrets

  • Delete Namespaces

Limiting permissions reduces the impact of mistakes, compromised applications, or unauthorized access.


A Simple Library Analogy

A useful way to understand Kubernetes RBAC is to imagine a library.

The ServiceAccount represents a library user.

The Role describes what that user is allowed to do.

For example:

Read books:        YES
View catalogue:    YES
Destroy books:     NO
Remove books:      NO
Change records:    NO

The RoleBinding is like assigning the library membership permissions to that particular user.

The relationship becomes:

Library User
     |
     v
Membership Assignment
     |
     v
Reader Permissions
     |
     +---- Read books
     +---- View books
     |
     +---- Cannot remove books

Kubernetes RBAC works in a similar way.


Complete Example

The commands used in this example are:

Create the namespace

kubectl create namespace dev

Create the ServiceAccount

kubectl create serviceaccount dev-reader -n dev

Create the Role

kubectl create role pod-reader 
  --verb=get 
  --verb=list 
  --verb=watch 
  --resource=pods 
  -n dev

Create the RoleBinding

kubectl create rolebinding dev-reader-binding 
  --role=pod-reader 
  --serviceaccount=dev:dev-reader 
  -n dev

Test read access

kubectl auth can-i get pods 
  -n dev 
  --as=system:serviceaccount:dev:dev-reader

Expected:

yes

Test delete access

kubectl auth can-i delete pods 
  -n dev 
  --as=system:serviceaccount:dev:dev-reader

Expected:

no

Understanding the Complete RBAC Setup

The complete configuration can be represented as:

Kubernetes Cluster
       |
       v
Namespace: dev
       |
       +--------------------------+
       |                          |
       v                          v
ServiceAccount                  Role
dev-reader                    pod-reader
                                  |
                                  +---- get pods
                                  +---- list pods
                                  +---- watch pods
       |                          |
       +------------+-------------+
                    |
                    v
              RoleBinding
           dev-reader-binding
                    |
                    v
           Permissions Applied

Access Control Summary

In this exercise, we created a simple but realistic Kubernetes access-control configuration.

First, we created a namespace called dev to isolate our development resources.

Next, we created a ServiceAccount called dev-reader to represent an application or process requiring access to Kubernetes.

We then created the pod-reader Role, which permits only:

get
list
watch

operations on Pods.

The Role was connected to the ServiceAccount using a RoleBinding.

Finally, we verified the configuration using:

kubectl auth can-i

Kubernetes returned:

yes

when we checked whether the ServiceAccount could read Pods, and:

no

when we checked whether it could delete them.

This confirms that Kubernetes is enforcing the permissions exactly as configured.

The key idea to remember is:

A Role defines what can be done, a ServiceAccount identifies who is doing it, and a RoleBinding connects the two.

This is the foundation of Role-Based Access Control in Kubernetes and an essential part of keeping Kubernetes clusters secure.

 


Comments (0)

There are no comments yet. Be the first to comment.

Log in or register to leave a comment. all comments are subject to admin approval