Kubernetes授權主要是用於對集群資源的訪問控制,通過檢查請求包含的相關屬性值,與相對應的訪問策略相比較,API請求必須滿足某些策略才能被處理。跟認證類似,Kubernetes 也支持多種授權機制,並支持同時開啟多個授權插件(只要有一個驗證通過即可)。如果授權成功,則用戶的請求會發送到準入控制模塊做進一步的請求驗證,反之,授權失敗的請求則返回HTTP 403。
Kubernetes授權後,僅處理以下請求屬性:
user, group, extra;API、請求方法(如get、post、update、patch和delete)和請求路徑(如 /api);請求資源和子資源;Namespace;API Group;Kubernetes的授權是基於插件形式的,其常用的授權插件有以下幾種:
Node(節點認證)ABAC(基於屬性的訪問控制)RBAC(基於角色的訪問控制)Webhook(基於http回調機制的訪問控制)本章節中主要學習RBAC授權
一、RBAC
RBAC是指基於角色的訪問控制(Role-based Access Control,RBAC)的授權模式。主要是引入了角色(Role)和角色綁定(RoleBinding)的抽象概念。在ABAC中,K8s集群中的訪問策略只能跟用戶直接關聯;而在RBAC中,訪問策略可以跟某個角色關聯,具體的用戶在跟一個或多個角色相關聯。顯然,RBAC像其他新功能一樣,每次引入新功能,都會引入新的API對象,從而引入新的概念抽象,而這一新的概念抽象一定會使集群服務管理和使用更容易擴展和重用。
在使用 RBAC 時,只需要在啟動 kube-apiserver 時配置 --authorization-mode=RBAC 即可。
RBAC讓一個用戶(Users)扮演一個角色(Role),角色擁有權限,從而讓用戶擁有這樣的權限,隨後在授權機制當中,只需要將權限授予某個角色,此時用戶將獲取對應角色的權限,從而實現角色的訪問控制。
RBAC引入了4個新的頂級資源對象:Role、ClusterRole、RoleBinding、ClusterRoleBinding。同其他API資源對象一樣,用戶可以使用kubectl或者API調用等方式操作這些資源對象。
1、角色(Role)
角色就是指一組權限的集合,這裡的權限都是許可形式的,不存在拒絕的規則。在一個命名空間中,可以用角色來定義一個角色,如果是集群級別的,就需要使用ClusterRole了。
2、集群角色(ClusterRole)
對多namespace和集群級的資源或者是非資源類的 API(如 /healthz)使用集群角色ClusterRole。其級別是集群級別範圍的。
3、角色綁定(RoleBinding)
RoleBinding(角色綁定)把角色Role的權限映射到用戶或者用戶組,從而讓用戶繼承角色Role在namespace中的權限。RoleBinding也可以引用ClusterRole,對屬於同一命名空間內ClusterRole定義的資源主體進行授權。
4、集群角色綁定(ClusterRoleBinding)
ClusterRoleBinding讓用戶繼承ClusterRole在整個集群中的權限。ClusterRoleBinding為集群範圍內授權。
5、工作流程
基於資源申明和管理方法申明組成rule規則列表,和Role(角色)或ClusterRole(集群角色)對象進行綁定,Role類的對象將擁有所申明資源及的指定方法的權限。將Role權限落到實際用戶或程序上,即RoleBinding(角色綁定)或CLusterRoleBinding(集群角色綁定)。
Role角色應用有兩種方式:1、User account是為人設計的:則將Role與User(或Group)綁定(需要創建User/Group);過程如下:
2、Service account是為程序使用:將Role與ServiceAccount指定(這需要創建ServiceAccount並且在deployment(或者應用程式)中指定ServiceAccount)。而service account則是僅局限它所在的namespace,其特點如下:
(1)每個namespace都會自動創建一個default service account
(2)Token controller檢測service account的創建,並為它們創建secret;
(3)開啟ServiceAccount Admission Controller後 ;
(4)每個Pod在創建後都會自動設置spec.serviceAccount為default(除非指定了其他ServiceAccout);
(5)驗證Pod引用的service account已經存在,否則拒絕創建;
(6)如果Pod沒有指定ImagePullSecrets,則把service account的ImagePullSecrets加到Pod中;
(7)每個container啟動後都會掛載該service account的token和ca.crt到container的/var/run/secrets/kubernetes.io/serviceaccount/目錄中。
關於service account在上一章節已經講過了,這裡就不再重複贅述。
二、RBAC操作演示
(一)Role和Rolebinding
1、創建角色
注意:一個Role對象只能用於授予對某一單一命名空間中資源的訪問權限
(1)查看role幫助
[root@k8s-master ~]# kubectl create role -h
(2)使用命令創建role
[root@k8s-master ~]# kubectl create role test-role --verb=get,list,watch --resource=pods指定role名稱--verb:指定權限--resource:指定資源或者資源組--dry-run:單跑模式並不會創建
[root@k8s-master ~]# kubectl get role test-rule
(3)使用yaml文件創建role
[root@k8s-master ~]# kubectl create role test-rule --verb=get,list,watch --resource=pods --dry-run -o yaml > test-role.yaml[root@k8s-master ~]# vim test-role.yamlapiVersion: rbac.authorization.k8s.io/v1kind: Role ##資源類型metadata: creationTimestamp: null name: test-rulenamespace: defaultrules:- apiGroups: ##對API內的資源進行操作 - "" ##空字符串:使用core API group resources: ##定義資源對象 - pods verbs: ##定義權限 - get - list - watch
[root@k8s-master ~]# kubectl apply -f test-role.yaml
[root@k8s-master ~]# kubectl get role test-rule
[root@k8s-master ~]# kubectl describe role test-rule
2、角色綁定
(1)查看rolebinding幫助
(2)使用命令創建rolebinding
[root@k8s-master ~]# kubectl create rolebinding test-rolebinding --role=test-role --user=dodo--role:指定綁定的角色--user:指定哪個用戶
[root@k8s-master ~]# kubectl get rolebinding
(3)使用yaml文件創建rolebinding
[root@k8s-master ~]# kubectl create rolebinding test-rolebinding --role=test-role --dry-run -o yaml > test-rolebinding.yaml[root@k8s-master ~]# vim test-rolebinding.yamlapiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: creationTimestamp: null name: test-rolebindingroleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: test-rulesubjects:- apiGroup: rbac.authorization.k8s.io kind: User name: dodo
[root@k8s-master ~]# kubectl apply -f test-rolebinding.yaml
[root@k8s-master ~]# kubectl get rolebinding
[root@k8s-master ~]# kubectl describe rolebinding test-rolebinding
[root@k8s-master ~]# kubectl config use-context dodo@kubernetes
(二)Clusterrole和Clusterrolebinding
1、創建clusterrole
[root@k8s-master ~]# vim test-clusterrole.yaml添加:apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: test-clusterrolerules:- apiGroups: - "" resources: - pods verbs: - get - list - watch
[root@k8s-master ~]# kubectl apply -f test-clusterrole.yaml
[root@k8s-master ~]# kubectl get clusterrole test-clusterrole
[root@k8s-master ~]# kubectl describe clusterrole test-clusterrole
2、創建clusterrolebinding
[root@k8s-master ~]# vim test-clusterrolebinding.yaml添加:apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: creationTimestamp: null name: test-clusterrolebindroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: test-clusterrolesubjects:- apiGroup: rbac.authorization.k8s.io kind: User name: dodo
[root@k8s-master ~]# kubectl apply -f test-clusterrolebinding.yaml
[root@k8s-master ~]# kubectl get clusterrolebindings test-clusterrolebind
[root@k8s-master ~]# kubectl describe clusterrolebindings test-clusterrolebind
3、驗證:
(1)查看資源
[root@k8s-master ~]# kubectl get pods
(2)切換用戶
[root@k8s-master ~]# kubectl config use-context dodo@kubernetes(2)查看資源
[root@k8s-master ~]# kubectl get pods
[root@k8s-master ~]# kubectl get pods -n kube-system
結論:沒有出現forbidden,因為用戶dodo已被授予get權限。
(3)刪除資源
[root@k8s-master ~]# kubectl delete -f test-web.yaml
結論:出現forbidden,無法刪除。原因是用戶dodo沒有delete(刪除)資源deployment的權限。