本文的kubernetes環境:http://www.javashuo.com/article/p-vjmoqwfx-gm.htmlnode
RBAC官方文檔:https://kubernetes.io/docs/reference/access-authn-authz/rbac/nginx
rbac.authorization.k8s.io
API Group 來實現受權決策,容許管理員經過 Kubernetes API 動態配置策略,要啓用RBAC,須要在 apiserver 中添加參數--authorization-mode=RBAC,若是使用的kubeadm安裝的集羣,都默認開啓了RBAC,能夠經過查看 Master 節點上 apiserver 的靜態Pod定義文件: [root@node-01 ~]# cat /etc/kubernetes/manifests/kube-apiserver.yaml ··· - --authorization-mode=Node,RBAC ···
Kubernetes有一個很基本的特性就是它的全部資源對象都是模型化的 API 對象,容許執行增、刪、改、查等操做,好比下面的這下資源:json
上面這些資源對象的可能存在的操做有:api
Kubernetes 並不會存儲由認證插件從客戶端請求中提取出的用戶及所屬組的信息,它們僅僅用於檢驗用戶是否有權限執行其所請求的操做。tomcat
客戶端訪問API服務的途徑一般有三種:kubectl、客戶端庫或者直接使用 REST接口進行請求。app
而能夠執行此類請求的主體也被 Kubernetes 分爲兩類:現實中的「人」和 Pod 對象, 它們的用戶身份分別對應於常規用戶 (User Account )和服務帳號 ( Service Account) 。 ide
Kubernetes 有着如下幾個內建的用於特殊目的的組 。測試
RoleBinding用於將Role上的許可權限綁定到一個或一組用戶之上,它隸屬於且僅能做用於一個命名空間。綁定時,能夠引用同一名稱中的Role,也能夠引用集羣級別的 ClusterRole。jsonp
Role、RoleBinding、ClusterRole和ClusterRoleBinding 的關係如圖 所示 。
spa
下面咱們來建立一個User Account,測試訪問某些咱們受權的資源:
建立user私鑰
[root@node-01 ~]# cd /etc/kubernetes/pki/ [root@node-01 pki]# (umask 077;openssl genrsa -out billy.key 2048) Generating RSA private key, 2048 bit long modulus .................................................................................+++ ..................+++ e is 65537 (0x10001)
建立證書籤署請求
O=組織信息,CN=用戶名
[root@node-01 pki]# openssl req -new -key billy.key -out billy.csr -subj "/O=jbt/CN=billy"
[root@node-01 pki]# openssl x509 -req -in billy.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out billy.crt -days 365 Signature ok subject=/O=jbt/CN=billy Getting CA Private Key
建立配置文件主要有如下幾個步驟:
kubectl config set-cluster --kubeconfig=/PATH/TO/SOMEFILE #集羣配置 kubectl config set-credentials NAME --kubeconfig=/PATH/TO/SOMEFILE #用戶配置 kubectl config set-context #context配置 kubectl config use-context #切換context
- --embed-certs=true的做用是不在配置文件中顯示證書信息。
- --kubeconfig=/root/billy.conf用於建立新的配置文件,若是不加此選項,則內容會添加到家目錄下.kube/config文件中,可使用use-context來切換不一樣的用戶管理k8s集羣。
- context簡單的理解就是用什麼用戶來管理哪一個集羣,即用戶和集羣的結合。
建立集羣配置
[root@node-01 pki]# kubectl config set-cluster k8s --server=https://10.31.90.200:8443 --certificate-authority=ca.crt --embed-certs=true --kubeconfig=/root/billy.conf Cluster "k8s" set. [root@node-01 pki]# kubectl config view --kubeconfig=/root/billy.conf apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: https://10.31.90.200:8443 name: k8s contexts: [] current-context: "" kind: Config preferences: {} users: []
建立用戶配置
[root@node-01 pki]# kubectl config set-credentials billy --client-certificate=billy.crt --client-key=billy.key --embed-certs=true --kubeconfig=/root/billy.conf User "billy" set. #查看配置文件 [root@node-01 pki]# kubectl config view --kubeconfig=/root/billy.conf apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: https://10.31.90.200:8443 name: k8s contexts: [] current-context: "" kind: Config preferences: {} users: - name: billy user: client-certificate-data: REDACTED client-key-data: REDACTED
建立context配置
[root@node-01 pki]# kubectl config set-context billy@k8s --cluster=k8s --user=billy --kubeconfig=/root/billy.conf Context "billy@k8s" created. #查看配置文件 [root@node-01 pki]# kubectl config view --kubeconfig=/root/billy.conf apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: https://10.31.90.200:8443 name: k8s contexts: - context: cluster: k8s user: billy name: billy@k8s current-context: "" kind: Config preferences: {} users: - name: billy user: client-certificate-data: REDACTED client-key-data: REDACTED
切換context
[root@node-01 pki]# kubectl config use-context billy@k8s --kubeconfig=/root/billy.conf Switched to context "billy@k8s". #查看配置文件 [root@node-01 pki]# kubectl config view --kubeconfig=/root/billy.conf apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: https://10.31.90.200:8443 name: k8s contexts: - context: cluster: k8s user: billy name: billy@k8s current-context: billy@k8s kind: Config preferences: {} users: - name: billy user: client-certificate-data: REDACTED client-key-data: REDACTED
建立系統用戶及k8s驗證文件
[root@node-01 ~]# useradd billy #建立什麼用戶名均可以 [root@node-01 ~]# mkdir /home/billy/.kube [root@node-01 ~]# cp billy.conf /home/billy/.kube/config [root@node-01 ~]# chown billy.billy -R /home/billy/.kube/ [root@node-01 ~]# su - billy [billy@node-01 ~]$ kubectl get pod Error from server (Forbidden): pods is forbidden: User "billy" cannot list resource "pods" in API group "" in the namespace "default" #默認新用戶是沒有任何權限的。
此role只有pod的get、list、watch權限
[root@node-01 rbac]# cat pods-reader.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: pods-reader rules: - apiGroups: - "" resources: - pods verbs: - get - list - watch [root@node-01 rbac]# kubectl apply -f pods-reader.yaml role.rbac.authorization.k8s.io/pods-reader created
用戶billy和role pods-reader的綁定
[root@node-01 rbac]# cat billy-pods-reader.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: billy-pods-reader roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: pods-reader subjects: - apiGroup: rbac.authorization.k8s.io kind: User name: billy [root@node-01 rbac]# kubectl apply -f billy-pods-reader.yaml rolebinding.rbac.authorization.k8s.io/billy-pods-reader created
若是沒有指定命名空間的話,默認就是default命名空間。
[billy@node-01 ~]$ kubectl get pod NAME READY STATUS RESTARTS AGE nginx-demo-95bd675d5-66xrm 1/1 Running 0 18d tomcat-5c5dcbc885-7vr68 1/1 Running 0 18d [billy@node-01 ~]$ kubectl -n kube-system get pod Error from server (Forbidden): pods is forbidden: User "billy" cannot list resource "pods" in API group "" in the namespace "kube-system"
因此咱們是能夠查看查看default命名空間的pod,可是其餘空間的pod是沒法查看的。
[root@node-01 rbac]# cat cluster-reader.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: cluster-reader rules: - apiGroups: - "" resources: - pods verbs: - get - list - watch [root@node-01 rbac]# kubectl apply -f cluster-reader.yaml clusterrole.rbac.authorization.k8s.io/cluster-reader created
[root@node-01 rbac]# cat billy-read-all-pods.yaml apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRoleBinding metadata: name: billy-read-all-pods roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-reader subjects: - apiGroup: rbac.authorization.k8s.io kind: User name: billy [root@node-01 rbac]# kubectl apply -f billy-read-all-pods.yaml clusterrolebinding.rbac.authorization.k8s.io/billy-read-all-pods created
建立了ClusterRole和ClusterRoleBinding後就能夠看到全部命名空間的pod了。
[billy@node-01 ~]$ kubectl get pod NAME READY STATUS RESTARTS AGE nginx-demo-95bd675d5-66xrm 1/1 Running 0 18d tomcat-5c5dcbc885-7vr68 1/1 Running 0 18d [billy@node-01 ~]$ kubectl -n kube-system get pod NAME READY STATUS RESTARTS AGE canal-gd4qn 2/2 Running 0 21d cert-manager-6464494858-wqpnb 1/1 Running 0 18d coredns-7f65654f74-89x69 1/1 Running 0 18d coredns-7f65654f74-bznrl 1/1 Running 2 54d ...
至於ServiceAccount怎麼受權,其實相對user account來講更簡單,只需先建立ServiceAccount,而後建立role或者ClusterRole,最後在RoleBinding或ClusterRoleBinding綁定便可。如下簡單作一個示例,就不在顯示結果了,你們能夠本身去驗證。
kubectl create sa billy-sa
[root@node-01 rbac]# cat billy-sa-role.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: billy-sa-role rules: - apiGroups: - "" resources: - pods verbs: - get - list - watch
將billy-sa和billy-sa-role的綁定
[root@node-01 rbac]# cat billy-sa-rolebinding.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: billy-sa-rolebinding roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: billy-sa-role subjects: - kind: ServiceAccount name: billy-sa
建立完SA以後系統會自動建立一個secret,咱們能夠獲取這個secret裏面的token去登陸dashboard,就能夠看到相應有權限的資源。
kubectl get secret billy-sa-token-9rc55 -o jsonpath={.data.token} |base64 -d
還能夠在建立pod時在pod的spec裏指定serviceAccountName,那麼這個pod就擁有了對應的權限,具體的就不在演示了。
本次的分享就到此,若有問題歡迎在下面留言交流,但願你們多多關注和點贊,謝謝!