io.kubernetes.client.openapi.ApiException: Forbidden 解决

使用spring-cloud-kubernetes时候,程序启动报错:

 .KubernetesClientConfigMapPropertySource : Unable to get ConfigMap xxx in namespace xxxx
io.kubernetes.client.openapi.ApiException: Forbidden 

其实是我们namespace默认的serviceaccount没有访问configmaps权限。

spring-cloud-kubernetes需要的权限包括:

 ["configmaps", "pods", "services", "endpoints", "secrets"]

并且仅仅需要读的权限。

 

 

 解决办法:

知道了错误原因,就知道咋解决了,就是给默认的serviceaccout设置上以上资源对象的读权限即可。

例如:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: 你自己的namespace
  name: default-read-role
rules:
  - apiGroups: ["", "extensions", "apps"]
    resources: ["configmaps", "pods", "services", "endpoints", "secrets"]
    verbs: ["get", "list", "watch"]

---

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: default-role-read-binding
  namespace: 你自己的namespace
subjects:
- kind: ServiceAccount
  name: default
  apiGroup: ""
roleRef:
  kind: Role
  name: default-read-role
  apiGroup: ""

以上只需要改namespace就能解决报错问题。

 

================================================

注意的是:Role指定的是默认的sa访问自己namespace的权限,如果要访问集群资源或者其它namespace的资源,请使用clusterrole和clusterrolebinding


版权声明:本文为u013189824原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。