Role(角色)是一系列权限的集合,例如一个角色可以包含读取 Pod 的权限和列出 Pod 的权限。Role 只能用来给某个特定 namespace 中的资源作鉴权,对多 namespace 和集群级的资源或者是非资源类的 API(如 /healthz)使用 ClusterRole。
# Role 示例kind:RoleapiVersion:rbac.authorization.k8s.io/v1metadata:namespace:defaultname:pod-readerrules:- apiGroups: [""] #"" indicates the core API groupresources: ["pods"]verbs: ["get","watch","list"]
# ClusterRole 示例kind:ClusterRoleapiVersion:rbac.authorization.k8s.io/v1metadata:# "namespace" omitted since ClusterRoles are not namespacedname:secret-readerrules:- apiGroups: [""]resources: ["secrets"]verbs: ["get","watch","list"]
# RoleBinding 示例(引用 Role)# This role binding allows "jane" to read pods in the "default" namespace.kind:RoleBindingapiVersion:rbac.authorization.k8s.io/v1metadata:name:read-podsnamespace:defaultsubjects:- kind:Username:janeapiGroup:rbac.authorization.k8s.ioroleRef:kind:Rolename:pod-readerapiGroup:rbac.authorization.k8s.io
# RoleBinding 示例(引用 ClusterRole)# This role binding allows "dave" to read secrets in the "development" namespace.kind:RoleBindingapiVersion:rbac.authorization.k8s.io/v1metadata:name:read-secretsnamespace:development# This only grants permissions within the "development" namespace.subjects:- kind:Username:daveapiGroup:rbac.authorization.k8s.ioroleRef:kind:ClusterRolename:secret-readerapiGroup:rbac.authorization.k8s.io
kind:ClusterRoleapiVersion:rbac.authorization.k8s.io/v1metadata:name:monitoringaggregationRule:clusterRoleSelectors: - matchLabels:rbac.example.com/aggregate-to-monitoring:"true"rules: [] # Rules are automatically filled in by the controller manager.---kind:ClusterRoleapiVersion:rbac.authorization.k8s.io/v1metadata:name:monitoring-endpointslabels:rbac.example.com/aggregate-to-monitoring:"true"# These rules will be added to the "monitoring" role.rules:- apiGroups: [""]resources: ["services","endpoints","pods"]verbs: ["get","list","watch"]