我的Kubernetes RBAC配置缺少什么导致无法监听Endpoints资源?
问题根因
你的报错存在两个相互独立的配置错误:
- RBAC绑定对象不匹配:运行程序的Pod位于
eng命名空间,默认使用的ServiceAccount是system:serviceaccount:eng:default,但你当前eng命名空间下的RoleBinding仅授权给了default命名空间下的default ServiceAccount,目标SA没有拿到对应权限。 - Informer范围与权限级别不匹配:代码中使用的
informers.NewSharedInformerFactory是全局集群级的工厂,默认会拉取集群所有命名空间的Endpoints资源,而你创建的Role是命名空间级别的权限,仅允许操作eng命名空间内的Endpoints,权限范围不足以支撑全局拉取的需求。
修复方案
根据你的业务需求选择对应方案即可:
方案1:仅监听eng命名空间的Endpoints
第一步:修正RoleBinding配置
修改eng命名空间下的RoleBinding,将授权对象改为eng命名空间的default ServiceAccount:
subjects: - kind: ServiceAccount name: default namespace: eng # 将原来的default改为eng
第二步:修正代码逻辑
将全局SharedInformerFactory替换为指定命名空间的版本:
// 替换原来的factory创建代码 factory := informers.NewSharedInformerFactoryWithOptions(cs, 0, informers.WithNamespace("eng"))
方案2:需要监听集群所有命名空间的Endpoints
直接将命名空间级的RBAC替换为集群级配置即可,代码无需修改:
# 集群角色配置 apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: mesh-endpoint-read-cluster rules: - apiGroups: - "" resources: - endpoints verbs: - get - list - watch --- # 集群角色绑定配置 apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: mesh-endpoint-read-cluster roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: mesh-endpoint-read-cluster subjects: - kind: ServiceAccount name: default namespace: eng
内容的提问来源于stack exchange,提问作者Jon Watte
相关产品推荐
相关产品推荐

