【发布时间】:2018-04-09 11:06:16
【问题描述】:
我的 Kubernetes 集群在 GKE 中运行,我想在集群外运行应用程序并与 Kubernetes API 通信。
通过使用从运行中检索到的密码:
gcloud container clusters get-credentials cluster-2 --log-http
我可以通过基本身份验证访问 API。 但我想创建多个 Kubernetes 服务帐户,并为它们配置所需的授权并适当地使用。
所以,我创建了服务帐户并使用以下方式获取令牌:
kubectl create serviceaccount sauser1
kubectl get serviceaccounts sauser1 -o yaml
kubectl get secret sauser1-token-<random-string-as-retrieved-from-previous-command> -o yaml
如果我尝试使用 Bearer 身份验证使用获得的令牌访问 Kubernetes API,则会收到 401 HTTP 错误。我认为可能需要为服务帐户设置一些权限,所以根据文档here,我在 YAML 文件下面创建了:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: pod-reader
subjects:
- kind: ServiceAccount
name: sauser1
namespace: default
roleRef:
kind: ClusterRole
name: pod-reader
apiGroup: rbac.authorization.k8s.io
并尝试使用以下命令应用它:
kubectl apply -f default-sa-rolebinding.yaml
我收到以下错误:
clusterrolebinding "pod-reader" created
Error from server (Forbidden): error when creating "default-sa-rolebinding.yaml"
: clusterroles.rbac.authorization.k8s.io "pod-reader" is forbidden: attempt to g
rant extra privileges: [PolicyRule{Resources:["pods"], APIGroups:[""], Verbs:["g
et"]} PolicyRule{Resources:["pods"], APIGroups:[""], Verbs:["watch"]} PolicyRule
{Resources:["pods"], APIGroups:[""], Verbs:["list"]}] user=&{xyz@gmail.
com [system:authenticated] map[authenticator:[GKE]]} ownerrules=[PolicyRule{Res
ources:["selfsubjectaccessreviews"], APIGroups:["authorization.k8s.io"], Verbs:[
"create"]} PolicyRule{NonResourceURLs:["/api" "/api/*" "/apis" "/apis/*" "/healt
hz" "/swagger-2.0.0.pb-v1" "/swagger.json" "/swaggerapi" "/swaggerapi/*" "/versi
on"], Verbs:["get"]}] ruleResolutionErrors=[]
我不知道如何从这里开始。我的方法是正确的还是我在这里遗漏了什么?
更新:根据@JanosLenart在cmets中引用的帖子,修改了kubectl命令,没有观察到上述错误。但是访问 API,仍然给出 401 错误。我使用的 curl 命令是:
curl -k -1 -H "Authorization: Bearer <token>" https://<ip-address>/api/v1/namespaces/default/pods -v
【问题讨论】:
-
GKE clusterrolebinding for cluster-admin fails with permission error 的可能重复项。否则你的方法是合理的!
-
@JanosLenart 另一个帖子,解决了我在创建集群角色时遇到的禁止错误。但是访问 API,仍然会出现 401 错误。我使用的 curl 命令是:
curl -k -1 -H "Authorization: Bearer <token>" https://<ip-address>/api/v1/namespaces/default/pods -v
标签: kubernetes google-kubernetes-engine