【发布时间】: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.yaml和kubectl apply -f manifest.yaml,结果都是一样的。
我还将“read-namespaces”RoleBinding.subjects[0].namespace 设置为正确的命名空间(在本例中为“默认”)。输出没有变化。
【问题讨论】:
-
角色是每个命名空间的,你需要创建一个集群角色
-
只能由集群角色访问命名空间吗?
标签: kubernetes kubectl rbac