Site icon Cloudlets

Securing your Kubernetes Cluster with RBAC

RBAC is quickly becoming best-practice for protecting your Kubernetes cluster. With RBAC, it is possible to arrange access control to an advanced level in an orderly manner. Since Kubernetes 1.8 it is possible to use Role Based Access Control. Many providers of Managed Kubernetes Clusters have activated RBAC by default. Also at Cloudlets, RBAC is activated at Kubernetes by default. To make a good start with Kubernetes, it is crucial that you understand how RBAC works, why it is important to use RBAC and how to use it in practice.

Cloudlets make Kubernetes accessible to a broader audience. We do this by making it easy to setup a Kubernetes cluster, by offering useful tools that help you manage Kubernetes and by providing excellent support. We are aware that in our ambitions to making Kubernetes more accessible,  we should under no circumstances compromise on security. That is why we will explain RBAC in easy-to-understand language.

What is RBAC for Kubernetes?

With Role Based Access Control, RBAC for short, you can manage permissions for users and applications to perform certain actions in your cluster. Before the introduction of RBAC, it was impossible to give users or applications limited access to your cluster. Either full access or no access at all were the only options. In many cases, it is desirable to work with roles. Every user who has assigned a certain role can perform the tasks that belong to that role. You can give access to your entire cluster or to namespaces in your cluster by means of a role. In addition, you define in a role which form of access you want to give: create, read, update, delete.

The use of RBAC is increasingly becoming the standard. Not only in Kubernetes, but also in many other areas, this form of access control is applied.

The basic concepts of RBAC

To work with RBAC it is important that you know the following basic concepts:

Default ClusterRoles

If you have not previously worked with RBAC, implementing the right rules quickly becomes a complex and time-consuming activity. In order to be able to start the safety of your cluster relatively quickly and easily, Kubernetes comes with a different standard ClusterRole. It concerns the following roles that all control access to your entire cluster:

The above is a quick representation of the rights per standard role. Do you want to know all the details? You can get them via kubectl with the command kubectl describe cluster rolename.

Using RBAC with Kubectl & .yaml files

When you’re used to assign rights to specific users or accounts, you’ll have to change your way of working. When using RBAC, permissions are assigned to roles. These roles, in turn, apply to specific users or service accounts. This role defines all permissions within a cluster or namespace.

Assign clusterrole to a user or serviceaccount

Kubernetes comes with some pre-defined roles by default, as shown in the chapter above. If you want to assign permissions to view your cluster, you can do so by applying an existing view-role to a user or service account:

$ kubectl create rolebinding rolename-view --clusterrole view --user username

In the above command, we create a role binding with the name role name-view. That role binding applies the view-role to username. Instead of view you can choose a different (cluster) role. It is recommanded to give the role binding a recognizable name so you can easily adjust or withdraw the role at a later time.

Creating a new role

Do you want to create a role that gives access within a namespace? This can be done by executing a .yaml-file that specifies all characteristics that role should have. Below you’ll find an example of a .yml file that creates the role pod-reader is created. The pod-reader role gives access to the namespace default. With the role you get the permissions to get, watch and list resources in the cluster.

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: default
  name: pod-reader
rules:
- apiGroups: [""] # "" indicates the core API group
  resources: ["pods"]
  verbs: ["get", "watch", "list"]