您好,登錄后才能下訂單哦!
這是本系列文章中的第三篇,前兩篇文章分別介紹了Kubernetes訪問控制以及身份認(rèn)證。本文將通過上手實(shí)踐的方式,帶你理解Kubernetes授權(quán)這一概念。
?
?
在文章正式開始之前,我們先快速回顧一下我們實(shí)操過程中的環(huán)境和場(chǎng)景。我們正在處理生產(chǎn)環(huán)境中的集群,其中每個(gè)部分都與命名空間相關(guān)聯(lián)?,F(xiàn)在,組里新來了一位同事叫Bob,我們?cè)谏掀坛讨袔椭鶥ob以engineering命名空間管理員的身份加入集群。并且他已經(jīng)獲得私鑰以及簽名證書來訪問集群。
?
如果你還沒有完成上述操作,請(qǐng)查看上篇教程,運(yùn)行其中的命令以完成環(huán)境設(shè)置以及為Bob配置證書。
?
好,我們正式開始本篇教程。
?
現(xiàn)在我們要給Bob授權(quán),以控制屬于engineering命名空間的資源。
?
首先,我們要為kubectl創(chuàng)建一個(gè)上下文(context),方便它在不同的環(huán)境之間切換。
?
kubectl config set-context eng-context \
--cluster=minikube \
--namespace=engineering \
--user=bob
Context "eng-context" created.
?
上面的命令使用Bob在minikube集群中的憑據(jù)創(chuàng)建了一個(gè)指向engineering命名空間的新上下文。這會(huì)導(dǎo)致在?/ .kube / config文件中添加一個(gè)新的部分。
?
?
我們現(xiàn)在在engineering命名空間中創(chuàng)建一個(gè)簡(jiǎn)單的pod:
?
apiVersion: v1
kind: Pod
metadata:
name: myapp
namespace: engineering
labels:
app: myapp
spec:
containers:
- name: myapp
image: busybox
command: ["/bin/sh", "-ec", "while :; do echo '.'; sleep 5 ; done"]
?
kubectl create -f myapp.yaml
pod/myapp created
?
kubectl get pods -n=engineering
NAME READY STATUS RESTARTS AGE
myapp 1/1 Running 0 89s
?
雖然您可以作為集群管理員在工程命名空間中創(chuàng)建和操作pod,但Bob甚至無法在同一名稱空間中列出pod。
?
kubectl get pods --namespace engineering --as bob
Error from server (Forbidden): pods is forbidden: User "bob" cannot list resource "pods" in API group
?
為了使得Bob可以在engineering命名空間中訪問資源,我們需要給他授權(quán)。這可以通過創(chuàng)建具有適當(dāng)權(quán)限的角色然后將其綁定到用戶Bob來完成。實(shí)質(zhì)上,我們使用的是基于角色訪問控制(RBAC)來允許Bob對(duì)engineering命名空間中的某些Kubernetes資源執(zhí)行特定操作。
?
創(chuàng)建一個(gè)名為eng-reader的Kubernetes角色,允許其在engineering命名空間中列出pod。
?
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: engineering
name: eng-reader
rules:
- apiGroups: [""] # "" indicates the core API group
resources: ["pods", "services", "nodes"]
verbs: ["get", "watch", "list"]
?
kubectl create -f role.yaml
role.rbac.authorization.k8s.io/eng-reader created
?
kubectl get roles --namespace=engineering
NAME AGE
eng-reader 58s
?
注意,這一角色目前和Bob毫無關(guān)聯(lián)。我們需要通過角色綁定將角色中指定的權(quán)限應(yīng)用于Bob。
?
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: eng-read-access
namespace: engineering
subjects:
- kind: User
name: bob # Name is case sensitive
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role #this must be Role or ClusterRole
name: eng-reader # this must match the name of the Role or ClusterRole you wish to bind to
apiGroup: rbac.authorization.k8s.io
?
kubectl create -f role-binding.yaml
rolebinding.rbac.authorization.k8s.io/eng-read-access created
?
kubectl get rolebindings --namespace=engineering
NAME AGE
eng-read-access 31s
?
讓我們來檢查一下Bob現(xiàn)在是否可以訪問pod。
?
kubectl get pods --namespace engineering --as bob
NAME READY STATUS RESTARTS AGE
myapp 1/1 Running 0 11m
?
既然他現(xiàn)在已經(jīng)關(guān)聯(lián)了eng-reader角色,那么他就獲得了pod列表的權(quán)限。
?
此時(shí),Bob在集群中的訪問權(quán)限依舊十分有限。他所能做的只是在engineering 命名空間中列出pod。這對(duì)Bob幫助不大。他想要檢查集群中的節(jié)點(diǎn)數(shù)量,但是令他失望的是,他遇到了 forbidden error。
?
kubectl get nodes --as bob
Error from server (Forbidden): nodes is forbidden: User "bob" cannot list resource "nodes" in API group
?
在Kubernetes中角色和角色綁定既可以應(yīng)用在命名空間層面也可以應(yīng)用在集群層面。我們現(xiàn)在創(chuàng)建一個(gè)集群角色以及一個(gè)與Bob關(guān)聯(lián)的角色綁定,以使他能夠列出節(jié)點(diǎn)。
?
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
# "namespace" omitted since ClusterRoles are not namespaced
name: cluster-node-reader
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get", "watch", "list"]
?
kubectl create -f cluster-role.yaml
clusterrole.rbac.authorization.k8s.io/cluster-node-reader created
?
kubectl get clusterroles cluster-node-reader
NAME AGE
cluster-node-reader 49s
?
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: read-cluster-nodes
subjects:
- kind: User
name: bob # Name is case sensitive
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: cluster-node-reader
apiGroup: rbac.authorization.k8s.io
?
kubectl create -f cluster-role-binding.yaml
clusterrolebinding.rbac.authorization.k8s.io/read-cluster-nodes created
?
kubectl get clusterrolebindings read-cluster-nodes
NAME AGE
read-cluster-nodes 35s
?
現(xiàn)在,Bob已經(jīng)設(shè)置為可以在集群中列出節(jié)點(diǎn)。
?
kubectl get nodes --as bob
NAME STATUS ROLES AGE VERSION
minikube Ready master 52m v1.15.2
?
本篇教程的目的是為了幫助你理解角色以及角色綁定如何在Kubernetes中工作的。在本系列下一篇文章中,我們將來看看service account,保持關(guān)注喲~
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。