admin管理员组文章数量:1648000
一、报错信息描述
错误信息:
Message: Forbidden!Configured service account doesn't have access. Service account may have been revoked. endpoints "xxx" is forbidden: User "system:serviceaccount:xxx:default" cannot get resource "endpoints" in API group "" in the namespace "xxx".
报出这个错误,要解决的话,首先你要知道serviceaccount是什么?
serviceaccount也是K8s中的资源对象,例如Pod、Deployment等一样,可以通过yaml定义。
Pod内部环境应用程序在访问Kubernetes的Api Server的时候的权限验证就是通过serviceaccount实现的。
当你创建namespace的时候,会默认为该namespace创建一个名为default的serviceaccount。可以通过命令查看:
kubectl get sa -n 你的namespace
正如报错所示“system:serviceaccount:xxx:default”,这个就是pod内部的用到的默认的serviceaccount,xxx代表你的namespace,default则是namespace的默认serviceaccount名字,前面system:serviceaccount代表k8s中的serviceaccount用户组。
所以这个的错误的信息代表的意思是,pod用namespace默认的serviceaccout是没有权限访问K8s的 API group的。
二、解决方法
知道了报错的原因,我们就可以解决这个问题了,那就是创建一个新的serviceaccount绑定到我们的pod上。
而新创建的serviceaccount需要有 API group的权限。说到权限有涉及到新的知识点,就是K8s的授权插件,我这里用的是RBAC,意思就是根据角色来控制权限。
serviceaccount对象代表一个账号,则我们还需要一个role对象和一个role与serviceaccount绑定的rolebinding对象,这些都是RBAC插件提供的资源对象。
举个例子,这个例子可以解决上面的报错,在你的namespace下创建一个拥有K8s集群里最高权限的serviceaccount,可以对任何资源对象操作:
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: test #ClusterRoleBinding的名字
subjects:
- kind: ServiceAccount
name: test #serviceaccount资源对象的name
namespace: test #serviceaccount的namespace
roleRef:
kind: ClusterRole
name: cluster-admin #k8s集群中最高权限的角色
apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: test # ServiceAccount的名字
namespace: test # serviceaccount的namespace
labels:
app: test #ServiceAccount的标签
然后通过在deployment内设置spec.template.spec.serviceAccountName或者pod的spec.serviceAccountName完成与serviceaccount的绑定。
以上可以解决,但是权限也非常的高。
我们还可以创建一个 ClusterRole,一个ServiceAccount,一个ClusterRoleBinding。(访问集群中所有资源的权限)
或者Role,一个ServiceAccount,一个RoleBinding。(同一个namespace下的资源控制)
ClusterRole制定角色的权限,然后通过ClusterRoleBinding与ServiceAccount绑定起来,做到精细化的权限控制,这里就不展开了,网上很容易查到~~
K8s中ServiceAccount、Role、RoleBinding、ClusterRole、ClusterRoleBinding之间的关系
------------------------------------------------------------------
补充
又见到一个错误,是使用spring-cloud-kubernetes时发现的:
2021-02-19 09:05:24.405 WARN 1 --- [ main] .KubernetesClientConfigMapPropertySource : Unable to get ConfigMap xxx in namespace xxxx
io.kubernetes.client.openapi.ApiException: Forbidden
也是一样的道理,按照上述解决方式可以解决
本文标签: ServiceAccountxxxUserSystemDefault
版权声明:本文标题:User “system:serviceaccount:xxx:default“ cannot get resource “endpoints“ in API group ““问题解决 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1729496468a1202966.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论