gpt4 book ai didi

kubernetes - RBAC:具有多个命名空间的角色

转载 作者:行者123 更新时间:2023-12-05 02:59:37 26 4
gpt4 key购买 nike

尝试编写我的第一套 RBAC 角色。因此,试图找出为多个命名空间组件分配 2 个角色的最佳方法。

管理员角色(3 个命名空间的 RW 表示默认,ns1 和 ns2)用户角色(只读 3 个命名空间,默认为 ns1 和 ns2)

我认为需要一个服务帐户,其中包含 2 个用于管理员/用户的 clusterRoles

apiVersion: rbac.authorization.k8s.io/v1
kind: ServiceAccount
metadata:
name: sa
namespace: default

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: admin-master
rules:
- apiGroups:
- batch
resources:
- pods
verbs:
- create
- delete
- deletecollection
- get
- list
- patch
- update
- watch

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: user-master
rules:
- apiGroups:
- batch
resources:
- pods
verbs:
- get
- list
- watch

然后使用角色绑定(bind):

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: admin-rw
namespace: ns1
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: admin-master
subjects:
- kind: ServiceAccount
name: sa
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: user-readonly
namespace: ns1
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: user-master
subjects:
- kind: ServiceAccount
name: sa
namespace: default

但不确定将角色 admin-rw/user-readonly 与命名空间 2 (ns2) 绑定(bind)的最佳方式是什么?

最佳答案

角色是有范围的,要么绑定(bind)到特定的命名空间,要么绑定(bind)到集群范围。对于命名空间范围的角色,you can just simply deploy the same role in multiple namespaces .

这背后的想法是在集群中拥有分区权限,虽然it implies more administrative effort但这是一种更安全的做法。

此外,在您的定义中,您试图将权限绑定(bind)到特定的命名空间,但是,您使用的是 ClusterRole,它是 cluster-scoped resource .如果您想要命名空间范围内的权限,您可能希望将其更改为 Role

您可能会发现这个 CNCF article在这件事上很有用。

关于kubernetes - RBAC:具有多个命名空间的角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57726890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com