ClusterRole

  集群级别的角色资源ClusterRole资源除了能够管理与Role资源一样的许可权限之外,还可以用于集群级别组件的授权,配置方式及其在rules字段中可内嵌的字段业余Role资源类似。如下示例:

  1. kind: clusterRole
  2. apiVersion: rbac.authorization.k8s.io/v1
  3. metadata:
  4. name: nodes-reader
  5. rules:
  6. - apiGroups: [""]
  7. resources: ["nodes"]
  8. verbs: ["get", "watch","list"]
  • apiGroups: 包含了资源的API组名,支持列表格式指定的多个组,空串””标示核心组。
  • resourceNames: 规则应用的目标资源名称列表,可选,缺省时意味着指定资源类型下的所有资源。
  • resources: 规则应用的目标资源类型组成的列表,如 “deployments pods daemonsets roles”
  • verbs: 可操作列表,必选字段,其值可以有”get list create udate patch watch proxy redirect delete deletecollection”

ClusterRole是集群级别资源,它不属于名称空间,故此其配置不应该使用metadata.namespace字段。

ClusterRoleBinding

  RoleBinding也能够将主题绑定至clusterRole资源之上,但仅能赋予用户访问Rolebinding资源本身所在的名称空间内可由clusterRole赋予的权限。因此,一种常见的做法是集群管理员在集群范围内预先定义好一组访问名称空间级别资源权限的ClusterRole资源,而后在多个名称空间中多次通过RoleBinding引用他们,从而让用户分别具有不通名称空间上的资源的相应访问权限,完成名称空间级别权限的快速授予。但,如果通过ClusterRoleBinding引用这类的ClusterRole,则相应的用户即拥有了在所有名称空间上的权限。
  事实上,所有的非名称空间级别的资源都无法通过RoleBinding绑定至用户并赋予用户相关的权限,这些是属于ClusterRoleBinding的功能。
  另外,出了名称空间及集群级别的资源外,K8s还有这/api /apis /healthz /swaggerapi /version等非资源类型URL,对这些URL的访问也必须实现获得相关的权限。它们也只能定义在ClusterRole当中,且需要ClusterRoleBinding进行授权。不过,对此类资源的读取权限已经由系统默认的名称同为”system:discovery” 的ClusterRole和ClusterRoleBinding两个资源自动设定。

下面的命令显示了 system:discovery 的ClusterRole的相关信息:

  1. [root@kmaster ~]# kubectl get clusterrole system:discovery -o yaml
  2. apiVersion: rbac.authorization.k8s.io/v1
  3. kind: ClusterRole
  4. metadata:
  5. annotations:
  6. rbac.authorization.kubernetes.io/autoupdate: "true"
  7. creationTimestamp: "2019-05-09T08:34:43Z"
  8. labels:
  9. kubernetes.io/bootstrapping: rbac-defaults
  10. name: system:discovery
  11. resourceVersion: "60"
  12. selfLink: /apis/rbac.authorization.k8s.io/v1/clusterroles/system%3Adiscovery
  13. uid: 4b632654-7235-11e9-8c1e-7eef5f257c8f
  14. rules:
  15. - nonResourceURLs:
  16. - /api
  17. - /api/*
  18. - /apis
  19. - /apis/*
  20. - /healthz
  21. - /openapi
  22. - /openapi/*
  23. - /swagger-2.0.0.pb-v1
  24. - /swagger.json
  25. - /swaggerapi
  26. - /swaggerapi/*
  27. - /version
  28. - /version/
  29. verbs:
  30. - get

下面的命令显示了 system:discovery 的ClusterRoleBinding的相关信息:

  1. [root@kmaster ~]# kubectl get clusterrolebinding system:discovery -o yaml
  2. apiVersion: rbac.authorization.k8s.io/v1
  3. kind: ClusterRoleBinding
  4. metadata:
  5. annotations:
  6. rbac.authorization.kubernetes.io/autoupdate: "true"
  7. creationTimestamp: "2019-05-09T08:34:44Z"
  8. labels:
  9. kubernetes.io/bootstrapping: rbac-defaults
  10. name: system:discovery
  11. resourceVersion: "117"
  12. selfLink: /apis/rbac.authorization.k8s.io/v1/clusterrolebindings/system%3Adiscovery
  13. uid: 4bdde006-7235-11e9-8c1e-7eef5f257c8f
  14. roleRef:
  15. apiGroup: rbac.authorization.k8s.io
  16. kind: ClusterRole
  17. name: system:discovery
  18. subjects:
  19. - apiGroup: rbac.authorization.k8s.io
  20. kind: Group
  21. name: system:authenticated
  22. - apiGroup: rbac.authorization.k8s.io
  23. kind: Group
  24. name: system:unauthenticated

  由命令的输出结果可知,它绑定了system:authenticated 和system:unauthenticated 两个组,因此所有用户默认均有权限请求读取这些资源,任何发生API Server的此类端点读取请求都会得到响应。

聚合型 ClusterRole

  k8s 1.9之后的版本,在rules字段中嵌套 “aggregationRule” 字段来整合其他ClusterRole对象的规则,这种类型的ClusterRole权限受控于控制器,他们有所有被标签选择器匹配到的用于聚合的ClusterRole的授权规则合并而成。如下示例:

  1. kind: ClusterRole
  2. apiVersion: rbac.authorization.k8s.io/v1
  3. metadata:
  4. name: monitoring
  5. aggregationRule:
  6. clusterRoleSelectors:
  7. - matchLabels:
  8. rbac.example.com/aggregate-to-monitoring: "true"
  9. rules: []

下面是一个能够匹配到此聚合性ClusterRole的另一个ClusterRole示例:
```yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: monitoring-endpoints
labels:
rbac.example.com/aggregate-tomonitoring: “true”
rules:

  • apiGroups: [“”]
    Resources: [“Services”,”endpoints”,”pods”]
    verbs: [“get”,”list”,”watch”]
文档更新时间: 2019-07-29 22:08   作者:张尚