peter (admin)   -  



 

Role-Based Access Control (RBAC)

Here we will learn the basics of RBAC in Kubernetes, which stands for Role-Based Access Control.

Using RBAC, we will create different users, assign them specific roles, and control what actions they can perform inside the cluster.

RBAC is a way to control who can do what inside a Kubernetes cluster. It helps define permissions for users, groups, or services so that not everyone has full access to everything.

In Kubernetes, RBAC plays an important role in security because it makes sure only authorized users can perform specific actions, such as creating pods, deleting resources, or viewing data.

The role of RBAC in Kubernetes is to manage access to cluster resources in a controlled and organized way. We define roles that contain permissions and then assign those roles to users.

These roles can control access at the namespace level or across the entire cluster.

For example, we can allow one user to only view pods while another user can create and delete pods.

This makes the system safer because users only get access to what they need.

Why RBAC Is Important

RBAC is important because in real environments many users work on the same cluster. Without proper access control, anyone could change or delete important resources such as pods, deployments, or services.

This could break applications and cause downtime. RBAC helps protect the cluster and keeps resources controlled and secure.

For example, imagine a team where one person is a developer and another is an administrator.

The developer may only need to view and check pods to debug issues, while the administrator needs full access to manage the cluster.

Using RBAC, we can give the developer permission to only read pods and give the administrator permission to create, update, and delete resources.

This way, both users can work safely without affecting each other's responsibilities.

Create the Namespace

Let's understand this concept practically.

For this, we are already logged into both the master node and the worker node.

To verify the cluster, run:

kubectl get nodes

Both the master and worker should be in the Ready state, which means the cluster is up and both nodes are properly connected and working.

Now create a namespace named deep:

kubectl create namespace deep

This allows us to apply RBAC rules to specific resources, such as pods, within this namespace without affecting the entire cluster.

Create the Users

Now let's create three users: user1, user2, and user3.

For the first user, generate a private key:

openssl genrsa -out user1.key 2048

This key is used to create a certificate that acts as the user's identity.

The certificate-based authentication process allows Kubernetes to identify who the user is when they try to access the cluster.

Next, create a Certificate Signing Request (CSR) for user1:

openssl req -new -key user1.key -out user1.csr -subj "/CN=user1"

The Common Name (CN) identifies the user inside the cluster.

The CSR is used to request a signed certificate from the certificate authority. This process is part of certificate-based authentication, where Kubernetes verifies users using certificates instead of usernames and passwords.

Once the certificate is approved, the user can be recognized by the cluster and RBAC rules can be applied to control what the user can do.

Sign the User Certificate

Sign the certificate request using the Kubernetes cluster's certificate authority.

The CA certificate and key are located on the master node at:

/etc/kubernetes/pki/ca.crt
/etc/kubernetes/pki/ca.key

The CA is the trusted authority that signs and verifies user identities.

A certificate can be generated using:

openssl x509 -req 
  -in user1.csr 
  -CA /etc/kubernetes/pki/ca.crt 
  -CAkey /etc/kubernetes/pki/ca.key 
  -CAcreateserial 
  -out user1.crt 
  -days 365

This creates the signed certificate user1.crt.

The certificate acts as the approved identity for the user. The certificate remains valid for the number of days specified during creation.

The certificate request contains the user's identity details. Kubernetes uses its certificate authority to verify the request and create a signed certificate.

After the certificate is created, the user can use it to access the cluster, and RBAC rules can determine what that user is allowed to do.

The same process is followed for user2 and user3. The user names are changed for each certificate, while the rest of the process remains the same.

Configure User Credentials

After creating the users, configure their credentials in Kubernetes.

For user1, provide the certificate and private key:

kubectl config set-credentials user1 
  --client-certificate=user1.crt 
  --client-key=user1.key

The same process is used for user2 and user3:

kubectl config set-credentials user2 
  --client-certificate=user2.crt 
  --client-key=user2.key

kubectl config set-credentials user3 
  --client-certificate=user3.crt 
  --client-key=user3.key

This links each user identity with Kubernetes so the system knows who the user is and can apply RBAC rules to control what actions they are allowed to perform.

Create User Contexts

Next, create a context for user1.

A context connects three things:

  • The cluster

  • The user identity

  • The namespace

For example:

kubectl config set-context user1-context 
  --cluster=kubernetes 
  --user=user1 
  --namespace=deep

This means that whenever we switch to this context, actions will be performed as user1 inside the deep namespace.

A context acts like a working profile for the user. It allows us to test RBAC rules by switching between different users and checking what they can and cannot do.

Create separate contexts for user2 and user3 in the same way:

kubectl config set-context user2-context 
  --cluster=kubernetes 
  --user=user2 
  --namespace=deep

kubectl config set-context user3-context 
  --cluster=kubernetes 
  --user=user3 
  --namespace=deep

Create the Pod Reader Role

Now that the users and their contexts have been created, we can define what these users are allowed to do.

Create a YAML file named role.yaml.

This file defines a Kubernetes Role containing permissions for resources such as pods.

The configuration is:

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

The apiVersion tells Kubernetes that we are using RBAC authorization.

