jobs.batch被禁止:用户''"system:serviceaccount:default:default"无法列出命名空间“default”中API组“batch”中的资源“jobs”
我正在使用带有集群内配置的Kubernetes javascript 客户端与集群进行交互。
我正在尝试获取工作列表
app.js(节点)
app.get("/", (req, res) => {
k8sApi2
.listNamespacedJob("default")
.then((res) => {
console.log(res.body);
res.send(res.body);
})
.catch((err) => console.log(err));
});
但这是我得到的 pod 的日志。
这是我的部署
服务
此外,我创建了一个角色和一个角色绑定,但我仍然不知道是什么导致了这个问题。
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: node-apis
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: node-apis
rules:
- apiGroups:
- ""
- "apps"
- "batch"
resources:
- endpoints
- deployments
- pods
- jobs
verbs:
- get
- list
- watch
- create
- delete
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: node-apis
namespace: default
subjects:
- kind: ServiceAccount
name: node-apis
namespace: default
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: node-apis
我是 Kubernetes 的新手,有什么帮助吗?
回答
您需要通过在 pod 的规范部分中指定它来使用服务帐户。由于您没有这样做,它使用的default服务帐户没有允许操作的 Role 和 RoleBinding,从而导致forbidden错误。
spec:
serviceAccountName: node-apis
containers:
...
或者,您可以default在 RoleBinding 中授予服务帐户权限
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: node-apis
namespace: default
subjects:
- kind: ServiceAccount
name: default
namespace: default
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: node-apis
以上是jobs.batch被禁止:用户''"system:serviceaccount:default:default"无法列出命名空间“default”中API组“batch”中的资源“jobs”的全部内容。
THE END
二维码