0
点赞
收藏
分享

微信扫一扫

k8s-apiServer鉴权

1.介绍

在 Kubernetes 项目中,负责完成授权(Authorization)工作的机制,就是 RBAC:基于角色的访问控制(Role-Based Access Control)。

Role:角色,它其实是一组规则,定义了一组对 Kubernetes API 对象的操作权限。

Subject:被作用者,即User或ServiceAccount。

RoleBinding:定义了“被作用者”和“角色”的绑定关系。

 k8s-apiServer鉴权_访问控制

2.RoleBinding

只针对指定Namespace生效

创建ServiceAccount

apiVersion: v1
kind: ServiceAccount
metadata:
namespace: mynamespace
name: example-sa

设置role:example-role的权限

拥有pod的"get", "watch", "list"权限

拥有某个pod的"get", "watch", "list"权限

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: mynamespace
name: example-role
rules:
- apiGroups: [""]
resources: ["pods"]
resourceNames: ["my-pod"]
verbs: ["get", "watch", "list"]

设置哪个User拥有上面的角色

这里的user需要通过外部认证服务,比如 Keystone,来提供。

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: example-rolebinding
namespace: mynamespace
subjects:
- kind: User
name: example-user
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: example-role
apiGroup: rbac.authorization.k8s.io

设置哪个ServiceAccount拥有上面的角色

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: example-rolebinding
namespace: mynamespace
subjects:
- kind: ServiceAccount
name: example-sa
namespace: mynamespace
roleRef:
kind: Role
name: example-role
apiGroup: rbac.authorization.k8s.io

3.ClusterRoleBinding

对所有Namespace生效

设置ClusterRole权限

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

设置哪个ServiceAccount拥有上面的角色

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: example-clusterrolebinding
subjects:
- kind: ServiceAccount
name: example-sa
namespace: mynamespace
roleRef:
kind: ClusterRole
name: example-clusterrole
apiGroup: rbac.authorization.k8s.io

4.Group概念

Kubernetes 还拥有“用户组”(Group)的概念,也就是一组“用户”的意思。

一个 ServiceAccount,在 Kubernetes 里对应的“用户”的名字是:

system:serviceaccount:<Namespace名字>:<ServiceAccount名字>

它对应的内置“用户组”的名字,就是

system:serviceaccounts:<Namespace名字>

举例如下规则1:

意味着这个 Role 的权限规则,作用于整个系统里的所有 ServiceAccount

subjects:
- kind: Group
name: system:serviceaccounts
apiGroup: rbac.authorization.k8s.io

举例如下规则2:

意味着这个 Role 的权限规则,作用于 mynamespace 里的所有 ServiceAccount。这就用到了“用户组”的概念。

subjects:
- kind: Group
name: system:serviceaccounts:mynamespace
apiGroup: rbac.authorization.k8s.io


更多介绍查看我的另一篇文章

​​https://blog.51cto.com/u_10983441/4976185​​


举报

相关推荐

0 条评论