io.fabric8.kubernetes.client.KubernetesClientException:禁止!配置的服务帐户无权访问

2024-04-28

Config config = new ConfigBuilder().withMasterUrl("https://c2.eu-de.containers.cloud.ibm.com:78945").build(); 尝试(KubernetesClient客户端=新的DefaultKubernetesClient(config)){

        client.pods().inNamespace("default").list().getItems().forEach(
                pod -> System.out.println(pod.getMetadata().getName())
        );

    } catch (KubernetesClientException ex) {
        // Handle exception
        ex.printStackTrace();
    }

我收到 io.fabric8.kubernetes.client.KubernetesClientException: 执行失败: GET at:https://c2.eu-de.containers.cloud.ibm.com:78945/api/v1/namespaces https://c2.eu-de.containers.cloud.ibm.com:78945/api/v1/namespaces。消息:禁止!配置的服务帐户无权访问。服务帐户可能已被撤销。命名空间被禁止:用户“system:serviceaccount:badefault”无法在集群范围的 API 组“”中列出资源“命名空间”。这个错误


从你的错误看来你的ServiceAccount没有执行该特定操作所需的访问权限。您已发布列表代码Pod对象,但你的错误是抱怨列表Namespace对象。

User "system:serviceaccount:badefault" cannot list resource "namespaces" in API group "" at the cluster scope

您可以向您的集群管理员提供访问权限ServiceAccount用这个命令:

kubectl create clusterrolebinding default-pod --clusterrole cluster-admin --serviceaccount=<namespace>:badefault

如果您不想授予它集群管理员访问权限,您可以定义 Custom ClusterRole 来限制您想要的 apiGroups 和资源ServiceAccount访问:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: badefault-cluster-role
rules:
- apiGroups:
  - "" 
  resources: 
  - pods
  - namespaces
  verbs:
  - create
  - delete
  - deletecollection
  - get
  - list
  - patch
  - update
  - watch

然后您可以定义一个ClusterRoleBinding绑定这个ClusterRole给你的ServiceAccount object:

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

有了这个你的ServiceAccount应该能够访问pods and namespace集群范围内的对象。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

io.fabric8.kubernetes.client.KubernetesClientException:禁止!配置的服务帐户无权访问 的相关文章

随机推荐