|NO.Z.00223|——————————|CloudNative|——|KuberNetes&细粒度权限控制.V07|——|RBAC.v03|实验实例|
一、RBAC实验实例
### --- RoleBinding下subjects示例
~~~ # subjects绑定到用户
~~~ 对于名为的用户alice@example.com:
~~~ 以下示例是RoleBinding仅显示本subjects节的摘录。
subjects:
- kind: User // 绑定到一个用户,
name: "alice@example.com" // 这个用户名是alice@example.com
apiGroup: rbac.authorization.k8s.io
### --- subjects绑定到组
subjects:
- kind: Group // 绑定到前端的管理员的组
name: "frontend-admins" // 组的名称
apiGroup: rbac.authorization.k8s.io
### --- 绑定到kube-system命名空间下的默认服务账户
subjects:
- kind: ServiceAccount // 绑定到ServiceAccount
name: default
namespace: kube-system // ServiceAccount是需要指定namespace的;若是不指,就是当前的namespace
### --- 绑定所有的ServiceAccount到qa这个namespace下
~~~ 注:就是一个namespace下,有很多个ServiceAccount,
~~~ 这个namespace下的ServiceAccount就可以按组来分
~~~ 可以把qa下的这个qa下的namespace下的所有的role的这个权限都赋予给qa下的这个
~~~ namespace的ServiceAccount有多少个ServiceAccount,就有多少个权限
subjects:
- kind: Group
name: system:serviceaccounts:qa
apiGroup: rbac.authorization.k8s.io
### --- 对于“开发”名称空间中“开发”组中的所有服务帐户:
subjects:
- kind: Group
name: system:serviceaccounts:dev
apiGroup: rbac.authorization.k8s.io
namespace: development
### --- 没有指定qa,就是对namespace下的所有的ServiceAccount都生效
subjects:
- kind: Group
name: system:serviceaccounts:dev
apiGroup: rbac.authorization.k8s.io
namespace: development
### --- 对通过身份认证的用户生效
subjects:
- kind: Group
name: system:authenticated
apiGroup: rbac.authorization.k8s.io
### --- 对于没有经过身份认证的用户生效,这个一般是不给它任何权限的
subjects:
- kind: Group
name: system:unauthenticated
apiGroup: rbac.authorization.k8s.io
### --- 既包含已认证的又包含没有经过认证的所有用户生效
subjects:
- kind: Group
name: system:authenticated
apiGroup: rbac.authorization.k8s.io
- kind: Group
name: system:unauthenticated
apiGroup: rbac.authorization.k8s.io
二、对于RoleBinding的创建和授权的限制:就是RoleBinding谁去操作,一般都是管理员去操作
### --- 例如,这ClusterRole和RoleBinding将允许user-1授予其他用户admin,
~~~ edit和view角色的命名空间user-1-namespace
~~~ 注:这个是管理员来操作,若是管理员很多的话,就会创建配置
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: role-grantor // 用户role-grantor
rules:
- apiGroups: ["rbac.authorization.k8s.io"]
resources: ["rolebindings"]
verbs: ["create"]
- apiGroups: ["rbac.authorization.k8s.io"]
resources: ["clusterroles"]
verbs: ["bind"] // 对这个RoleBinding有bind的权限
# omit resourceNames to allow binding any ClusterRole
resourceNames: ["admin","edit","view"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: role-grantor-binding
namespace: user-1-namespace
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: role-grantor
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: user-1
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了