【问题标题】:How do you set up kubernetes RBAC resources so that pods can access the API via a client?如何设置 kubernetes RBAC 资源,以便 pod 可以通过客户端访问 API?
【发布时间】:2020-09-09 12:31:31
【问题描述】:

问题

我有一个简单的 RBAC 配置来访问集群内的 Kubernetes API。但是,我从kubectl 得到了似乎相互矛盾的信息。部署清单后,RBAC 似乎设置正确。

$ kubectl exec -ti pod/controller -- kubectl auth can-i get namespaces
Warning: resource 'namespaces' is not namespace scoped
yes

但是,实际发出请求会产生权限错误

$ kubectl exec -ti pod/controller -- kubectl get namespaces
Error from server (Forbidden): namespaces is forbidden: User "system:serviceaccount:default:controller" cannot list resource "namespaces" in API group "" at the cluster scope
command terminated with exit code 1

清单

apiVersion: 'v1'
kind: 'ServiceAccount'
metadata:
  name: 'controller'
---

apiVersion: 'rbac.authorization.k8s.io/v1'
kind: 'Role'
metadata:
  name: 'read-namespaces'
rules:
  - apiGroups:
      - ''
    resources:
      - 'namespaces'
    verbs:
      - 'get'
      - 'watch'
      - 'list'
---

apiVersion: 'rbac.authorization.k8s.io/v1'
kind: 'RoleBinding'
metadata:
  name: 'read-namespaces'
roleRef:
  apiGroup: ''
  kind: 'Role'
  name: 'read-namespaces'
subjects:
  - kind: 'ServiceAccount'
    name: 'controller'
---

apiVersion: 'v1'
kind: 'Pod'
metadata:
  name: 'controller'
  labels:
    'app': 'controller'
spec:
  containers:
    - name: 'kubectl'
      image: 'bitnami/kubectl:latest'
      imagePullPolicy: 'Always'
      command:
        - 'sleep'
        - '3600'
  serviceAccountName: 'controller'
---

其他信息

我试过kubectl auth reconcile -f manifest.yamlkubectl apply -f manifest.yaml,结果都是一样的。

我还将“read-namespaces”RoleBinding.subjects[0].namespace 设置为正确的命名空间(在本例中为“默认”)。输出没有变化。

【问题讨论】:

  • 角色是每个命名空间的,你需要创建一个集群角色
  • 只能由集群角色访问命名空间吗?

标签: kubernetes kubectl rbac


【解决方案1】:

命名空间是集群范围的资源。所以你需要一个ClusterRole 和一个ClusterRoleBinding

apiVersion: 'rbac.authorization.k8s.io/v1'
kind: 'ClusterRole'
metadata:
  name: 'read-namespaces'
rules:
  - apiGroups:
      - ''
    resources:
      - 'namespaces'
    verbs:
      - 'get'
      - 'watch'
      - 'list'
---

apiVersion: 'rbac.authorization.k8s.io/v1'
kind: 'ClusterRoleBinding'
metadata:
  name: 'read-namespaces'
roleRef:
  apiGroup: 'rbac.authorization.k8s.io'
  kind: 'ClusterRole'
  name: 'read-namespaces'
subjects:
  - kind: 'ServiceAccount'
    name: 'controller'
--- 

【讨论】:

    【解决方案2】:

    角色是每个命名空间的,您需要使用ClusterRoleBinding创建集群角色和绑定

    如果你想将你的集群角色绑定到特定的命名空间,你可以这样做,在 ClusterRole 上使用 RoleBinding:

    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: sa
      namespace: myapp
    
    ---
    kind: ClusterRole
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: role-myapp
    rules:
    - apiGroups:
      - batch
      resources:
      - cronjobs
      verbs:
      - create
      - delete
      - deletecollection
      - get
      - list
      - patch
      - update
      - watch
    
    ---
    
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: job001
      namespace: myapp
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: role-myapp
    subjects:
    - kind: ServiceAccount
      name: sa
      namespace: myapp
    

    【讨论】:

    • 我能够让它工作,但我还需要将 RoleBinding 更改为 ClusterRoleBinding
    猜你喜欢
    • 2021-07-14
    • 1970-01-01
    • 2023-01-31
    • 2018-12-08
    • 2013-10-09
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多