是否有办法让K8s服务帐户在不同的命名空间中创建另一个服务帐户?



我有一个应用程序,它与指定命名空间上的现有服务帐户("代理")进行交互。我希望代理能够在其他名称空间上创建其他服务帐户和角色。有办法吗?

我已经在评论区回答了这个问题,但是我还决定用例子提供更全面的信息。

的背景Kubernetes包含RBAC(基于角色的访问控制)机制,使您能够指定允许特定用户或用户组执行哪些操作。来自Kubernetesv1.6RBAC 有四个Kubernetes对象:Role,ClusterRole,RoleBindingClusterRoleBinding,我们可以使用它们来配置所需的RBAC规则。RoleRoleBinding是命名空间资源,ClusterRoleClusterRoleBinding是集群作用域资源。

我们使用RoleRoleBinding来授权用户访问命名空间资源,我们使用ClusterRoleClusterRoleBinding来授权集群范围的资源。但是,我们也可以混合这些资源。

下面我将简要描述常见的组合。
注意:ClusterRoleBindingsRole是不可能连接起来的。

对于每个测试用例,我创建了新的test命名空间和test-agent服务帐户。

Role和RoleBinding

我在特定的命名空间中创建了简单的RoleRoleBinding:

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

我创建ClusterRoleRoleBinding:
注意:我没有为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使用RoleBinding链接到ServiceAccount,ClusterRole权限适用ONLY到创建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

最后我创建了ClusterRoleClusterRoleBinding:
我没有指定ClusterRoleClusterRoleBinding的名称空间。

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使用ClusterRoleBinding链接到ServiceAccount,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中所有可能的动词,我们可以使用:

$ kubectl api-resources -o wide | grep deployment
deployments                       deploy       apps/v1                           true         Deployment                       [create delete deletecollection get list patch update watch]

最新更新