RBAC: Role Based Access Control
Role Based Access Control is comprised of four layers:
ClusterRole
- permissions assigned to a role that apply to an entire clusterClusterRoleBinding
- binding a ClusterRole to a specific accountRole
- permissions assigned to a role that apply to a specific namespaceRoleBinding
- binding a Role to a specific accountIn order for RBAC to be applied to an nginx-ingress-controller, that controller should be assigned to a
ServiceAccount
. ThatServiceAccount
should be bound to theRole
s andClusterRole
s defined for the nginx-ingress-controller.
资源访问
object URL
/apis/<GROUP>/<VERSION>/namespace/<NAMESPACE_NAME>/<KIND>[/OBJECT_ID]
HTTP requsets verb
get
,post
,put
,delete
API requetes verb
get
,list
,create
,update
,patch
,watch
,proxy
,redirect
,delete
deletecollection
Role
- operations
- objects
角色的访问控制, 要定义以上2个 即: 有什么权限 , 要访问什么资源 默认都是拒绝的 , 所以, 我们在定义权限的时候, 定义的内容是允许权限
, 没有明确定义出来的都是拒绝
Rolebinding
- user account OR serviceaccount
- role
角色的绑定是要先有一个角色 , 之后把这个user account OR serviceaccount
绑定在Role
上 , 而这个
Role
是之前就定义好的 , 上面规定了这个角色有哪些权限
, 这样这个账号通过角色的绑定间接获得了权限
注意: 同时Rolebinding
是基于某个名称空间的 , Role
的权限只是基于某个名称空间的
示例
创建一个role
1 | kubectl create role pods-reader --verb=get,list,watch --resource=pods |
也可以创建出了定义role的模板文件, 用于以后的定义
1 | kubectl create role pods-reader --verb=get,list,watch --resource=pods --dry-run -o yaml > role-demo.yaml |
vim role-demo.yaml
1 | apiVersion: rbac.authorization.k8s.io/v1 |
查看刚刚创建的role
1 | kubectl describe role pods-reader |
创建一个rolebinding
1 | kubectl create rolebinding test-read-pods --role=pods-reader --user=test |
查看刚刚创建的rolebinding
1 | kubectl describe rolebinding test-read-pods |
切换用户查看效果
1 | kubectl config use-context test@kubernetes |
删除一个rolebinding
1 | kubectl delete rolebinding test-read-pods |
创建一个ClusterRole
vim cluserrole-demo.yaml
1 | apiVersion: rbac.authorization.k8s.io/v1 |
1 | kubectl apply -f clusterrole-demo.yaml |
创建一个clusterrolebinding
1 | kubectl create clusterrolebinding test-read-all-pods --clusterrole=cluster-reader --user=test |
##
用ClusterRoleBinding
来绑定一个ClusterRole
1 | kubectl create clusterrolebinding test-read-all-pods --clusterrole=cluster-reader --user=test --dry-run -o yaml > clusterrolebinding-demo.yaml |
1 | vim clusterrolebinding-demo.yaml |
1 | kubectl apply -f clusterrolebinding-demo.yaml |
查看刚刚创建的clusterrolebinding
1 | kubectl describe clusterrolebinding test-read-all-pods |
查看效果
1 | kubectl config use-context test@kubernetes //切换到test用户 |
查看集群中所有的clusterrole
1 | kubectl get clusterrole |
用RoleBinding
来绑定一个ClusterRole
先把刚刚创建好的ClusterRoleBinding
删除了
1 | kubectl delete clusterrolebinding test-read-all-pods |
创建一个rolebinding
1 | kubectl create rolebinding test-read-pods --clusterrole=cluster-reader --user=test |
当然也可以用文件来创建
1 | kubectl create rolebinding test-read-pods --clusterrole=cluster-reader --user=test --dry-run -o yaml > rolebinding-clusterrole.yaml |
vim rolebinding-clusterrole.yaml
1 | apiVersion: rbac.authorization.k8s.io/v1 |
1 | kubectl apply -f rolebinding-clusterrole.yaml |
查看效果
1 | kubectl config use-context test@kubernetes //切换到test用户 |