Kubernetes身份認證和受權操做全攻略:上手操做Kubernetes受權

這是本系列文章中的第三篇,前兩篇文章分別介紹了Kubernetes訪問控制以及身份認證。本文將經過上手實踐的方式,帶你理解Kubernetes受權這一律念。node

在文章正式開始以前,咱們先快速回顧一下咱們實操過程當中的環境和場景。咱們正在處理生產環境中的集羣,其中每一個部分都與命名空間相關聯。如今,組裏新來了一位同事叫Bob,咱們在上篇教程中幫助Bob以engineering命名空間管理員的身份加入集羣。而且他已經得到私鑰以及簽名證書來訪問集羣。api

若是你尚未完成上述操做,請查看上篇教程,運行其中的命令以完成環境設置以及爲Bob配置證書。app

好,咱們正式開始本篇教程。this

如今咱們要給Bob受權,以控制屬於engineering命名空間的資源。spa

首先,咱們要爲kubectl建立一個上下文(context),方便它在不一樣的環境之間切換。code

kubectl config set-context eng-context \
	--cluster=minikube \
	--namespace=engineering \
	--user=bob
Context "eng-context" created.

上面的命令使用Bob在minikube集羣中的憑據建立了一個指向engineering命名空間的新上下文。這會致使在〜/ .kube / config文件中添加一個新的部分。server

咱們如今在engineering命名空間中建立一個簡單的pod:blog

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

雖然您能夠做爲集羣管理員在工程命名空間中建立和操做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命名空間中訪問資源,咱們須要給他受權。這能夠經過建立具備適當權限的角色而後將其綁定到用戶Bob來完成。實質上,咱們使用的是基於角色訪問控制(RBAC)來容許Bob對engineering命名空間中的某些Kubernetes資源執行特定操做。資源

建立一個名爲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毫無關聯。咱們須要經過角色綁定將角色中指定的權限應用於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如今是否能夠訪問pod。

kubectl get pods --namespace engineering --as bob
NAME    READY   STATUS    RESTARTS   AGE
myapp   1/1     Running   0          11m

既然他如今已經關聯了eng-reader角色,那麼他就得到了pod列表的權限。

此時,Bob在集羣中的訪問權限依舊十分有限。他所能作的只是在engineering 命名空間中列出pod。這對Bob幫助不大。他想要檢查集羣中的節點數量,可是令他失望的是,他遇到了 forbidden error。

kubectl get nodes --as bob
Error from server (Forbidden): nodes is forbidden: User "bob" cannot list resource "nodes" in API group

在Kubernetes中角色和角色綁定既能夠應用在命名空間層面也能夠應用在集羣層面。咱們如今建立一個集羣角色以及一個與Bob關聯的角色綁定,以使他可以列出節點。

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

如今,Bob已經設置爲能夠在集羣中列出節點。

kubectl get nodes --as bob
NAME       STATUS   ROLES    AGE   VERSION
minikube   Ready    master   52m   v1.15.2

本篇教程的目的是爲了幫助你理解角色以及角色綁定如何在Kubernetes中工做的。在本系列下一篇文章中,咱們未來看看service account,保持關注喲~

相關文章
相關標籤/搜索