尝试从Pod访问Kubernetes API时出现403禁止错误

时间:2019-10-21 11:48:23

标签: kubernetes google-cloud-platform kubernetes-apiserver

As per this Documentation,我正在尝试使用以下命令从pod中访问Kuberenetes API

curl --cacert ca.crt -H "Authorization: Bearer $(<token)" https://kubernetes/apis/extensions/v1beta1/namespaces/default/deployments/ballerina-prime/scale

遵循以下模板

curl --cacert ca.crt -H "Authorization: Bearer $(<token)" https://kubernetes/apis/extensions/v1beta1/namespaces/{namespace}/deployments/{name}/scale

它引发以下错误

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "deployments.extensions \"ballerina-prime\" is forbidden: User \"system:serviceaccount:default:default\" cannot get resource \"deployments/scale\" in API group \"extensions\" in the namespace \"default\"",
  "reason": "Forbidden",
  "details": {
    "name": "ballerina-prime",
    "group": "extensions",
    "kind": "deployments"
  },
  "code": 403
}

有人可以指出我在哪里出错,或者提出其他可以访问Kubernetes API的方法吗?

更新01

我根据建议的文档创建了一个角色。以下是我使用的清单。

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: deployments-and-deployements-scale
rules:
- apiGroups: [""]
  resources: ["deployments", "deployments/scale"]
  verbs: ["get", "list"]

我使用此命令将其应用。 kubectl apply -f deployments-and-deployements-scale.yaml。仍然无法访问所需的端点。我在哪里犯错?

2 个答案:

答案 0 :(得分:2)

首先,您已正确连接到kubernetes API!

但是您使用的默认服务帐户(“用户”)没有执行所需操作所需的特权。 (在名称空间“默认”中读取部署“芭蕾舞女演员-prima”)

您需要执行的操作:使用其他服务帐户或授予默认服务帐户所需的权限。

您可以在文档中找到详细信息:https://kubernetes.io/docs/reference/access-authn-authz/rbac/

答案 1 :(得分:2)

正如@Thomas在他的回答下方的评论中所述,您需要通过Role资源将特定的account分配给目标服务RoleBinding以便修复authorization问题。

参考您的清单:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: deployments-and-deployements-scale
rules:
- apiGroups: ["extensions", "apps"]
  resources: ["deployments", "deployments/scale"]
  verbs: ["get", "list"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: deployments-and-deployements-scale-rb
subjects:
- kind: ServiceAccount
  name: default
  namespace: default
roleRef:
  kind: Role
  name: deployments-and-deployements-scale
  apiGroup: ""

您可以考虑在角色定义中显式设置apiGroups:,以匹配特定的API组,或者广泛搜索["*"],以查找所有API版本。