【问题标题】:Not able to fetch namespace, nodes & pods data from Alibaba cluster无法从阿里巴巴集群中获取命名空间、节点和 Pod 数据
【发布时间】:2022-01-08 22:41:45
【问题描述】:

我在阿里巴巴上创建了一个集群。

我需要在 Golang 项目中获取集群数据。

从 API 得到以下错误:

{
  "Code": 403,
  "Message": "namespaces is forbidden: User \"281247226166595041\" cannot list resource \"namespaces\" in API group \"\" at the cluster scope"
}

尝试通过 kubectl 访问它:

$ kubectl get namespace
Error from server (Forbidden): namespaces is forbidden: User "225396037912844073" cannot list resource "namespaces" in API group "" at the cluster scope

无法获取其他用户创建的集群的数据。

请帮我解决这个问题。

【问题讨论】:

    标签: go kubernetes alibaba-cloud


    【解决方案1】:

    这是身份验证问题

    您的用户无权列出该命名空间。

    您需要更新 RBAC 或用户访问权限,或者您正在将 Go 客户端身份验证到 Kubernetes。

    如果您使用服务帐户授予访问权限,请检查 RBAC。

    授予服务帐号cluster-admin访问权限

    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: <new-account-name>
      namespace: <namespace>
    
    ---
    apiVersion: rbac.authorization.k8s.io/v1beta1
    kind: ClusterRoleBinding
    metadata:
      name: go-rbac
    subjects:
      - kind: ServiceAccount
        name: default
        namespace: default
    roleRef:
      kind: ClusterRole
      name: cluster-admin
      apiGroup: rbac.authorization.k8s.io
    

    kubectl apply -f .yaml

    如果您在集群外运行,请确保您的脚本指向正确的 kubeconfig

    package main
    
    import (
      "fmt"
    
      "k8s.io/client-go/1.5/kubernetes"
      "k8s.io/client-go/1.5/pkg/api/v1"
      "k8s.io/client-go/1.5/tools/clientcmd"
    )
    
    func main()  {
        config, err := clientcmd.BuildConfigFromFlags("", <kube-config-path>)
        if err != nil {
          return nil, err
        }
    
        c, err := kubernetes.NewForConfig(config)
        if err != nil {
          return nil, err
        }
    

    在卡斯特内部运行

    package main
    
    import (
        "context"
        "fmt"
        "time"
    
        "k8s.io/apimachinery/pkg/api/errors"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        "k8s.io/client-go/kubernetes"
        "k8s.io/client-go/rest"
        //
        // Uncomment to load all auth plugins
        // _ "k8s.io/client-go/plugin/pkg/client/auth"
        //
        // Or uncomment to load specific auth plugins
        // _ "k8s.io/client-go/plugin/pkg/client/auth/azure"
        // _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
        // _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
        // _ "k8s.io/client-go/plugin/pkg/client/auth/openstack"
    )
    
    func main() {
        // creates the in-cluster config
        config, err := rest.InClusterConfig()
        if err != nil {
            panic(err.Error())
        }
        // creates the clientset
        clientset, err := kubernetes.NewForConfig(config)
        if err != nil {
            panic(err.Error())
        }
    

    例如:https://github.com/kubernetes/client-go/blob/master/examples/in-cluster-client-configuration/main.go

    【讨论】:

    • 阿里集群的服务账号如何获取,能帮我一下吗?
    • 你是如何运行代码的?在 K8s 上的容器内还是在 K8s 集群外?
    • @PranitaT 你还在面对问题吗?如果它对你有用,你能更新一下状态吗?
    猜你喜欢
    • 2019-07-04
    • 2020-09-22
    • 1970-01-01
    • 2021-12-18
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 2020-01-13
    相关资源
    最近更新 更多