在Kubernetes(K8s)中蹬屹,權(quán)限管理是確保集群安全性和合規(guī)性的關(guān)鍵組成部分。Role-Based Access Control(RBAC)是K8s中使用最廣泛的權(quán)限管理模型之一佣谐,它允許管理員定義和控制用戶(hù)、服務(wù)賬戶(hù)等實(shí)體對(duì)于資源的訪問(wèn)權(quán)限。本文將深入探討K8s中的RBAC模型乖杠,包括其基本概念、核心組件澄成、使用方法以及詳細(xì)示例胧洒。
RBAC基本概念
角色(Role)
角色是RBAC的基本單元,用于定義對(duì)資源的一組權(quán)限墨状。角色是獨(dú)立于命名空間的卫漫,可以在整個(gè)集群范圍內(nèi)使用。角色綁定(RoleBinding)
角色綁定用于將角色與用戶(hù)肾砂、服務(wù)賬戶(hù)等實(shí)體綁定在一起列赎,賦予其相應(yīng)的權(quán)限。通過(guò)角色綁定镐确,可以實(shí)現(xiàn)將某個(gè)用戶(hù)或服務(wù)賬戶(hù)與特定的權(quán)限關(guān)聯(lián)起來(lái)包吝。集群角色(ClusterRole)
集群角色與角色類(lèi)似,但作用于整個(gè)集群源葫,而不是單個(gè)命名空間漏策。它允許定義對(duì)集群級(jí)別資源的權(quán)限。集群角色綁定(ClusterRoleBinding)
與角色綁定類(lèi)似臼氨,集群角色綁定用于將集群角色與用戶(hù)掺喻、服務(wù)賬戶(hù)等實(shí)體綁定在一起,賦予其在整個(gè)集群中的權(quán)限储矩。
RBAC核心組件
- Role
以下是一個(gè)簡(jiǎn)單的Role定義的示例感耙,該Role允許用戶(hù)對(duì)Pod進(jìn)行g(shù)et和list操作:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
- RoleBinding
創(chuàng)建RoleBinding將用戶(hù)綁定到上述Role:
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: read-pods
namespace: default
subjects:
- kind: User
name: "john"
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
- ClusterRole
以下是一個(gè)簡(jiǎn)單的ClusterRole定義的示例比勉,該ClusterRole允許用戶(hù)對(duì)Nodes進(jìn)行g(shù)et操作:
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: node-reader
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get"]
- ClusterRoleBinding
創(chuàng)建ClusterRoleBinding將用戶(hù)綁定到上述ClusterRole:
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: read-nodes
subjects:
- kind: User
name: "john"
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: node-reader
apiGroup: rbac.authorization.k8s.io
RBAC的使用方法
- 創(chuàng)建Role和RoleBinding
首先斯碌,創(chuàng)建一個(gè)Role,定義資源和權(quán)限:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
接下來(lái)兼贡,創(chuàng)建一個(gè)RoleBinding屡拨,將用戶(hù)綁定到上述Role:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: default
subjects:
- kind: User
name: "john"
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
- 創(chuàng)建ClusterRole和ClusterRoleBinding
創(chuàng)建一個(gè)ClusterRole只酥,定義對(duì)Nodes資源的權(quán)限:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: node-reader
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get"]
創(chuàng)建一個(gè)ClusterRoleBinding褥实,將用戶(hù)綁定到上述ClusterRole:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: read-nodes
subjects:
- kind: User
name: "john"
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: node-reader
apiGroup: rbac.authorization.k8s.io
RBAC示例演示
假設(shè)我們有一個(gè)Pod,其定義如下:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
namespace: default
spec:
containers:
- name: nginx
image: nginx
使用上述創(chuàng)建的Role和RoleBinding裂允,用戶(hù)john將具有g(shù)et和list的權(quán)限:
使用用戶(hù)john的身份驗(yàn)證
kubectl auth can-i get pods --as john
kubectl auth can-i list pods --as john
同樣损离,使用創(chuàng)建的ClusterRole和ClusterRoleBinding,用戶(hù)john將具有對(duì)Nodes資源的get權(quán)限:
使用用戶(hù)john的身份驗(yàn)證
kubectl auth can-i get nodes --as john
通過(guò)以上示例绝编,我們演示了如何使用RBAC在Kubernetes中定義和控制用戶(hù)對(duì)資源的權(quán)限僻澎。RBAC通過(guò)細(xì)粒度的訪問(wèn)控制,有力地保護(hù)了Kubernetes集群中的資源十饥,確保了集群的安全性和合規(guī)性窟勃。
結(jié)論
通過(guò)本文,我們深入了解了Kubernetes中權(quán)限管理模型RBAC的基本概念逗堵、核心組件秉氧,并通過(guò)詳細(xì)的示例演示了如何創(chuàng)建Role、RoleBinding蜒秤、ClusterRole和ClusterRoleBinding谬运,以及如何驗(yàn)證用戶(hù)對(duì)資源的權(quán)限。RBAC是Kubernetes中一個(gè)強(qiáng)大而靈活的權(quán)限管理工具垦藏,可以根據(jù)實(shí)際需求為不同用戶(hù)和服務(wù)賬戶(hù)分配適當(dāng)?shù)臋?quán)限,確保了集群的安全性伞访。在實(shí)際使用中掂骏,需要根據(jù)集群規(guī)模和業(yè)務(wù)需求,合理設(shè)計(jì)和配置RBAC規(guī)則厚掷,以達(dá)到最佳的安全實(shí)踐弟灼。