Give a team just enough access with RBAC
In this task you'll let a team manage its own apps in one namespace and nothing else.
Kubernetes RBAC has four objects. A Role lists allowed verbs (get, list, watch, create, update, patch, delete) on resources in one namespace; a ClusterRole does the same cluster-wide or for reuse. A RoleBinding gives a Role (or a ClusterRole, limited to one namespace) to users, groups or service accounts; a ClusterRoleBinding gives it everywhere.
Start small and add what's missing: errors say exactly which verb and resource were refused. kubectl auth can-i checks any identity's permissions before you hand them out. Watch for powerful verbs that look harmless: create on pods lets someone run anything with the namespace's service accounts, and get on secrets reads every password in the namespace.
Example. Quayhurst Health gives its appointments team the built-in edit ClusterRole through a RoleBinding in the appointments namespace only. The team can deploy and debug its apps, but can't read other teams' namespaces or change RBAC itself.
Before you start
You need: Docker Desktop, kind and kubectl on your computer (all free), and a cluster from Run a practice cluster on your computer. Time: 30 minutes.
Where to look
- Terminal
- kubectl
Steps
- Create a namespace
appointmentsand a service accountdeployerin it. - Create a Role allowing get, list, watch, create, update, patch and delete on deployments and pods (see Commands).
- Bind it to the service account with a RoleBinding.
- Check with
kubectl auth can-i create deployments -n appointments --as=system:serviceaccount:appointments:deployer(yes) and the same indefault(no). - Check
get secretsis refused, and that reading logs is refused too:kubectl auth can-i get pods --subresource=logsays no, because pods/log is a separate resource.Don't writeget pods/log: kubectl reads that as a pod named log and can wrongly answer yes. - Add a second Role with get on
pods/log, bind it, check the subresource again (yes), then list everything withkubectl auth can-i --list. - Swap your Role for the built-in
editClusterRole in a RoleBinding and compare the lists.
Commands
kubectl create namespace appointments
kubectl create serviceaccount deployer -n appointments
kubectl create role app-deployer -n appointments --verb=get,list,watch,create,update,patch,delete --resource=deployments,pods
kubectl create rolebinding app-deployer -n appointments --role=app-deployer --serviceaccount=appointments:deployer
kubectl auth can-i create deployments -n appointments --as=system:serviceaccount:appointments:deployer
kubectl auth can-i get secrets -n appointments --as=system:serviceaccount:appointments:deployer
# Subresources: use --subresource (get pods/log would mean a pod named log)
kubectl auth can-i get pods --subresource=log -n appointments --as=system:serviceaccount:appointments:deployer
kubectl create role log-reader -n appointments --verb=get --resource=pods/log
kubectl create rolebinding log-reader -n appointments --role=log-reader --serviceaccount=appointments:deployer
kubectl auth can-i --list -n appointments --as=system:serviceaccount:appointments:deployer
Watch out for
- Cost
- Free.
- Security
- Avoid cluster-admin for people and pipelines, avoid wildcards (*), and review who can read secrets or create pods.
- Common mistakes
- Using a ClusterRoleBinding when a RoleBinding would do, forgetting that pods/log and pods/exec are separate resources, and checking them with
can-i get pods/loginstead of--subresource=log.
Clean up
kubectl delete namespace appointments (it removes the Role, binding and service account).
Words to know
- Role
- A list of allowed actions on resources in one namespace.
- RoleBinding
- Gives a Role or ClusterRole to people or service accounts in one namespace.
- Service account
- An identity for software running in pods.
- Verb
- An action such as get, list, create or delete.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands OnKeep going
- Kubernetes roadmap: the skills behind the CKA and CKADExplained
- Kubernetes security basics: RBAC, pod security and secretsExplained
- Keep data with volumes, claims and a StatefulSetPrevious in Kubernetes skills
- Configure an app with ConfigMaps and SecretsNext in Kubernetes skills
- Containers and KubernetesAll pages in this topic