禁止访问Kubernetes的Pods/节点。

22

我正在本地使用 Kubernetes

在使用 Kubernetes 构建 GitLab 时遇到了一些问题。 我认为这与服务账户或角色绑定有关。 但是无法找到正确的解决方法。

我找到了这些帖子:

Kubernetes 日志,用户 "system:serviceaccount:default:default" 无法获取命名空间中的服务

https://github.com/kubernetes/kops/issues/3551

我的错误日志

==> /var/log/gitlab/prometheus/current <==
2018-12-24_03:06:08.88786 level=error ts=2018-12-24T03:06:08.887812767Z caller=main.go:240 component=k8s_client_runtime err="github.com/prometheus/prometheus/discovery/kubernetes/kubernetes.go:372: Failed to list *v1.Node: nodes is forbidden: User \"system:serviceaccount:default:default\" cannot list resource \"nodes\" in API group \"\" at the cluster scope"
2018-12-24_03:06:08.89075 level=error ts=2018-12-24T03:06:08.890719525Z caller=main.go:240 component=k8s_client_runtime err="github.com/prometheus/prometheus/discovery/kubernetes/kubernetes.go:320: Failed to list *v1.Pod: pods is forbidden: User \"system:serviceaccount:default:default\" cannot list resource \"pods\" in API group \"\" at the cluster scope"
1个回答

41

问题是由于您的默认服务帐户没有在集群范围内获取节点或Pod的权限。解决此问题所需的最小集群角色和集群角色绑定为:

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: prom-admin
rules:
# Just an example, feel free to change it
- apiGroups: [""]
  resources: ["pods", "nodes"]
  verbs: ["get", "watch", "list"]

---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: prom-rbac
subjects:
- kind: ServiceAccount
  name: default
roleRef:
  kind: ClusterRole
  name: prom-admin
  apiGroup: rbac.authorization.k8s.io

以上的集群角色允许默认服务账户访问任何命名空间中的所有Pod或节点。

如果您想要授予默认服务账户所有权限,则可以更改集群角色以提供更多权限。在prom-admin中用resources: ["*"]替换即可。

希望这可以帮助到您。


1
嗨@Siner,如果这个或任何答案解决了您的问题,请考虑通过点击复选标记接受它。这向更广泛的社区表明您已找到解决方案,并为回答者和您自己赢得了一些声誉。没有义务这样做。 - Prafull Ladha
2
只是补充一下,有时候也可能是RoleRoleBinding - Ivan Aracki
1
这个答案对我有用,但是我确实需要在底部的主题部分添加 namespace: default - timsegraves
2
对于来到这里的朋友们,关于这个主题的官方文档在此处:https://kubernetes.io/docs/reference/access-authn-authz/rbac/。 - speedplane
1
我的区别在于 kind: ClusterRole。我一直在遵循使用 kubectl create role 的文档,但我需要使用 kubectl create clusterrole 来运行与节点相关的命令。 - E. Moffat
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接