【问题标题】:Forbidden to access Kubernetes API Server禁止访问 Kubernetes API Server
【发布时间】:2019-04-13 07:29:45
【问题描述】:

我已经为 Prometheus 定义了一个 ClusterRole:

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: prometheus
  labels:
    k8s-app: prometheus
rules:
- apiGroups: [""] # "" indicates the core API group
  resources:
  - namespaces
  - endpoints
  - services
  - nodes
  - pods
  verbs:
  - get
  - watch
  - list
- nonResourceURLs:
  - /metrics
  - /api/*
  verbs:
  - get

Prometheus 能够访问 API-Servers /metrics 路由:

https://10.0.1.104:443/metrics
https://10.0.2.112:443/metrics

但我得到“服务器返回 HTTP 状态 403 禁止”

https://kubernetes.default.svc:443/api/v1/nodes/ip-10-0-0-219.eu-west-1.compute.internal/proxy/metrics

https://kubernetes.default.svc:443/api/v1/nodes/ip-10-0-0-219.eu-west-1.compute.internal/proxy/metrics/cadvisor

我以为我已经涵盖了

- nonResourceURLs:
  - /api/*

我错过了什么?

【问题讨论】:

  • 你有 ClusterRoleBinding 来将它绑定到 Prometheus 吗?
  • 确定 - 否则无法访问 10.0.2.112:443/metrics
  • 尝试用cluster-admin来clusterrolebinding prometheus进行测试

标签: kubernetes prometheus


【解决方案1】:

对于这两个端点,规则可能缺少(子)资源的nodes/metricsnodes/proxy,并且可能缺少proxy 动词。

如果从安全角度来看可以接受,将cluster-reader 角色分配给 prometheus 的服务帐户会容易得多。

【讨论】:

  • 我的 kubernetes 实例中没有集群角色“cluster-reader”。我有一个集群角色“视图”,但这不起作用,因为它没有足够的权限。
  • @Ronald - 我的错。 cluster-reader 是 OpenShift 中的标准集群角色,而不是“普通”Kubernetes。当我提供答案并且正在处理关于如何在角色规则中引用子资源的一些模糊记忆时,我无法尝试我的任何建议。今天我在文档中找到了它 - kubernetes.io/docs/reference/access-authn-authz/rbac/… 不是很好,但我认为它可能很有用。干杯!
【解决方案2】:

我自己试过了,是的,nodes/proxy 不见了。 (添加后对我有用)

rules:
- apiGroups: [""]
  resources:
  - namespaces
  - endpoints
  - services
  - nodes
  - nodes/proxy <===
  - pods

# From my K8s master
$ curl -k -H 'Authorization: Bearer <redacted>' https://localhost:6443/api/v1/nodes/ip-x-x-x-x.us-west-2.compute.internal/proxy/stats/summary
{
  "node": {
   "nodeName": "ip-x-x-x-x.us-west-2.compute.internal",
   "systemContainers": [
    {
     "name": "kubelet",
     "startTime": "2018-10-19T21:02:19Z",
     "cpu": {
      "time": "2018-11-09T23:51:15Z",
      "usageNanoCores": 30779949,
      "usageCoreNanoSeconds": 59446529195638
     },
....

删除它:

$ curl -k -H 'Authorization: Bearer <redacted>'  https://localhost:6443/api/v1/nodes/ip-x-x-x-x.us-west-2.compute.internal/proxy/stats/summary
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "nodes \"ip-x-x-x-x.us-west-2.compute.internal\" is forbidden: User \"system:serviceaccount:default:prometheus-k8s\" cannot get resource \"nodes/proxy\" in API group \"\" at the cluster scope",
  "reason": "Forbidden",
  "details": {
    "name": "ip-x-x-x-x.us-west-2.compute.internal",
    "kind": "nodes"
  },
  "code": 403
}

【讨论】:

猜你喜欢
  • 2021-05-23
  • 1970-01-01
  • 2018-03-01
  • 2020-02-17
  • 2021-04-18
  • 1970-01-01
  • 1970-01-01
  • 2018-11-24
  • 1970-01-01
相关资源
最近更新 更多