k8s_dashboard权限控制

创建一个只允许访问default 命名空间下pod  的控制权限案例:

1、在默认名称空间中为仪表板创建服务帐户

kubectl create serviceaccount dashboard -n default
  • Role

Role表示是一组规则权限,只能累加,Role可以定义在一个namespace中,只能用于授予对单个命名空间中的资源访问的权限。

  • ClusterRole

ClusterRole具有与Role相同的权限角色控制能力,不同的是ClusterRole是集群级别的,可以用于:

  • 集群级别的资源控制(例如 node 访问权限)

  • 非资源型 endpoints(例如 /healthz 访问)

  • 所有命名空间资源控制(例如 pods)

  • RoleBinding和ClusterRoleBinding

RoloBinding可以将角色中定义的权限授予用户或用户组,RoleBinding包含一组权限列表(subjects),权限列表中包含有不同形式的待授予权限资源类型(users、groups、service accounts),RoleBinding适用于某个命名空间内授权,而 ClusterRoleBinding适用于集群范围内的授权。

  • RoleBinding同样可以引用ClusterRole来对当前 namespace 内用户、用户组或 ServiceAccount 进行授权,这种操作允许集群管理员在整个集群内定义一些通用的 ClusterRole,然后在不同的 namespace 中使用 RoleBinding 来引用
  • 使用 ClusterRoleBinding 可以对整个集群中的所有命名空间资源权限进行授权;

2、编写服务帐户的RBAC yaml文件

apiVersion: rbac.authorization.k8s.io/v1
kind: Role #Role表示是一组规则权限,只能累加,Role可以定义在一个namespace中,只能用于授予对单个命名空间中的资源访问的权限
metadata:
  name: dashboard-to-pod-role
  namespace: default
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources: ["pods/exec"]
  verbs: ["create"]

---

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: dashboard-to-pod-rolebinding
  namespace: default
subjects:
- kind: Group
  name: system:authenticated
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: dashboard-to-pod-role
  apiGroup: rbac.authorization.k8s.io

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: dashboard-to-pod-clusterrole
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources: ["pods/exec"]
  verbs: ["create"]

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: dashboard-to-pod-clusterrolebinding
subjects:
- kind: Group
  name: system:authenticated
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: dashboard-to-pod-clusterrole
  apiGroup: rbac.authorization.k8s.io

3、相关参数
1、Role、ClsuterRole Verbs可配置参数"get", "list", "watch", "create", "update", "patch", "delete", "exec"

2、Role、ClsuterRole Resource可配置参数"services", "endpoints", "pods","secrets","configmaps","crontabs","deployments","jobs","nodes","rolebindings","clusterroles","daemonsets","replicasets","statefulsets","horizontalpodautoscalers","replicationcontrollers","cronjobs"

3、Role、ClsuterRole APIGroup可配置参数"","apps", "autoscaling", "batch"

4、使用创建的token登陆

我们前面说过,某一用户的secret名称为用户名-token-随机字符串格式.我们可以使用kubectl describe secret secret名称 -n=kube-system的方式查看secret,然后把token复制到dashboard登陆页的token里就可以登陆了。例如:

kubectl describe secrets dashboard-to-pod-token-sjkh3

 

 

 

 可以发现只能对pod进行部分操作,即只有exec 到pod 内的权限。

 

posted @ 2023-04-07 16:40  听&夏  阅读(201)  评论(0编辑  收藏  举报