【问题标题】:Why does this pod get a 403 Forbidden when calling the Kubernetes API despite a RoleBinding (same with ClusterRoleBinding)?尽管有 RoleBinding(与 ClusterRoleBinding 相同),为什么这个 pod 在调用 Kubernetes API 时会得到 403 Forbidden?
【发布时间】:2021-07-14 04:41:21
【问题描述】:

我创建了一个 pod(一个用于运行命令的 Alpine“BusyBox”),然后获取与之关联的 default 服务帐户。然后我创建了一个RoleBinding(后来ClusterRoleBinding 第一个不起作用)但它仍然不允许我调用 K8s API。

我做错了什么?

首先我创建了一个容器来运行命令:

# Create a namespace to install our pod
kubectl create namespace one

# Now create a pod that we can run stuff in
kubectl run runner -n one --image alpine -- sleep 3600

然后我创建了一个角色绑定:

# My understanding of this command is that I'm doing the following:
# 1. Creating a binding for the "default" service account in the "one" namespace
# 2. Tying that to the cluster role for viewing things
# 3. Making this binding work in the "default" namespace, so that it can call
#    the API there FROM its own namespace (one)
kubectl create rolebinding default-view --clusterrole=view --serviceaccount=one:default --namespace=default

然后我连接到 pod 的终端并尝试调用 API 以列出其自己命名空间中的所有服务:

kubectl exec --stdin --tty use-rest -n one -- /bin/ash

# Now I run all these inside that terminal:

# Point to the internal API server hostname
APISERVER=https://kubernetes.default.svc

# Path to ServiceAccount token
SERVICEACCOUNT=/var/run/secrets/kubernetes.io/serviceaccount

# Read the ServiceAccount bearer token
TOKEN=$(cat ${SERVICEACCOUNT}/token)

# Reference the internal certificate authority (CA)
CACERT=${SERVICEACCOUNT}/ca.crt

# The wget installed with Alpine cannot do SSL
apk --no-cache add ca-certificates
apk add wget

wget --ca-certificate=${CACERT} --header="Authorization: Bearer ${TOKEN}" ${APISERVER}/api/v1/namespaces/$NAMESPACE/services

上面给出了错误:

--2021-04-20 01:04:54-- https://kubernetes.default.svc/api/v1/namespaces/default/services/
正在解决 kubernetes.default.svc (kubernetes.default.svc)... 10.43.0.1
连接到 kubernetes.default.svc (kubernetes.default.svc)|10.43.0.1|:443... 已连接。
已发送 HTTP 请求,等待响应... 403 Forbidden
2021-04-20 01:04:54 错误 403:禁止。

但这应该被允许!使用集群角色绑定时出现同样的错误。

使用:

  • k3d版本v4.4.1
  • k3s 版本 v1.20.5-k3s1(默认)
  • 印花布

【问题讨论】:

  • 您在命名空间 one 中创建了 ServiceAccount,但您在 -n two 中创建了 Pod
  • @mdaniel 抱歉,这只是我帖子中的一个错字。我在本地有正确的命名空间,但它给出了错误

标签: ssl kubernetes kubectl project-calico kubernetes-apiserver


【解决方案1】:

每个 pod 只能有一个 ServiceAccount,一旦您为该 pod 分配了一个帐户,default 帐户将不再适用。我试图将角色绑定到 default 帐户,但传递了我为 pod 创建的另一个帐户的令牌。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-29
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多