我正在尝试使用其余api访问kubernetes集群。我按照this的说明进行操作,但我想获取群集的豆荚。
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
在群集的主节点上,现在它可以按需运行。
如何使Athorization Bearer以相同的方式工作?
答案 0 :(得分:2)
您正在使用default
服务帐户来获取广告连播。 default
服务帐户没有该权限。您可以通过运行
$ kubectl auth can-i get pods --as system:serviceaccount:default:default
no
“消息”:“禁止播客:用户\“ system:serviceaccount:default:default \”无法在群集范围内的API组\“ \”中列出资源\“ pods \”,
从上面可以看到,默认服务帐户无法列出广告连播
但是在被赋予适当的角色和角色绑定时,如下所示
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": []
}
答案 1 :(得分:0)
为了能够获取所有命名空间的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