我有一个应用程序,它与指定命名空间上的现有服务帐户(“代理”)进行交互。我希望代理能够在其他命名空间上创建额外的服务帐户和角色。有办法吗?
答案 0 :(得分:0)
我已经在评论部分回答了这个问题,但我也决定通过示例提供更全面的信息。
Kubernetes 包含 RBAC (role-based access control) 机制,可让您指定允许特定用户或用户组执行哪些操作。从 Kubernetes x
RBAC 默认启用。
有四个 Kubernetes 对象:Role
, ClusterRole
、RoleBinding
和 ClusterRoleBinding
,我们可以使用它们来配置所需的 RBAC 规则。 v1.6
和 Role
是命名空间,而 RoleBinding
和 ClusterRole
是集群范围的资源。
我们使用 ClusterRoleBinding
和 Role
来授权用户使用命名空间资源,我们使用 RoleBinding
和 ClusterRole
来获取集群范围的资源。
但是,我们也可以混合使用这些资源。
下面我将简单介绍一下常见的组合。
注意:无法将 ClusterRoleBinding
与 ClusterRoleBindings
链接。
对于每个测试用例,我都创建了新的 Role
命名空间和 test
服务帐户。
我在特定的命名空间中创建了简单的 test-agent
和 Role
:
RoleBinding
我们可以看到 apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: test-role
namespace: test
rules:
- apiGroups:
- ""
resources:
- '*'
verbs:
- '*'
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: test-rolebinding
namespace: test
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: test-role
subjects:
- kind: ServiceAccount
name: test-agent
只能访问 test-agent
命名空间中的资源:
test
我创建了 $ kubectl auth can-i get pod -n test --as=system:serviceaccount:test:test-agent
yes
$ kubectl auth can-i get pod -n default --as=system:serviceaccount:test:test-agent
no
和 ClusterRole
:
注意:我没有为 RoleBinding
指定任何命名空间。
ClusterRole
现在我们可以看到,如果使用 apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: test-clusterrole
rules:
- apiGroups:
- ""
resources:
- '*'
verbs:
- '*'
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: test-rolebinding
namespace: test
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: test-clusterrole
subjects:
- kind: ServiceAccount
name: test-agent
将 ClusterRole
链接到 ServiceAccount
,则 RoleBinding
权限仅适用于创建此 ClusterRole
的命名空间:
RoleBinding
最后我创建了 $ kubectl auth can-i get pod -n test --as=system:serviceaccount:test:test-agent
yes
$ kubectl auth can-i get pod -n default --as=system:serviceaccount:test:test-agent
no
和 ClusterRole
:
注意:我没有为 ClusterRoleBinding
和 ClusterRole
指定任何命名空间。
ClusterRoleBinding
现在我们可以看到,如果使用 apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: test-clusterrole
rules:
- apiGroups:
- ""
resources:
- '*'
verbs:
- '*'
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: test-clusterrolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: test-clusterrole
subjects:
- kind: ServiceAccount
name: test-agent
namespace: test
将 ClusterRole
链接到 ServiceAccount
,则 ClusterRoleBinding
权限适用于所有命名空间:
ClusterRole
有用的说明:您可以使用以下命令显示特定资源的所有可能动词
$ kubectl auth can-i get pod -n test --as=system:serviceaccount:test:test-agent
yes
$ kubectl auth can-i get pod -n default --as=system:serviceaccount:test:test-agent
yes
$ kubectl auth can-i get pod -n kube-system --as=system:serviceaccount:test:test-agent
yes
,例如要显示 kubectl api-resources -o wide
的所有可能动词,我们可以使用:
Deployment