38

学习 Kubernetes(二十):RBAC

 4 years ago
source link: http://muziyuchen.com/kubernetes-20/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

RBAC

RBAC(Role Based Access Control 基于角色的访问控制) 是安全领域一种授权(Authorization)机制。权限被授予给角色,角色又被赋予给主体。

权限(Permission)、角色(Role)和主体(Subject)之间的关系:

77fE3au.png!web

Kubernetes RBAC

Permission

权限在 Kubernetes 中为对资源(Resource)可以执行的操作(Verb)。

通过执行 kubectl api-resources 查看服务支持的资源列表。

资源按范围分为两类:

  • 命名空间资源,如 podsconfigmaps 等;
  • 非命名空间资源,如 nodes 等。

操作包括: getlistwatchcreateupdatepatchdelete

Role

Kubernetes 中的角色按范围分为两类,对应资源:

  • Role 作用范围为所在 Namespace;
  • ClusterRole 作用范围为集群。

Role 举:chestnut::

apiVersion: rbac.authorization.k8s.io/v1  
kind: Role  
metadata:  
  name: my-role
  namespace: default
rules:  
  - apiGroups: [""]
    resources: ["configmaps"]
    verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]  # 或者 ["*"]

:point_up_2:角色 my-role 拥有所在 Namespace 的 ConfigMap 资源全部操作权限。

ClusterRole 举:chestnut::

apiVersion: rbac.authorization.k8s.io/v1  
kind: ClusterRole  
metadata:  
  name: my-cluster-role
rules:  
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "list", "watch"]

:point_up_2:角色 my-cluster-role 拥有集群 Pod 资源 getlistwatch 操作权限。

Subject

Kubernetes 中的主体分为:

  • User 用户,外部管理的用户;
  • Group 用户组;
  • ServiceAccount 服务账号,内部管理的服务账号。默认情况下,Kubernetes 为每个 Namespace 创建了一个名为 default 的 ServiceAccount。

授权

Kubernetes 通过将角色和主体绑定完成授权,按作用范围分为:

  • RoleBinding 作用范围为所在 Namespace;
  • ClusterRoleBinding 作用范围为集群。

RoleBinding 举:chestnut::

apiVersion: rbac.authorization.k8s.io/v1  
kind: RoleBinding  
metadata:  
  name: my-role-binding
  namespace: default
roleRef:  
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: my-role
subjects:  
  - kind: ServiceAccount 
    name: default
    namespace: default

:point_up_2:为 default 绑定了 my-role 角色。

ClusterRoleBinding 举:chestnut::

apiVersion: rbac.authorization.k8s.io/v1  
kind: ClusterRoleBinding  
metadata:  
  name: my-cluster-role-binding
roleRef:  
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:  
  - kind: ServiceAccount 
    name: default
    namespace: default

:point_up_2:为 default 绑定了 cluster-admin 角色。

注意:cluster-admin 角色之于 Kubernetes 相当 root 用户之于 Linux,请谨慎操作!

参考


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK