Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forbidden to access Kubernetes API Server

I have defined a ClusterRole for Prometheus:

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 is able to access the API-Servers /metrics route:

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

But I get "server returned HTTP status 403 Forbidden" on

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

and

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

I thought I had this covered by

- nonResourceURLs:
  - /api/*

What am I missing?

like image 575
Ronald Avatar asked Nov 09 '18 17:11

Ronald


People also ask

How do I access the Kubernetes API?

Users access the Kubernetes API using kubectl , client libraries, or by making REST requests. Both human users and Kubernetes service accounts can be authorized for API access. When a request reaches the API, it goes through several stages, illustrated in the following diagram:

What is the default port for Kubernetes API?

By default, the Kubernetes API server listens on port 6443 on the first non-localhost network interface, protected by TLS. In a typical production Kubernetes cluster, the API serves on port 443. The port can be changed with the --secure-port, and the listening IP address with the --bind-address flag. The API server presents a certificate.

What are the authorization modules supported by Kubernetes?

Kubernetes supports multiple authorization modules, such as ABAC mode, RBAC Mode, and Webhook mode. When an administrator creates a cluster, they configure the authorization modules that should be used in the API server.

How do I start a Kubernetes proxy server?

Using kubectl to start a proxy server. This command starts a proxy to the Kubernetes API server: kubectl proxy --port=8080 Exploring the Kubernetes API. When the proxy server is running, you can explore the API using curl, wget, or a browser. Get the API versions: curl http://localhost:8080/api/ The output should look similar to this:


2 Answers

I tried this myself and yes nodes/proxy is missing. (it works for me after adding it)

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
     },
....

Removing it:

$ 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
}
like image 97
Rico Avatar answered Nov 15 '22 08:11

Rico


For those two endpoints, the rules may be missing nodes/metrics and nodes/proxy for (sub)resources, and possibly the proxy verb.

If acceptable from security standpoint, it will be much easier to assign the cluster-reader role to the prometheus' service account.

like image 23
apisim Avatar answered Nov 15 '22 09:11

apisim