【问题标题】:Kubernetes service account does not have assigned role?Kubernetes 服务帐户没有分配角色?
【发布时间】:2020-12-23 23:00:43
【问题描述】:

我在 Kubernetes 中有一个服务帐号:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: testsa
  namespace: project-1

我已经为它分配了view 角色:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: testsa-view
  namespace: project-1
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: view
subjects:
- kind: ServiceAccount
  name: testsa
  namespace: project-1

这应该授予服务帐户对所有资源的读取权限。在 project-1 命名空间中的 pod 内,我正在尝试运行以下 Python 代码:

>>> from kubernetes import client, config
>>> config.load_incluster_config()
>>> api = client.CoreV1Api()
>>> api.list_pod_for_all_namespaces()

但这会失败并出现403 错误:

kubernetes.client.rest.ApiException: (403)
Reason: Forbidden
[...]
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"pods is forbidden: User \"system:serviceaccount:project-1:testsa\" cannot list resource \"pods\" in API group \"\" at the cluster scope","reason":"Forbidden","details":{"kind":"pods"},"code":403}

pod 与服务帐号相关联:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: testsa
  name: testsa-2-l929g
  namespace: project-1
spec:
  serviceAccountName: testsa
  automountServiceAccountToken: true
  containers:
  - image: larsks/testsa
    imagePullPolicy: Always
    name: testsa
    ports:
    - containerPort: 8080
      protocol: TCP
    resources: {}

在容器内,我可以看到挂载的秘密:

/src $ find /run/secrets/ -type f
/run/secrets/kubernetes.io/serviceaccount/..2020_09_04_16_30_26.292719465/ca.crt
/run/secrets/kubernetes.io/serviceaccount/..2020_09_04_16_30_26.292719465/token
/run/secrets/kubernetes.io/serviceaccount/..2020_09_04_16_30_26.292719465/service-ca.crt
/run/secrets/kubernetes.io/serviceaccount/..2020_09_04_16_30_26.292719465/namespace
/run/secrets/rhsm/ca/redhat-uep.pem
/run/secrets/rhsm/ca/redhat-entitlement-authority.pem

我在这里错过了什么?

【问题讨论】:

    标签: python kubernetes openshift rbac


    【解决方案1】:

    错误提示 cannot list resource \"pods\" in API group \"\" at the cluster scope,因为您试图访问集群中所有命名空间的所有 pod,而不是仅 project-1 命名空间的所有 pod。

    所以将Role 更改为ClusterRoleBinding

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: testsa-view
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: view
    subjects:
    - kind: ServiceAccount
      name: testsa
      namespace: project-1
    

    从示例中引用 here RoleBinding 始终授予仅限于该特定命名空间的命名空间范围资源的权限,即使您在其中引用 ClusterRole

    您可以使用以下命令检查服务帐户的权限

    kubectl auth can-i --list --as=system:serviceaccount:project-1:testsa
    kubectl auth can-i --list --as=system:serviceaccount:project-1:testsa -n project-1
    kubectl auth list pods --as=system:serviceaccount:project-1:testsa
    kubectl auth list pods --as=system:serviceaccount:project-1:testsa -n project-1
    

    【讨论】:

    • 谢谢,这很有意义。我自己应该已经发现了这一点,而且我发现如果我只打电话给api.list_namespaced_pod("project-1') 就可以了。
    猜你喜欢
    • 2020-03-26
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 2020-08-22
    • 2022-12-14
    • 2021-10-28
    相关资源
    最近更新 更多