我在k8s集群中创建了以下对象。
请参阅以下yaml文件中的Role和RoleBinding资源。
$ cat developer.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: developer
namespace: testpsp
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- create
- apiGroups:
- extensions
- apps
resources:
- deployments
- replicasets
verbs:
- '*'
$ cat developer-binding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: developer-binding
namespace: testpsp
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: developer
subjects:
- kind: ServiceAccount
name: testuser
正如您在上述角色清单文件中所看到的,我已将Pod
资源的 CREATE权限授予了testuser
服务帐户。但是我仍然遇到错误。
错误
来自服务器的错误(禁止):创建“ hello-pod.yaml”时出错:禁止使用pod:用户“ testuser”无法在名称空间“ testpsp”的API组“”中创建资源“ pods” < / em>
这是Pod yaml文件。我在这里想念什么吗?
$ cat hello-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: hello-pod
namespace: testpsp
spec:
serviceAccountName: testuser
containers:
- name: hello-kubernetes
image: paulbouwer/hello-kubernetes:1.5
ports:
- containerPort: 8080
这是我正在运行的用于创建Pod的命令。
$ kubectl --as=testuser -n testpsp create -f hello-pod.yaml
答案 0 :(得分:1)
在解决此问题时,我注意到我们需要使用以下格式,而不是在kubectl命令的“ as”标志中直接提及ServiceAccount名称。
system:serviceaccount:<namespace_name>:<serviceaccount_name>
就我而言,它看起来像这样- system:serviceaccount:testpsp:testuser
此后,它开始正常运行。