How to control access to Managed Kubernetes clusters with RBAC
When more than one person works on an UpCloud Managed Kubernetes cluster, each of them needs the right level of access. Someone on the support team may only need to view resources, while a development team needs to deploy to its own namespace without touching anyone else's. Kubernetes handles this with role-based access control (RBAC). This guide walks you through the built-in roles, granting access for one namespace or the whole cluster, writing a role of your own, checking what someone can do, and taking access away.
The examples are for people who sign in with OIDC cluster authentication, where your identity provider supplies a username such as oidc:[email protected]. RBAC works the same way for the service accounts described in Creating Restricted and Revocable Kubeconfigs.
Before you start
You need kubectl and the cluster's admin kubeconfig, which you can download from the cluster's overview in the Control Panel. The admin kubeconfig has full access to the cluster, so you can create the roles and bindings in this guide with it.
How access works
Signing in only proves who someone is. A user who has just signed in can't do anything until you give them access:
kubectl auth can-i list pods --as=oidc:[email protected]noThe --as option runs the check as another user without needing their credentials, so you can try out a binding before the person signs in for the first time. It works for anyone allowed to impersonate other users, and the admin kubeconfig has that permission. To include a group the user belongs to, add --as-group.
Access in RBAC comes in two parts. A role is a list of what's allowed, such as "list pods" or "update deployments". A binding gives a role to users or groups. Both come in two scopes:
- A Role and RoleBinding apply in a single namespace.
- A ClusterRole and ClusterRoleBinding apply across the whole cluster.
A RoleBinding can also point to a ClusterRole, which gives that role's permissions in one namespace only. This is the usual way to give a team access to its own namespace.
Use the built-in roles
Every cluster comes with four ClusterRoles that cover most needs:
| Role | What it allows |
|---|---|
view | Read most resources, but not secrets, and change nothing |
edit | Create, change and delete most resources, but not roles or bindings |
admin | Everything edit allows, plus managing roles and bindings in a namespace |
cluster-admin | Everything, everywhere |
You can list them with:
kubectl get clusterroles view edit admin cluster-adminGive each person the smallest role that covers what they need, and keep cluster-admin for the few people who run the cluster.
Give read-only access to the whole cluster
To let someone look at everything in the cluster, bind view to them with a ClusterRoleBinding:
kubectl create clusterrolebinding alice-view \
--clusterrole=view \
--user=oidc:[email protected]Alice can now list pods in every namespace, but she can't delete them or read secrets:
kubectl auth can-i list pods --all-namespaces --as=oidc:[email protected]
kubectl auth can-i delete pods --all-namespaces --as=oidc:[email protected]
kubectl auth can-i get secrets --all-namespaces --as=oidc:[email protected]yes
no
noGive a team access to its own namespace
To let a team deploy to its own namespace, bind edit to the team's group with a RoleBinding in that namespace. Binding a group rather than each person means that when someone joins or leaves the team at your identity provider, their access in the cluster changes with it, and you don't need to update the binding.
kubectl create namespace team-a
kubectl create rolebinding team-a-edit \
--clusterrole=edit \
--group=oidc:team-a \
--namespace=team-aA member of the team can now deploy to team-a, but not to any other namespace, and they can't change who has access:
kubectl auth can-i create deployments --namespace=team-a --as=oidc:[email protected] --as-group=oidc:team-a
kubectl auth can-i create deployments --namespace=default --as=oidc:[email protected] --as-group=oidc:team-a
kubectl auth can-i create rolebindings --namespace=team-a --as=oidc:[email protected] --as-group=oidc:team-ayes
no
noIf you want a team lead to manage access within the namespace as well, bind admin to them instead of edit.
Create a role of your own
When none of the built-in roles fits, write your own. This Role lets someone deploy and update applications in team-a and look at their pods and logs, but not delete anything. Save it as deployer-role.yaml:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: deployer
namespace: team-a
rules:
- apiGroups: ["apps"]
resources: ["deployments", "replicasets"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
- apiGroups: [""]
resources: ["pods", "pods/log", "services", "configmaps"]
verbs: ["get", "list", "watch"]Then create the role and bind it:
kubectl apply -f deployer-role.yaml
kubectl create rolebinding carol-deployer \
--role=deployer \
--user=oidc:[email protected] \
--namespace=team-aCarol can update deployments in team-a, but she can't delete them:
kubectl delete deployment web --namespace=team-a --as=oidc:[email protected]Error from server (Forbidden): deployments.apps "web" is forbidden: User "oidc:[email protected]" cannot delete resource "deployments" in API group "apps" in the namespace "team-a"Check who can do what
To see everything a user can do in a namespace, list their permissions:
kubectl auth can-i --list --namespace=team-a --as=oidc:[email protected]Resources Non-Resource URLs Resource Names Verbs
...
deployments.apps [] [] [get list watch create update patch]
replicasets.apps [] [] [get list watch create update patch]
configmaps [] [] [get list watch]
pods/log [] [] [get list watch]
pods [] [] [get list watch]
services [] [] [get list watch]
...To see who has access to a namespace, list its bindings. The -o wide output shows the users and groups each binding applies to:
kubectl get rolebindings --namespace=team-a -o wideNAME ROLE AGE USERS GROUPS SERVICEACCOUNTS
carol-deployer Role/deployer 4s oidc:[email protected]
team-a-edit ClusterRole/edit 17s oidc:team-aUse kubectl get clusterrolebindings -o wide in the same way for access across the whole cluster.
If a signed-in user gets Forbidden for something you expected them to have access to, ask them to run kubectl auth whoami. It shows the exact username and groups the cluster sees. These must match the names in the binding, including the prefix.
Remove access
To take access away, delete the binding. It takes effect straight away:
kubectl delete clusterrolebinding alice-view
kubectl auth can-i list pods --all-namespaces --as=oidc:[email protected]clusterrolebinding.rbac.authorization.k8s.io "alice-view" deleted
noWhen someone leaves, delete their bindings and disable them at your identity provider. Deleting the binding is the quickest way to cut their access to the cluster. Disabling them at your identity provider stops them from signing in again, but a token they already have keeps working until it expires.
Next steps
- OIDC cluster authentication on Managed Kubernetes explains how to set up an identity provider so people can sign in with their own accounts.
- Creating Restricted and Revocable Kubeconfigs shows how to give tools and collaborators a kubeconfig of their own with a service account.
- The Kubernetes documentation on Using RBAC Authorization covers every option in detail.
Kubernetes is a registered trademark of The Linux Foundation.