【问题标题】:Creating a cluster-wide readonly user in Kubernetes using RBAC for Grafana使用 RBAC for Grafana 在 Kubernetes 中创建集群范围的只读用户
【发布时间】:2019-09-26 03:06:21
【问题描述】:

我正在尝试配置用户,以便 Grafana 能够监控我的集群资源。我遵循了 Bitnami 指南here。并将这些步骤作为 Ansible playbook 找到 here

Here 是用户使用的 ClusterRole,here 是它与用户的绑定方式。

然而,尽管如此,当我使用生成的证书配置 Grafana 时,它会引发 this 错误。

此外,如果我尝试使用用户上下文运行任何命令,它会说我未经授权。

$> kubectl --context=grafana-prometheus-scraper get pods -n grafana
error: You must be logged in to the server (Unauthorized)

有什么想法吗?

【问题讨论】:

    标签: kubernetes ansible certificate grafana rbac


    【解决方案1】:

    这就是您的 ServiceAccount ClusterRole and ClusterRoleBinding 的样子。

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: grafana-prometheus-scraper
    
    ---
    
    apiVersion: rbac.authorization.k8s.io/v1beta1
    kind: ClusterRole
    metadata:
      name: grafana-prometheus-scraper
    rules:
      - apiGroups: [""]
        resources:
          - configmaps
          - secrets
          - nodes
          - pods
          - services
          - resourcequotas
          - replicationcontrollers
          - limitranges
          - persistentvolumeclaims
          - persistentvolumes
          - namespaces
          - endpoints
        verbs: ["list", "watch"]
      - apiGroups: ["extensions"]
        resources:
          - daemonsets
          - deployments
          - replicasets
          - ingresses
        verbs: ["list", "watch"]
      - apiGroups: ["apps"]
        resources:
          - daemonsets
          - deployments
          - replicasets
          - statefulsets
        verbs: ["list", "watch"]
      - apiGroups: ["batch"]
        resources:
          - cronjobs
          - jobs
        verbs: ["list", "watch"]
      - apiGroups: ["autoscaling"]
        resources:
          - horizontalpodautoscalers
        verbs: ["list", "watch"]
      - apiGroups: ["policy"]
        resources:
          - poddisruptionbudgets
        verbs: ["list", "watch"]
      - apiGroups: ["certificates.k8s.io"]
        resources:
          - certificatesigningrequests
        verbs: ["list", "watch"]
      - apiGroups: ["storage.k8s.io"]
        resources:
          - storageclasses
        verbs: ["list", "watch"]
      - apiGroups: ["autoscaling.k8s.io"]
        resources:
          - verticalpodautoscalers
        verbs: ["list", "watch"]
    
    ---
    
    apiVersion: rbac.authorization.k8s.io/v1beta1
    kind: ClusterRoleBinding
    metadata:
      name: grafana-prometheus-scraper
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: grafana-prometheus-scraper
    subjects:
    - kind: ServiceAccount
      name: grafana-prometheus-scraper
    

    创建集群角色时不必指定命名空间,因为这适用于整个集群。如果你想限制对特定命名空间的访问,你应该使用 Role 和 RoleBinding。

    Medium上有一篇关于Configuring RBAC For Your Kubernetes Service Accounts的非常好的文章,我强烈推荐。

    【讨论】:

    • 谢谢你,我相信 SA 对象需要一个命名空间字段,而 CRB 需要引用 SA 所在的命名空间,但这些清单有效。事实上,它们是我在迁移到用户而不是 SA 之前所做的 1:1。我迁移的原因是,在部署这些清单后,Prometheus 无法使用此 scrape_config 从集群中抓取任何元数据。知道为什么吗? github.com/zimmertr/Kubernetes-Manifests/blob/master/Grafana/…
    • 您可以在这一行看到,Prometheus 的部署清单配置为使用分配给该集群角色的已创建服务帐户。 github.com/zimmertr/Kubernetes-Manifests/blob/… 也许问题出在 Prometheus 配置上?
    • 你能发布你的 scrape_config 吗?
    【解决方案2】:

    kubernetes 中没有用户对象。创建一个服务帐户并在 clusterrolebinding 中更新它。

    subjects:
        - kind: User
          name: grafana-prometheus-scraper
    

    【讨论】:

    • 不同意服务帐户可能是更好的方法,但这个官方文档说用户是 Kubernetes 中的一个对象:kubernetes.io/docs/reference/access-authn-authz/rbac/…
    • 创建服务帐户并检查 rbac 角色。它应该工作
    • 使用 openssl 证书创建用户。一些用户使用这种方式来创建用户。首先,您需要检查 rbac 是否适用于集群中的服务帐户。然后将角色绑定映射到您使用 openssl 证书创建的用户,而不是服务帐户
    猜你喜欢
    • 2019-02-05
    • 2019-05-19
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 2020-07-14
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多