ClusterRoleBinding 需要命名空间

far*_*din 11 rbac kubernetes

我有以下几点:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: SomeServiceAccount
Run Code Online (Sandbox Code Playgroud)
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: SomeClusterRole
rules:
  - apiGroups:
      - "myapi.com"
    resources:
      - 'myapi-resources'
    verbs:
      - '*'
Run Code Online (Sandbox Code Playgroud)
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: SomeClusterRoleBinding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: SomeClusterRole
subjects:
  - kind: ServiceAccount
    name: SomeServiceAccount
Run Code Online (Sandbox Code Playgroud)

但它抛出: The ClusterRoleBinding "SomeClusterRoleBinding" is invalid: subjects[0].namespace: Required value

我认为重点"Cluster"RoleBinding是它不仅限于单个命名空间。任何人都可以解释这一点?

Kubernetes 版本1.13.12 Kubectl 版本v1.16.2 谢谢。

Mar*_*ney 13

创建 ServiceAccount 时不需要设置命名空间,这里的情况是在创建 ClusterRoleBinding 时引用它时需要指定您的 Service 帐户的命名空间以选择它。

ServiceAccounts 是命名空间范围的主题,因此当您引用它们时,您必须指定要绑定的服务帐户的命名空间。来源

例如,在您的情况下,您可以在创建 ClusterRoleBinding 时使用默认命名空间。

通过这样做,您不会将 ClusterRoleBinding 绑定到任何命名空间,正如您在此示例中看到的那样。

$ kubectl get clusterrolebinding.rbac.authorization.k8s.io/tiller -o yaml 
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"rbac.authorization.k8s.io/v1","kind":"ClusterRoleBinding","metadata":{"annotations":{},"name":"tiller"},"roleRef":{"apiGroup":"rbac.authorization.k8s.io","kind":"ClusterRole","name":"cluster-admin"},"subjects":[{"kind":"ServiceAccount","name":"tiller","namespace":"kube-system"}]}
  creationTimestamp: "2019-11-18T13:47:59Z"
  name: tiller
  resourceVersion: "66715"
  selfLink: /apis/rbac.authorization.k8s.io/v1/clusterrolebindings/tiller
  uid: 085ed826-0a0a-11ea-a665-42010a8000f7
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: tiller
  namespace: kube-system
Run Code Online (Sandbox Code Playgroud)


小智 5

kubernetes 服务帐户的范围仅限于命名空间。如果您在创建服务帐户时未指定命名空间,则会在“默认”命名空间中创建您的服务帐户。

这允许您在不同的命名空间中创建具有相同名称的服务帐户。即,当创建命名空间时,所有命名空间都有一个名为“default”的服务帐户。

在命名空间中创建服务帐户:

kubectl create serviceaccount my-sa -n my-namespace
Run Code Online (Sandbox Code Playgroud)

您必须在此处放置在主题中的命名空间指的是“服务帐户所在的位置”,而不是“此集群角色绑定将资源访问权限绑定到哪些命名空间”。