如何以只读模式访问 Microk8s?

How can I access Microk8s in Read only mode?

我想使用 µK8s 读取 K8s 的状态,但我不想拥有修改任何内容的权限。如何实现?

以下将授予我完全访问权限:

microk8s.kubectl  Insufficient permissions to access MicroK8s. You can either try again with sudo or add the user digital to the 'microk8s' group:

   sudo usermod -a -G microk8s digital    sudo chown -f -R digital ~/.kube

The new group will be available on the user's next login.

on Unix/Linux we can just set appropriate file/directory access permission - just rx, decrease shell limits (like max memory/open file descriptors), decrease process priority (nice -19). We are looking for similar solution for K8S

Kubernetes 中的此类解决方案是通过 RBAC(基于角色的访问控制)处理的。 RBAC 防止未经授权的用户查看或修改集群状态。因为 API 服务器公开了一个 REST 接口,用户通过向服务器发送 HTTP 请求来执行操作。用户通过在请求中包含凭据(身份验证令牌、用户名和密码或客户端证书)来验证自己。

对于 REST 客户端,您会得到 GETPOSTPUTDELETE 等。这些被发送到代表特定的特定 URL 路径REST API 资源(Pods、服务、部署等)。

RBAC auth 配置了两组:

  • Roles 和 ClusterRoles - 这指定可以执行哪些 actions/verbs
  • RoleBinding 和 ClusterRoleBindings - 将上述角色绑定到用户、组或服务帐户。

您可能已经发现 ClusterRole 就是您要找的那个。这将允许针对集群限制特定用户或组。 在下面的示例中,我们正在创建只能列出 pods 的 ClusterRole。命名空间被省略,因为 ClusterRoles 没有命名空间。

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: pod-viewer
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["list"]

此权限必须通过 ClusterRoleBinding 绑定:

apiVersion: rbac.authorization.k8s.io/v1
# This cluster role binding allows anyone in the "manager" group to list pods in any namespace.
kind: ClusterRoleBinding
metadata:
  name: list-pods-global
subjects:
- kind: Group
  name: manager # Name is case sensitive
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: pod-viewer
  apiGroup: rbac.authorization.k8s.io

因为您自己没有足够的权限,您必须联系适当的管理权限的人为您创建具有 ClusterRole: View 的用户。查看角色应该已经在集群中预定义 ( kubectl get clusterrole view)

如果您想阅读更多内容Kubernetes docs 很好地解释了它的整个授权概念。