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

时间:2021-01-22 10:02:59

标签: kubernetes

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

1 个答案:

答案 0 :(得分:0)

我已经在评论部分回答了这个问题,但我也决定通过示例提供更全面的信息。

背景

Kubernetes 包含 RBAC (role-based access control) 机制,可让您指定允许特定用户或用户组执行哪些操作。从 Kubernetes x RBAC 默认启用。

有四个 Kubernetes 对象:Role, ClusterRoleRoleBindingClusterRoleBinding,我们可以使用它们来配置所需的 RBAC 规则。 v1.6Role 是命名空间,而 RoleBindingClusterRole 是集群范围的资源。

我们使用 ClusterRoleBindingRole 来授权用户使用命名空间资源,我们使用 RoleBindingClusterRole 来获取集群范围的资源。 但是,我们也可以混合使用这些资源。

下面我将简单介绍一下常见的组合。
注意:无法将 ClusterRoleBindingClusterRoleBindings 链接。

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

角色和角色绑定

我在特定的命名空间中创建了简单的 test-agentRole

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

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
注意:我没有为 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

ClusterRole 和 ClusterRoleBinding

最后我创建了 $ 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
注意:我没有为 ClusterRoleBindingClusterRole 指定任何命名空间。

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