在Kubernetes中创建适当的RBAC以便修改其他RBAC

33次阅读
没有评论

问题描述

想要通过CI/CD部署应用程序,但其中一些应用程序(例如Filebeat)会创建/更新ClusterRole和ClusterRole Bindings。当用户自己部署应用程序时,没有问题,因为用户是集群管理员。但是,如果用户尝试创建一些更受限制的与Service Account相关联的RBAC,就无法正确完成操作。用户尝试了不同的变体,但都遇到了问题。

错误示例

用户尝试的两种变体:

A)

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: filebeat-clusterrole-updater
subjects:
- kind: ServiceAccount
  name: filebeat
  namespace: filebeat
roleRef:
  kind: ClusterRole
  name: clusterrole-updater
  apiGroup: rbac.authorization.k8s.io

B)

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: filebeat-clusterrole-updater
  namespace: filebeat
subjects:
- kind: ServiceAccount
  name: filebeat
  namespace: filebeat
roleRef:
  kind: ClusterRole
  name: clusterrole-updater
  apiGroup: rbac.authorization.k8s.io

在这两种情况下,用户遇到了以下错误:

Error from server (Forbidden): clusterroles.rbac.authorization.k8s.io is forbidden: User "system:serviceaccount:filebeat:filebeat" cannot list resource "clusterroles" in API group "rbac.authorization.k8s.io" at the cluster scope

解决方案

要在Kubernetes中创建适当的RBAC以便修改其他RBAC,需要确保正确设置角色和权限。这里提供一个基本的解决方案来帮助你解决问题。请注意,具体的环境和要求可能会有所不同,因此你需要根据实际情况进行调整。

步骤

以下是创建适当的RBAC以便修改其他RBAC的基本步骤:

  1. 创建ClusterRole
    首先,你需要创建一个ClusterRole,授予你的Service Account在特定资源上的权限。这是你需要修改的ClusterRole。

  2. 创建Service Account
    创建一个新的Service Account,并将其与你的应用程序关联。

  3. 创建ClusterRoleBinding
    创建一个ClusterRoleBinding,将Service Account绑定到之前创建的ClusterRole上。

示例

以下是一个示例,展示如何创建适当的RBAC以便修改其他RBAC:

  1. 创建ClusterRole(例如clusterrole-updater):
    “`yaml
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
    name: clusterrole-updater
    rules:
  2. apiGroups: [“rbac.authorization.k8s.io”]
    resources: [“clusterroles”, “clusterrolebindings”]
    verbs: [“watch”, “get”, “list”, “update”]
    “`

  3. 创建Service Account(例如filebeat):
    bash
    kubectl create serviceaccount filebeat -n filebeat

  4. 创建ClusterRoleBinding将Service Account绑定到ClusterRole上:
    “`yaml
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
    name: filebeat-clusterrole-updater
    subjects:

  5. kind: ServiceAccount
    name: filebeat
    namespace: filebeat
    roleRef:
    kind: ClusterRole
    name: clusterrole-updater
    apiGroup: rbac.authorization.k8s.io
    “`

通过上述步骤,你将创建一个适当的RBAC配置,使得你的Service Account能够修改其他RBAC。请确保根据实际需求进行适当的调整。

请注意,RBAC的配置可能因Kubernetes版本和集群配置而有所不同。在实施任何更改之前,强烈建议你在测试环境中进行验证。

结论

通过按照上述步骤设置适当的ClusterRole、Service Account和ClusterRoleBinding,你可以实现在Kubernetes中创建RBAC以便修改其他RBAC的需求。请根据实际情况进行调整,并在进行任何更改之前在测试环境中进行验证。这将确保你的RBAC设置是正确且安全的。

正文完