k8s創建用戶賬號——User Account


用戶賬戶和用戶組

Kubernetes 並不會存儲由認證插件從客戶端請求中提取出的用戶及所屬組的信息,它們僅僅用於檢驗用戶是否有權限執行其所請求的操作。

客戶端訪問API服務的途徑通常有三種:kubectl、客戶端庫或者直接使用 REST接口進行請求。

而可以執行此類請求的主體也被 Kubernetes 分為兩類:現實中的“人”和 Pod 對象,

它們的用戶身份分別對應於常規用戶 (User Account )和服務賬號 ( Service Account) 。

Use Account(用戶賬號):一般是指由獨立於Kubernetes之外的其他服務管理的用 戶賬號,例如由管理員分發的密鑰、Keystone一類的用戶存儲(賬號庫)、甚至是包 含有用戶名和密碼列表的文件等。Kubernetes中不存在表示此類用戶賬號的對象, 因此不能被直接添加進 Kubernetes 系統中 。

Service Account(服務賬號):是指由Kubernetes API 管理的賬號,用於為Pod 之中的服務進程在訪問Kubernetes API時提供身份標識( identity ) 。Service Account通常要綁定於特定的命名空間,它們由 API Server 創建,或者通過 API 調用於動創建 ,附帶着一組存儲為Secret的用於訪問API Server的憑據。

  Kubernetes 有着以下幾個內建的用於特殊目的的組 。system:unauthenticated :未能通過任何一個授權插件檢驗的賬號,即未通過認證測 試的用戶所屬的組 。system :authenticated :認證成功后的用戶自動加入的一個組,用於快捷引用所有正常通過認證的用戶賬號。system : serviceaccounts :當前系統上的所有 Service Account 對象。system :serviceaccounts :<namespace>:特定命名空間內所有的 Service Account 對象。

用戶驗證

盡管K8S認知用戶靠的只是用戶的名字,但是只需要一個名字就能請求K8S的API顯然是不合理的,所以依然需要驗證此用戶的身份

下面我們來創建一個User Account,測試訪問某些我們授權的資源:

在K8S中,有以下幾種驗證方式:

  • X509客戶端證書
    客戶端證書驗證通過為API Server指定--client-ca-file=xxx選項啟用,API Server通過此ca文件來驗證API請求攜帶的客戶端證書的有效性,一旦驗證成功,API Server就會將客戶端證書Subject里的CN屬性作為此次請求的用戶名
  • 靜態token文件
    通過指定--token-auth-file=SOMEFILE選項來啟用bearer token驗證方式,引用的文件是一個包含了 token,用戶名,用戶ID 的csv文件 請求時,帶上Authorization: Bearer 31ada4fd-adec-460c-809a-9e56ceb75269頭信息即可通過bearer token驗證
  • 靜態密碼文件
    通過指定--basic-auth-file=SOMEFILE選項啟用密碼驗證,類似的,引用的文件時一個包含 密碼,用戶名,用戶ID 的csv文件 請求時需要將Authorization頭設置為Basic BASE64ENCODED(USER:PASSWORD)

這里只介紹客戶端驗證

創建k8s User Account

1、創建證書

創建user私鑰

root@k8s-master:/etc/kubernetes/pki# (umask 077;openssl genrsa -out cbmljs.key 2048)
Generating RSA private key, 2048 bit long modulus
.......................+++
...........................................+++
e is 65537 (0x10001)
root@k8s-master:/etc/kubernetes/pki#

創建簽署請求

O=組織信息,CN=用戶名

root@k8s-master:/etc/kubernetes/pki#  openssl req -new -key cbmljs.key -out cbmljs.csr -subj "/O=k8s/CN=cbmljs"

簽署證書

root@k8s-master:/etc/kubernetes/pki# openssl  x509 -req -in cbmljs.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out cbmljs.crt -days 365

1、創建配置文件

創建配置文件主要有以下幾個步驟:

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/cbmljs.conf用於創建新的配置文件,如果不加此選項,則內容會添加到家目錄下.kube/config文件中,可以使用use-context來切換不同的用戶管理k8s集群。
* context簡單的理解就是用什么用戶來管理哪個集群,即用戶和集群的結合。

創建集群配置

root@k8s-master:/etc/kubernetes/pki# kubectl config set-cluster k8s --server=https://192.168.253.136:6443 --certificate-authority=ca.crt --embed-certs=true --kubeconfig=/root/cbmljs.conf
Cluster "k8s" set.
root@k8s-master:/etc/kubernetes/pki# kubectl config view --kubeconfig=/root/cbmljs.conf
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://192.168.253.136:6443
  name: k8s
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []

創建用戶配置

root@k8s-master:/etc/kubernetes/pki# kubectl config set-credentials cbmljs --client-certificate=cbmljs.crt --client-key=cbmljs.key --embed-certs=true --kubeconfig=/root/cbmljs.conf
User "cbmljs" set.
root@k8s-master:/etc/kubernetes/pki# kubectl config view --kubeconfig=/root/cbmljs.conf
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://192.168.253.136:6443
  name: k8s
contexts: []
current-context: ""
kind: Config
preferences: {}
users:
- name: cbmljs
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

創建context配置

root@k8s-master:/etc/kubernetes/pki# kubectl config set-context cbmljs@k8s --cluster=k8s --user=cbmljs --kubeconfig=/root/cbmljs.conf
Context "cbmljs@k8s" created.
root@k8s-master:/etc/kubernetes/pki# kubectl config view --kubeconfig=/root/cbmljs.conf
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://192.168.253.136:6443
  name: k8s
