系列目錄html
前面咱們的示例中,咱們建立的ServiceAccount是與cluster-admin 綁定的,這個用戶默認有最高的權限,實際生產環境中,每每須要對不一樣運維人員賦預不一樣的權限.而根據實際狀況也可能會賦予開發人員只讀的權限.這一節咱們將介紹如何建立不一樣權限的用戶.node
在開始以前,咱們先了解一些關於kubernetes RABA的一些基本概念.api
Role表示是一組規則權限,只能累加,Role能夠定義在一個namespace中,只能用於授予對單個命名空間中的資源訪問的權限。好比咱們新建一個對默認命名空間中Pods具備訪問權限的角色:app
kind: Role apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: namespace: default name: pod-reader rules: - apiGroups: [""] # "" indicates the core API group resources: ["pods"] verbs: ["get", "watch", "list"]
ClusterRole具備與Role相同的權限角色控制能力,不一樣的是ClusterRole是集羣級別的,能夠用於:運維
集羣級別的資源控制(例如 node 訪問權限)spa
非資源型 endpoints(例如 /healthz 訪問)code
全部命名空間資源控制(例如 pods)htm
好比咱們要建立一個受權某個特定命名空間或所有命名空間(取決於綁定方式)訪問secrets的集羣角色:blog
kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: # "namespace" omitted since ClusterRoles are not namespaced name: secret-reader rules: - apiGroups: [""] resources: ["secrets"] verbs: ["get", "watch", "list"]
RoloBinding能夠將角色中定義的權限授予用戶或用戶組,RoleBinding包含一組權限列表(subjects),權限列表中包含有不一樣形式的待授予權限資源類型(users、groups、service accounts),RoleBinding適用於某個命名空間內受權,而 ClusterRoleBinding適用於集羣範圍內的受權。token
好比咱們將默認命名空間的pod-reader角色授予用戶jane,這樣之後該用戶在默認命名空間中將具備pod-reader的權限:
# This role binding allows "jane" to read pods in the "default" namespace. kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-pods namespace: default subjects: - kind: User name: jane apiGroup: rbac.authorization.k8s.io roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io
RoleBinding一樣能夠引用ClusterRole來對當前 namespace 內用戶、用戶組或 ServiceAccount 進行受權,這種操做容許集羣管理員在整個集羣內定義一些通用的 ClusterRole,而後在不一樣的 namespace 中使用 RoleBinding 來引用
例如,如下 RoleBinding 引用了一個 ClusterRole,這個 ClusterRole 具備整個集羣內對 secrets 的訪問權限;可是其受權用戶 dave 只能訪問 development 空間中的 secrets(由於 RoleBinding 定義在 development 命名空間)
# This role binding allows "dave" to read secrets in the "development" namespace. kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-secrets namespace: development # This only grants permissions within the "development" namespace. subjects: - kind: User name: dave apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: secret-reader apiGroup: rbac.authorization.k8s.io
使用 ClusterRoleBinding 能夠對整個集羣中的全部命名空間資源權限進行受權;如下 ClusterRoleBinding 示例展現了受權 manager 組內全部用戶在所有命名空間中對 secrets 進行訪問
# This cluster role binding allows anyone in the "manager" group to read secrets in any namespace. kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-secrets-global subjects: - kind: Group name: manager apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: secret-reader apiGroup: rbac.authorization.k8s.io
有了上面的理論基礎,咱們就能夠來新建一個用戶,爲該用戶指定特定的訪問權限了,好比咱們要實現如下功能:
新增一個新的用戶tyler(tyler.yml)
該用戶只能對命名空間kube-system下面的pods和deployments進行管理
首先,咱們先建立這個ServiceAccount
kubectl create sa tyler -n kube-system
kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: namespace: kube-system name: role-tyler rules: - apiGroups: [""] resources: ["pods"] verbs: ["get", "watch", "list"] - apiGroups: ["extensions", "apps"] resources: ["deployments"] verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
注意上面的rules規則:管理pods和deployments的權限。
而後咱們建立一個角色綁定,將tyler用戶綁定到role-tyler角色,這樣用戶就擁有了角色的權限.
執行命令建立文件
$ kubect create -f tyler.yml $ kubect create -f role-tyler.yml
前面一節咱們使用的是命令來建立角色和已有的role綁定,這作方法簡單快捷,可是不便於像這裏同樣進行復雜的權限編排操做,也不便於版本管理.更爲複雜的權限編排建議使用yml聲明式的方式建立.
使用建立的token登錄
咱們前面說過,某一用戶的secret名稱爲用戶名-token-隨機字符串
格式.咱們可使用kubectl describe secret secret名稱 -n=kube-system
的方式查看secret,而後把token複製到dashboard登錄頁的token裏就能夠登錄了.