通过Bearer Token授权访问Kubernetes API

cvxl0en2  于 11个月前  发布在  Kubernetes
关注(0)|答案(3)|浏览(146)

我尝试使用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


在集群的主节点,现在它可以按预期工作。
我怎样才能让授权承载者以同样的方式工作?

qlvxas9a

qlvxas9a1#

您正在使用default服务帐户获取pods。default serviceaccount没有该权限。您可以通过运行以下命令来检查是否允许某个操作

$ kubectl auth can-i get pods --as system:serviceaccount:default:default
no

字符串
“message”:“pods被禁止:User \“system:serviceaccount:default:default\”无法列出集群范围内API组"\”中的资源\“pods\”,
如上面所示,默认服务帐户无法列出Pod
但当被赋予适当的角色和角色绑定时,

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: demo-role
  namespace: default
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
  - list

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: demo-binding
  namespace: default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: demo-role
subjects:
- kind: ServiceAccount
  name: default
  namespace: default


如果你检查

$ kubectl auth can-i get pods --as system:serviceaccount:default:default
yes


如果你想使用API列出默认命名空间的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/namespaces/default/pods --header "Authorization: Bearer $TOKEN" --insecure
{
  "kind": "PodList",
  "apiVersion": "v1",
  "metadata": {
    "selfLink": "/api/v1/namespaces/default/pods",
    "resourceVersion": "1589"
  },
  "items": []
}

3df52oht

3df52oht2#

为了能够获得所有名称空间的pod,您必须定义一个cluster-role和一个cluster-role-binding,而不是一个role/role-binding对

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: get-pods
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: get-pods-binding
subjects:
- kind: ServiceAccount
  name: default
  namespace: default
roleRef:
  kind: ClusterRole
  name: get-pods
  apiGroup: rbac.authorization.k8s.io

字符串
您可以将其保存到一个名为get_pods_role.yaml的文件中并执行kubectl create -f get_pods_role.yaml。然后您将能够通过以下方式获得所需的输出:

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

xu3bshqb

xu3bshqb3#

如果您使用的是启用了RBAC的Kubernetes集群,则服务帐户可能没有被授权访问API服务器。允许您查询API服务器的最简单方法是通过运行以下命令来绕过RBAC:

kubectl create clusterrolebinding permissive-binding --clusterrole=cluster-admin --group=system:serviceaccounts

字符串
这将为所有服务帐户(我们也可以说是所有pod)提供群集管理员权限,允许他们做任何他们想做的事情。请注意:**显然,这样做是危险的,不应该在生产群集上进行。**出于测试目的,这是可以的。

相关问题