如何在集群角色 kubernetes 中包含或排除特定命名空间

How to include or exclude specific namespaces in cluster role kubernetes

我正在尝试创建一个守护程序集,它将从特定名称空间的节点中的所有 pods 收集日志。我不确定如何指定命名空间名称。

我有一个命名空间日志记录,我在其中部署了守护程序集。我创建了一个 serviceccount 如下

apiVersion: v1
kind: ServiceAccount
metadata:
  name: fluent-bit
  namespace: logging

我的集群角色如下所示

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: fluent-bit-read
rules:
- apiGroups: [""]
  resources:
  - namespaces
  - pods
  verbs: ["get", "list", "watch"]

角色绑定

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: fluent-bit-read
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: fluent-bit-read
subjects:
- kind: ServiceAccount
  name: fluent-bit
  namespace: logging

现在守护进程从路径 /var/log/containers/*.log 收集日志,该路径目前在所有命名空间中都有来自容器 运行 的日志文件。有什么方法可以限制这个守护进程只从我需要的命名空间收集日志吗?

这是我们在 k8s 文档中的内容 (link)。

A Role always sets permissions within a particular namespace; when you create a Role, you have to specify the namespace it belongs in.

ClusterRole, by contrast, is a non-namespaced resource. The resources have different names (Role and ClusterRole) because a Kubernetes object always has to be either namespaced or not namespaced; it can’t be both.

因此,在您的情况下,您需要使用 RoleRoleBinding 而不是 ClusterRole 和 ClusterRoleBinding。

您必须调整位配置以仅读取您想要的日志文件。或者,如果您愿意,可以使用路由规则。这与 Kubernetes API 无关,Bit 通过绑定挂载直接从磁盘读取日志。