contexts:
- context:
    cluster: k8s
    user: cbmljs
  name: cbmljs@k8s
current-context: ""
kind: Config
preferences: {}
users:
- name: cbmljs
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

切換context

root@k8s-master:/etc/kubernetes/pki# kubectl config use-context cbmljs@k8s --kubeconfig=/root/cbmljs.conf
Switched to context "cbmljs@k8s".
root@k8s-master:/etc/kubernetes/pki# kubectl config view --kubeconfig=/root/cbmljs.conf
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://192.168.253.136:6443
  name: k8s
contexts:
- context:
    cluster: k8s
    user: cbmljs
  name: cbmljs@k8s
current-context: cbmljs@k8s
kind: Config
preferences: {}
users:
- name: cbmljs
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

創建系統用戶

root@k8s-master:~# mkdir -p /home/cbmljs/.kube
root@k8s-master:~# cp cbmljs.conf /home/cbmljs/.kube/config
root@k8s-master:~# chown cbmljs.cbmljs -R /home/cbmljs/
root@k8s-master:~# su - cbmljs

k8s驗證文件

cbmljs@k8s-master:~$ kubectl get pod
Error from server (Forbidden): pods is forbidden: User "cbmljs" cannot list resource "pods" in API group "" in the namespace "default"
cbmljs@k8s-master:~$

#默認新用戶是沒有任何權限的。

創建Role

此role只有pod的get、list、watch權限

root@k8s-master:~# kubectl create role pods-reader --verb=list,get,watch --resource=pods --dry-run -o yaml > ./role-demo.yaml
root@k8s-master:~# cat pods-reader.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: pods-reader rules: - apiGroups: - "" resources: - pods verbs: - get - list - watch

創建Rolebinding

用戶cbmljs和role pods-reader的綁定

root@k8s-master:~# kubectl create rolebinding cbmljs-pods-reader --user=cbmljs --role=pod-reader --dry-run -o yaml >./rolebinding-demo.yaml
root@k8s-master:~# cat cbmljs-pods-reader.yaml 
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: cbmljs-pods-reader
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: pods-reader
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: cbmljs

驗證結果

如果沒有指定命名空間的話,默認就是default命名空間。

cbmljs@k8s-master:~$ kubectl get pod
NAME                                      READY   STATUS    RESTARTS   AGE
callous-poodle-mychart-66d44dfd76-8xp27   2/2     Running   2          23h
flaskapp-v1-b5c74d74c-js2rt               2/2     Running   0          47h
flaskapp-v2-687794745-8664j               2/2     Running   0          47h
musty-scorpion-mysql-5f45d5647-dhbnj      0/2     Pending   0          23h
sleep-v1-5b99774dcf-ccjzp                 2/2     Running   2          2d22h
sleep-v2-b88548576-zgmc2                  2/2     Running   1          2d22h
cbmljs@k8s-master:~$
cbmljs@k8s-master:~$ kubectl get pod -n kube-system
Error from server (Forbidden): pods is forbidden: User "cbmljs" cannot list resource "pods" in API group "" in the namespace "kube-system"
cbmljs@k8s-master:~$

所以我們是可以查看查看default命名空間的pod,但是其他空間的pod是無法查看的。

創建ClusterRole

root@k8s-master:~# cat cluster-reader.yaml 
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: cluster-reader
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
  - list
  - watch

創建ClusterRolebinding

root@k8s-master:~# cat cbmljs-read-all-pod.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: cbmljs

驗證結果

創建了ClusterRole和ClusterRoleBinding后就可以看到所有命名空間的pod了。

cbmljs@k8s-master:~$ kubectl get pod
NAME                                      READY   STATUS    RESTARTS   AGE
callous-poodle-mychart-66d44dfd76-8xp27   2/2     Running   2          23h
flaskapp-v1-b5c74d74c-js2rt               2/2     Running   0          47h
flaskapp-v2-687794745-8664j               2/2     Running   0          47h
musty-scorpion-mysql-5f45d5647-dhbnj      0/2     Pending   0          24h
sleep-v1-5b99774dcf-ccjzp                 2/2     Running   2          2d22h
sleep-v2-b88548576-zgmc2                  2/2     Running   1          2d22h
 
cbmljs@k8s-master:~$ kubectl get pod -n kube-system
NAME                                 READY   STATUS    RESTARTS   AGE
coredns-5644d7b6d9-chmhn             1/1     Running   7          6d22h
coredns-5644d7b6d9-qxd4h             1/1     Running   7          6d22h
etcd-k8s-master                      1/1     Running   3          6d22h
kube-apiserver-k8s-master            1/1     Running   11         6d22h
kube-controller-manager-k8s-master   1/1     Running   20         6d22h
kube-flannel-ds-amd64-l9g62          1/1     Running   0          6d22h
kube-flannel-ds-amd64-nxtr9          1/1     Running   1          6d22h
kube-proxy-p8k57                     1/1     Running   0          6d22h
kube-proxy-s4n7b                     1/1     Running   1          6d22h
kube-scheduler-k8s-master            1/1     Running   21         6d22h
tiller-deploy-684c9f98f5-6ld5z       1/1     Running   14         3d3h

參考:
https://blog.csdn.net/cpongo1/article/details/89547625

https://blog.51cto.com/billy98/2380061

https://blog.51cto.com/11233559/2378013

https://docs.bitnami.com/tutorials/configure-rbac-in-your-kubernetes-cluster/


轉載自:https://blog.csdn.net/cbmljs/article/details/102953428


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM