Azure kubernetes clusterrolebindings不工作

lsmepo6l  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(2)|浏览(105)

我有一个Azure Kubernetes集群,设置身份验证和授权设置为“Azure AD Authentication with Kubernetes Rbac”。我希望能够同时使用AAD进行身份验证,而且还可以使用集群中的常规服务帐户。
虽然它似乎在我的命名空间内工作,但看起来好像我不能授予服务帐户访问集群范围的资源:

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: dummy-admin
  5. ---
  6. apiVersion: rbac.authorization.k8s.io/v1
  7. # This cluster role binding allows anyone in the "manager" group to
  8. # read secrets in any namespace.
  9. kind: RoleBinding
  10. metadata:
  11. name: exmple-binding
  12. subjects:
  13. - kind: User
  14. name: dummy-admin # Name is case sensitive
  15. apiGroup: rbac.authorization.k8s.io
  16. roleRef:
  17. kind: Role
  18. name: example-role
  19. apiGroup: rbac.authorization.k8s.io
  20. ---
  21. apiVersion: rbac.authorization.k8s.io/v1
  22. kind: Role
  23. metadata:
  24. name: example-role
  25. rules:
  26. - apiGroups: ["*"]
  27. resources: ["*"]
  28. verbs: ["*"]
  29. ---
  30. apiVersion: v1
  31. kind: Pod
  32. metadata:
  33. name: kubectl
  34. spec:
  35. serviceAccountName: dummy-admin
  36. containers:
  37. - name: kubectl
  38. image: bitnami/kubectl
  39. # Just spin & wait forever
  40. command: [ "/bin/bash", "-c", "--" ]
  41. args: [ "while true; do sleep 30; done;" ]

字符串
应用此配置,运行kubectl exec -ti pod/kubectl -- /bin/bash并尝试列出节点,给予我这个错误:

  1. I have no name!@kubectl:/$ kubectl get all
  2. Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:jaiv:dummy-admin" cannot list resource "pods" in API group "" in the namespace "jaiv"
  3. Error from server (Forbidden): replicationcontrollers is forbidden: User "system:serviceaccount:jaiv:dummy-admin" cannot list resource "replicationcontrollers" in API group "" in the namespace "jaiv"
  4. Error from server (Forbidden): services is forbidden: User "system:serviceaccount:jaiv:dummy-admin" cannot list resource "services" in API group "" in the namespace "jaiv"
  5. Error from server (Forbidden): daemonsets.apps is forbidden: User "system:serviceaccount:jaiv:dummy-admin" cannot list resource "daemonsets" in API group "apps" in the namespace "jaiv"
  6. Error from server (Forbidden): deployments.apps is forbidden: User "system:serviceaccount:jaiv:dummy-admin" cannot list resource "deployments" in API group "apps" in the namespace "jaiv"
  7. Error from server (Forbidden): replicasets.apps is forbidden: User "system:serviceaccount:jaiv:dummy-admin" cannot list resource "replicasets" in API group "apps" in the namespace "jaiv"
  8. Error from server (Forbidden): statefulsets.apps is forbidden: User "system:serviceaccount:jaiv:dummy-admin" cannot list resource "statefulsets" in API group "apps" in the namespace "jaiv"
  9. Error from server (Forbidden): horizontalpodautoscalers.autoscaling is forbidden: User "system:serviceaccount:jaiv:dummy-admin" cannot list resource "horizontalpodautoscalers" in API group "autoscaling" in the namespace "jaiv"
  10. Error from server (Forbidden): cronjobs.batch is forbidden: User "system:serviceaccount:jaiv:dummy-admin" cannot list resource "cronjobs" in API group "batch" in the namespace "jaiv"
  11. Error from server (Forbidden): jobs.batch is forbidden: User "system:serviceaccount:jaiv:dummy-admin" cannot list resource "jobs" in API group "batch" in the namespace "jaiv"


在这种情况下,我看到我是正确的身份,并希望被允许在我的命名空间中列出资源。

hc2pp10m

hc2pp10m1#

在代码中,使用rolerolebindings,而不是clusterrolesclusterrolebdingins。这是不同的资源类型。
您正在default命名空间中创建服务帐户,并尝试在jaiv命名空间上执行某些操作。尝试使用ClusterRoleClusterRoleBinding。它也适用于命名空间资源,但适用于群集范围。(创建方式相同,只是名称不同)
更多详情请点击此处:https://kubernetes.io/docs/reference/access-authn-authz/rbac/#role-and-clusterrole

axr492tv

axr492tv2#

我终于发现了我的错误。
在进行角色绑定时,有三种不同的主体类型:用户、组和服务帐户。我使用User作为主题类型,而是需要将角色绑定定义为:

  1. apiVersion: rbac.authorization.k8s.io/v1
  2. # This cluster role binding allows anyone in the "manager" group to
  3. # read secrets in any namespace.
  4. kind: RoleBinding
  5. metadata:
  6. name: exmple-binding
  7. subjects:
  8. - kind: ServiceAccount
  9. name: dummy-admin # Name is case sensitive
  10. apiGroup: rbac.authorization.k8s.io
  11. roleRef:
  12. kind: Role
  13. name: example-role
  14. apiGroup: rbac.authorization.k8s.io

字符串

展开查看全部

相关问题