The kind defines that we are creating a Role, rather than a cluster-wide ClusterRole. Therefore, this role only works inside a specific namespace.

Inside metadata, we define the namespace as deep, which means the role applies only to resources inside this namespace.

The role is named pod-reader, which describes what it is intended to do.

In the rules section, we define the permissions.

Pods belong to the core API group, so apiGroups is empty. In resources, we specify pods, meaning this role applies only to pod resources.

The verbs are:

verbs:
  - get
  - list

This means a user assigned this role can view pod details and list pods, but cannot create, delete, or modify them.

This is a read-only permission for pods inside the deep namespace and is useful when we want to allow a user to monitor pods without giving them full control.

Save the file and exit.

Apply the Pod Reader Role

Apply the role:

kubectl apply -f role.yaml

The pod-reader role should be created successfully.

This means the permission rules for accessing pods are now available in the deep namespace and ready to be assigned to users.

Assign the Pod Reader Role to User1

Now assign the pod-reader role to user1.

Create a RoleBinding named user1-binding:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: user1-binding
  namespace: deep
subjects:
  - kind: User
    name: user1
    apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

This connects the pod-reader role with user1 inside the deep namespace.

The permission attached to user1 is read-only access to pods. The user can get and list pods but cannot create, delete, or modify them.

After applying the RoleBinding:

kubectl apply -f user1-binding.yaml

The permissions are now applied, and user1 is restricted to reading pod information.

Create the Pod Admin Role

Next, create another YAML file named role-full.yaml.

This role will provide full access to pods, allowing the user to get, list, create, update, and delete pods.

The configuration is:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: pod-admin
  namespace: deep
rules:
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["*"]

The apiVersion shows that we are using RBAC authorization, and the kind defines that we are creating a Role.

This permission therefore applies only inside the specified namespace.

The namespace is deep, and the role is named pod-admin.

In the rules section, pods is specified as the resource and * is used for the verbs.

The wildcard * means all permissions are allowed, including:

  • get

  • list

  • create

  • update

  • delete

This role therefore provides full control over pods inside the deep namespace, unlike the previous role, which only allowed read-only access.

Save the file and exit.

Apply the Pod Admin Role

Apply the role:

kubectl apply -f role-full.yaml

The pod-admin role should be created successfully.

This means full permissions for managing pods in the deep namespace are now defined and ready to be assigned to a user.

Assign the Pod Admin Role to User2

Now assign the pod-admin role to user2.

Create a RoleBinding named user2-binding:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: user2-binding
  namespace: deep
subjects:
  - kind: User
    name: user2
    apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: pod-admin
  apiGroup: rbac.authorization.k8s.io

This connects the pod-admin role with user2 inside the deep namespace.

user2 will therefore have permissions to get, list, create, update, and delete pods.

Create a Test Pod

Before testing the users, switch to the admin context:

kubectl config use-context kubernetes-admin@kubernetes

This is the default administrator context with full permissions.

We use administrator access because the normal users do not yet have permission to create resources.

Now create a pod named test-pod using the NGINX image inside the deep namespace:

kubectl run test-pod 
  --image=nginx 
  --namespace=deep

Verify the pod:

kubectl get pods -n deep

The test-pod should be in the Running state.

This gives us a resource that we can use to test what each user is allowed to do based on their RBAC permissions.

Test User1

First, switch to the user1 context:

kubectl config use-context user1-context

We are now working as user1 instead of the administrator. Commands will follow the permissions assigned to user1.

Try to list the pods:

kubectl get pods -n deep

The test-pod should be displayed successfully.

This works because user1 has read access and is allowed to get and list pods.

Now try to delete the pod:

kubectl delete pod test-pod -n deep

The command should return a Forbidden error.

This means the action is not allowed. The message shows that user1 cannot delete pods in the deep namespace.

This confirms that RBAC is working correctly because user1 only has get and list permissions and does not have delete permission.

Test User2

Now test user2, who was assigned the pod-admin role.

Switch to the user2 context:

kubectl config use-context user2-context

Try to delete the same pod:

kubectl delete pod test-pod -n deep

The pod should be deleted successfully without an authorization error.

This confirms that user2 has full access to pods and can perform actions such as getting, listing, creating, updating, and deleting pods inside the deep namespace.

Test User3

Finally, test user3.

No RBAC rules have been assigned to this user, so the user should not have permission to access the pods.

Switch to the user3 context:

kubectl config use-context user3-context

Try to list the pods:

kubectl get pods -n deep

The command should return a Forbidden error.

This means user3 does not have permission even to view pods.

This demonstrates that without an RBAC rule assigned to a user, the user cannot access the protected resources.

Summary

RBAC is an important part of Kubernetes security because it controls access by allowing specific users to perform specific actions.

In this example:

  • user1 was given read-only access to pods.

  • user2 was given full access to pods.

  • user3 was not given any RBAC permissions.

Testing these users demonstrated how Kubernetes allows or denies actions based on the roles assigned to each user.

This helps prevent unauthorized changes to resources such as pods and allows access to be controlled according to each user's responsibilities.

 


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