我尝试使用rest API访问kubernetes集群。我按照this的说明操作,但我想获取集群的pod。
APISERVER=$(kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}')
SECRET_NAME=$(kubectl get serviceaccount default -o jsonpath='{.secrets[0].name}')
TOKEN=$(kubectl get secret $SECRET_NAME -o jsonpath='{.data.token}' | base64 --decode)
curl $APISERVER/api/v1/pods --header "Authorization: Bearer $TOKEN" --insecure
字符串
结果是:
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "pods is forbidden: User \"system:serviceaccount:default:default\" cannot list resource \"pods\" in API group \"\" at the cluster scope",
"reason": "Forbidden",
"details": {
"kind": "pods"
},
"code": 403
型
我通过运行以下命令尝试了相同的(curl localhost:8001/api/v1/pods)
:
kubectl proxy --address='0.0.0.0' --disable-filter=true
型
在集群的主节点,现在它可以按预期工作。
我怎样才能让授权承载者以同样的方式工作?
3条答案
按热度按时间qlvxas9a1#
您正在使用
default
服务帐户获取pods。default
serviceaccount没有该权限。您可以通过运行以下命令来检查是否允许某个操作字符串
“message”:“pods被禁止:User \“system:serviceaccount:default:default\”无法列出集群范围内API组"\”中的资源\“pods\”,
如上面所示,默认服务帐户无法列出Pod
但当被赋予适当的角色和角色绑定时,
型
如果你检查
型
如果你想使用API列出默认命名空间的pod,那么运行
型
3df52oht2#
为了能够获得所有名称空间的pod,您必须定义一个cluster-role和一个cluster-role-binding,而不是一个role/role-binding对
字符串
您可以将其保存到一个名为
get_pods_role.yaml
的文件中并执行kubectl create -f get_pods_role.yaml
。然后您将能够通过以下方式获得所需的输出:型
xu3bshqb3#
如果您使用的是启用了RBAC的Kubernetes集群,则服务帐户可能没有被授权访问API服务器。允许您查询API服务器的最简单方法是通过运行以下命令来绕过RBAC:
字符串
这将为所有服务帐户(我们也可以说是所有pod)提供群集管理员权限,允许他们做任何他们想做的事情。请注意:**显然,这样做是危险的,不应该在生产群集上进行。**出于测试目的,这是可以的。