Kubernetes身份認(rèn)證和授權(quán)操作全攻略:上手操作Kubernetes授權(quán)

這是本系列文章中的第三篇,前兩篇文章分別介紹了Kubernetes訪問(wèn)控制以及身份認(rèn)證。本文將通過(guò)上手實(shí)踐的方式,帶你理解Kubernetes授權(quán)這一概念。
?

創(chuàng)新互聯(lián)建站專注于天元網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供天元營(yíng)銷型網(wǎng)站建設(shè),天元網(wǎng)站制作、天元網(wǎng)頁(yè)設(shè)計(jì)、天元網(wǎng)站官網(wǎng)定制、成都小程序開發(fā)服務(wù),打造天元網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供天元網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

Kubernetes身份認(rèn)證和授權(quán)操作全攻略:上手操作Kubernetes授權(quán)
?
在文章正式開始之前,我們先快速回顧一下我們實(shí)操過(guò)程中的環(huán)境和場(chǎng)景。我們正在處理生產(chǎn)環(huán)境中的集群,其中每個(gè)部分都與命名空間相關(guān)聯(lián)。現(xiàn)在,組里新來(lái)了一位同事叫Bob,我們?cè)谏掀坛讨袔椭鶥ob以engineering命名空間管理員的身份加入集群。并且他已經(jīng)獲得私鑰以及簽名證書來(lái)訪問(wèn)集群。

?
如果你還沒(méi)有完成上述操作,請(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è)新的部分。
?
Kubernetes身份認(rèn)證和授權(quán)操作全攻略:上手操作Kubernetes授權(quán)
?
我們現(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甚至無(wú)法在同一名稱空間中列出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命名空間中訪問(wèn)資源,我們需要給他授權(quán)。這可以通過(guò)創(chuàng)建具有適當(dāng)權(quán)限的角色然后將其綁定到用戶Bob來(lái)完成。實(shí)質(zhì)上,我們使用的是基于角色訪問(wèn)控制(RBAC)來(lái)允許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毫無(wú)關(guān)聯(lián)。我們需要通過(guò)角色綁定將角色中指定的權(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

?
讓我們來(lái)檢查一下Bob現(xiàn)在是否可以訪問(wè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在集群中的訪問(wèn)權(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中工作的。在本系列下一篇文章中,我們將來(lái)看看service account,保持關(guān)注喲~

標(biāo)題名稱:Kubernetes身份認(rèn)證和授權(quán)操作全攻略:上手操作Kubernetes授權(quán)
網(wǎng)頁(yè)URL:http://bm7419.com/article48/gejjhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、微信小程序品牌網(wǎng)站設(shè)計(jì)、App設(shè)計(jì)網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)