Kubernetes RBAC 為指定使用者授權訪問不同名稱空間許可權
技術標籤:k8skubernetes
在開啟了 TLS 的叢集中,每當與叢集互動的時候少不了的是身份認證,使用 kubeconfig(即證書) 和 token 兩種認證方式是最簡單也最通用的認證方式。
以kubectl為例介紹kubeconfig的配置。kubectl只是個go編寫的可執行程式,只要為kubectl配置合適的kubeconfig,就可以在叢集中的任意節點使用。kubectl預設會從$HOME/.kube目錄下查詢檔名為config
的檔案,也可以通過設定環境變數KUBECONFIG
或者通過設定--kubeconfig
去指定其它 kubeconfig 檔案。
總之kubeconfig就是為訪問叢集所作的配置。
生成的 kubeconfig 被儲存到~/.kube/config
檔案;配置檔案描述了叢集、使用者和上下文
案例:為指定使用者授權訪問不同名稱空間許可權
示例:為lulei使用者授權default名稱空間Pod讀取許可權(只能檢視,不能刪除)
1. 用K8S CA簽發客戶端證書基於證書的客戶端認證方式
2. 生成kubeconfig授權檔案kubectl使用kubeconfig連線叢集
3. 建立RBAC許可權策略做一定的許可權分配
也就是生成一個kubeconfig檔案,讓指定使用者拿著這個檔案去訪問叢集,如何檢視資源
CA簽發客戶端證書
(1)生成k8s客戶端證書,這裡面準備了CA的配置檔案
cat > lulei-csr.json <<EOF
{
"CN": "lulei",
這個欄位相當於客戶端使用者資訊的標識
[[email protected] ~]# ls /etc/kubernetes/pki/ca.crt
/etc/kubernetes/pki/ca.crt
cfssl使用根證書來簽發lulei客戶端的證書
[[email protected] rbac]# cat cfssl.sh wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 chmod +x cfssl* mv cfssl_linux-amd64 /usr/bin/cfssl mv cfssljson_linux-amd64 /usr/bin/cfssljson mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo
[[email protected] rbac]# cat cert.sh
cat > ca-config.json <<EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"kubernetes": {
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
],
"expiry": "87600h"
}
}
}
}
EOF
cat > lulei-csr.json <<EOF
{
"CN": "lulei",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "BeiJing",
"L": "BeiJing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cfssl gencert -ca=/etc/kubernetes/pki/ca.crt -ca-key=/etc/kubernetes/pki/ca.key -config=ca-config.json -profile=kubernetes lulei-csr.json | cfssljson -bare lulei
API Server會把客戶端證書的CN欄位作為User,把names.O欄位作為Group。kubelet 使用 TLS Bootstaping 認證時,API Server 可以使用 Bootstrap Tokens 或者 Token authenticationfile 驗證=token,無論哪一種,Kubenetes 都會為 token 繫結一個預設的 User 和 GroupPod使用 ServiceAccount 認證時,service-account-token 中的 JWT 會儲存 User 資訊有了使用者資訊,再建立一對角色/角色繫結(叢集角色/叢集角色繫結)資源物件,就可以完成許可權綁定了
[[email protected] rbac]# ls
lulei-key.pem lulei.pem
注意這裡要指定k8s根證書的,jubeadm部署的話根證書預設在/etc/kubernetes/pki/
[[email protected] rbac]# ls /etc/kubernetes/pki/
apiserver.crt apiserver-kubelet-client.crt etcd front-proxy-client.key
apiserver-etcd-client.crt apiserver-kubelet-client.key front-proxy-ca.crt sa.key
apiserver-etcd-client.key ca.crt front-proxy-ca.key sa.pub
apiserver.key ca.key front-proxy-client.crt
生成kubeconfig授權檔案
叢集引數設定
使用kubectl config這條命令生成kubeconfig證書,逐步生成kubeconfig裡面的資訊
生成證書的格式和家目錄的config內容是一樣的
[[email protected] ~]# cd .kube/
[[email protected] .kube]# ls
cache config
這裡填充了cluster的資訊
kubectl config set-cluster kubernetes \
--certificate-authority=/etc/kubernetes/pki/ca.crt \
--embed-certs=true \
--server=https://192.168.179.102:6443 \
--kubeconfig=lulei.kubeconfig
[[email protected] rbac]# kubectl config set-cluster kubernetes \
> --certificate-authority=/etc/kubernetes/pki/ca.crt \
> --embed-certs=true \
> --server=https://192.168.179.102:6443 \
> --kubeconfig=lulei.kubeconfig
Cluster "kubernetes" set.
這裡生成了檔案
[[email protected] rbac]# cat lulei.kubeconfig
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM1ekNDQWMrZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJd01URXhOVEEyTlRZeU5sb1hEVE13TVRFeE16QTJOVFl5Tmxvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBSmZsCmtaVVhUcVJ3ZjJVeDdqS1pjQURvaHV1aHNXcHAvK2NIOEF1MVpmR3JqN0lTb3VoelVISkZ5cGR5ekdoTDFBRHgKMTkxVlVCN1ZCWlk4cXBDRHYyRnZvWkRFbFVYR1JrTFFDT0lxc1hOVUt3ZU0xdnJqZnpZVW5ZNGN6NDhKNDdpTgpnUjJHRGtsWUlrZFpGZEU1ekN2eitldEtEVVUzdE5GV0djL00yZFE0NUIxNGZlTldpTUR3T0l3dTBSSWRvYmpnCndmSnhoVlJHMmgzdEplL0RpUjQ2cXI1NEdMVUU2VXNDUGJvZ0JxNTVBakt2c2NiOWUzeGdzMVJhZW1BdFZhd08KSFNjRkRDYWhBZDVPUlgzMlIzSGNoRnY2QmFwczgyb0dqUDBLOEY1RXNQWkFYQXFoK3c0UTFMRWQ2aHRuQWhyWAoxcmJ5cTF4eUNESnhhbENaSHBjQ0F3RUFBYU5DTUVBd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0hRWURWUjBPQkJZRUZPZXFad29TeUxrVDVKWXlRNURMamUzRFRHUjJNQTBHQ1NxR1NJYjMKRFFFQkN3VUFBNElCQVFCemtMRXMzUGhIN08wYWs5VWlTbFhDVTljK1RoYlZaTmZVUStKS1JpQU5KQys5Qm8vZApLdUlLYWhxQlE5UkprSk53TmVwT0NJOWxzcGJ4TGtHK3ArYktHV29Yb25CME5RdnZoZTRQd0p0dmdUcGlhVmVYCmNLejhUMDBRT1YyRzR0WEtmaTViK0dvZDA3NktlZm4zbm00U0l1VDh6bmNDclUxeHRpQTRSU3RQRVZZZTRQalAKY2p6MHJVS3BnTmhJL0lYc2JPVDNYV0hFNE4vU0VLcmtIYkRJNFpEUGRYa2gxd3piM1FZODFuZTh1enBNQVJ5VwpLbkx6R1NRaGJ0WTlWRUZkdlhGK0wreWFrQjcrR29xa1lJa1BNdURveFRoeTBuMFJjZStSTUhtZkFQSEh2RDFyCjdSNC9xdlpBLzA4Q0hxb3ZDcCsxUzNXbFE5WE5MRzE3TWExTAotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
server: https://192.168.179.102:6443
name: kubernetes
contexts: null
current-context: ""
kind: Config
preferences: {}
users: null
本段設定了所需要訪問的叢集的資訊。
- 使用set-cluster設定了需要訪問的叢集,如上為kubernetes,這只是個名稱,實際為--server指向的apiserver
- --certificate-authority設定了該叢集的公鑰
- --embed-certs為true表示將--certificate-authority證書寫入到kubeconfig中
- --server則表示該叢集的kube-apiserver地址
生成的kubeconfig 被儲存到lulei.kubeconfig
檔案
使用者引數設定
kubectl config set-credentials lulei \
--client-key=lulei-key.pem \
--client-certificate=lulei.pem \
--embed-certs=true \
--kubeconfig=lulei.kubeconfig
[[email protected] rbac]# kubectl config set-credentials lulei \
> --client-key=lulei-key.pem \
> --client-certificate=lulei.pem \
> --embed-certs=true \
> --kubeconfig=lulei.kubeconfig
User "lulei" set.
[[email protected] rbac]# cat lulei.kubeconfig
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM1ekNDQWMrZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQ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RpQTRSU3RQRVZZZTRQalAKY2p6MHJVS3BnTmhJL0lYc2JPVDNYV0hFNE4vU0VLcmtIYkRJNFpEUGRYa2gxd3piM1FZODFuZTh1enBNQVJ5VwpLbkx6R1NRaGJ0WTlWRUZkdlhGK0wreWFrQjcrR29xa1lJa1BNdURveFRoeTBuMFJjZStSTUhtZkFQSEh2RDFyCjdSNC9xdlpBLzA4Q0hxb3ZDcCsxUzNXbFE5WE5MRzE3TWExTAotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
server: https://192.168.179.102:6443
name: kubernetes
contexts: null
current-context: ""
kind: Config
preferences: {}
users:
- name: lulei
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURnakNDQW1xZ0F3SUJBZ0lVYVVuc3VhVnA1WkxVRnNmcXk2TWRaNGRQR0pFd0RRWUpLb1pJaHZjTkFRRUwKQlFBd0ZURVRNQkVHQTFVRUF4TUthM1ZpWlhKdVpYUmxjekFlRncweU1UQXhNVGd3T0RRd01EQmFGdzB6TVRBeApNVFl3T0RRd01EQmFNR0F4Q3pBSkJnTlZCQVlUQWtOT01SQXdEZ1lEVlFRSUV3ZENaV2xLYVc1bk1SQXdEZ1lEClZRUUhFd2RDWldsS2FXNW5NUXd3Q2dZRFZRUUtFd05yT0hNeER6QU5CZ05WQkFzVEJsTjVjM1JsYlRFT01Bd0cKQTFVRUF4TUZiSFZzWldrd2dnRWlNQTBHQ1NxR1NJYjNEUUVCQVFVQUE0SUJEd0F3Z2dFS0FvSUJBUUNZNWM2Kwo0bFZIbTJvMXR6bHcyd3dncFp1Sjd2RnJ3TWVtMUlqSkg2c1ZTSE92NnJBOStjUUFmYk50N0swM0NHa2JhVlh5ClJ0ejh6RitYL3BhZUNmZW8wNnFKcU5RZkVhMllQc1lyREZkdXRRdUNGSmpxdlBrbWN3TDdGdnVGcGlsNDZ4TkoKQ2s2QWpXNzh2TUNKQWx2dEtWcWdQdXFyd1lDTkNvZ0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N21TS1ZxMkVEZThKU1pFOUpEVCswbm5zaHd5bm5hdXZpbk8zZEF4Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
本段主要設定使用者的相關資訊,主要是使用者證書。如上的使用者名稱為lulei,證書為:lulei.pem,私鑰為:lulei-key.pem。注意客戶端的證書首先要經過叢集CA的簽署,否則不會被叢集認可。此處使用的是ca認證方式,也可以使用token認證,如kubelet的TLS Boostrap機制下的bootstrapping使用的就是token認證方式。上述kubectl使用的是ca認證,不需要token欄位
上下文引數
kubectl config set-context kubernetes \
--cluster=kubernetes \
--user=lulei \
--kubeconfig=lulei.kubeconfig
# 設定上下文引數
[[email protected] rbac]# kubectl config set-context kubernetes \
> --cluster=kubernetes \
> --user=lulei \
> --kubeconfig=lulei.kubeconfig
Context "kubernetes" created.
[[email protected] rbac]# cat lulei.kubeconfig
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM1ekNDQWMrZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJd01URXhOVEEyTlRZeU5sb1hEVE13TVRFeE16QTJOVFl5Tmxvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBSmZsCmtaVVhUcVJ3ZjJVeDdqS1pjQURvaHV1aHNXcHAvK2NIOEF1MVpmR3JqN0lTb3VoelVISkZ5cGR5ekdoTDFBRHgKMTkxVlVCN1ZCWlk4cXBDRHYyRnZvWkRFbFVYR1JrTFFDT0lxc1hOVUt3ZU0xdnJqZnpZVW5ZNGN6NDhKNDdpTgpnUjJHRGtsWUlrZFpGZEU1ekN2eitldEtEVVUzdE5GV0djL00yZFE0NUIxNGZlTldpTUR3T0l3dTBSSWRvYmpnCndmSnhoVlJHMmgzdEplL0RpUjQ2cXI1NEdMVUU2VXNDUGJvZ0JxNTVBakt2c2NiOWUzeGdzMVJhZW1BdFZhd08KSFNjRkRDYWhBZDVPUlgzMlIzSGNoRnY2QmFwczgyb0dqUDBLOEY1RXNQWkFYQXFoK3c0UTFMRWQ2aHRuQWhyWAoxcmJ5cTF4eUNESnhhbENaSHBjQ0F3RUFBYU5DTUVBd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0hRWURWUjBPQkJZRUZPZXFad29TeUxrVDVKWXlRNURMamUzRFRHUjJNQTBHQ1NxR1NJYjMKRFFFQkN3VUFBNElCQVFCemtMRXMzUGhIN08wYWs5VWlTbFhDVTljK1RoYlZaTmZVUStKS1JpQU5KQys5Qm8vZApLdUlLYWhxQlE5UkprSk53TmVwT0NJOWxzcGJ4TGtHK3ArYktHV29Yb25CME5RdnZoZTRQd0p0dmdUcGlhVmVYCmNLejhUMDBRT1YyRzR0WEtmaTViK0dvZDA3NktlZm4zbm00U0l1VDh6bmNDclUxeHRpQTRSU3RQRVZZZTRQalAKY2p6MHJVS3BnTmhJL0lYc2JPVDNYV0hFNE4vU0VLcmtIYkRJNFpEUGRYa2gxd3piM1FZODFuZTh1enBNQVJ5VwpLbkx6R1NRaGJ0WTlWRUZkdlhGK0wreWFrQjcrR29xa1lJa1BNdURveFRoeTBuMFJjZStSTUhtZkFQSEh2RDFyCjdSNC9xdlpBLzA4Q0hxb3ZDcCsxUzNXbFE5WE5MRzE3TWExTAotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
server: https://192.168.179.102:6443
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: lulei
name: kubernetes
current-context: ""
kind: Config
preferences: {}
users:
- name: lulei
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURnakNDQW1xZ0F3SUJBZ0lVYVVuc3VhVnA1WkxVRnNmcXk2TWR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eW1sdWJaRjExZVVJTXdIYkloVzI1d25ScUIxekNKV1h4YlgxMUFzZFVGam9IOGxPL05NMTNSVng4bDFxRAowcExhS3J0MFNKNFRJR2NsWVpLWUEzL0dkckdvRStxQ2tQYlZLYVF6NXJXVjNjS0I1TmM1cG4wMjJ4Yk1qQmVwCitYQng0WHpVZTJjMFplMnFEMWdjTFFLQmdDNy9ETzlMNGxQaWNQbUNjUURWelVCL0hNeXAySUk3SWJFa3N0VmsKakdhWVFjNG9sellqb1VNc0RnZXhzYktTdHQ0WEJhZmhySzVXWTBGbEgrb1FDY1RtRE9lS1A2U3Jmc25jN3VMaQo1bGhFWE1CbEQ0WEZKU01FaVJlMFFvZDMyNjhmeER2aHhqR09ZQXc4a2thZFNsRExqL2pDclgzRWptc1gveHZhCkxPVDVBb0dCQUw0azE0NVdnQlRCOWZRY05iOE01SXNNZmx1QzhHc2FNbDgzQnU3T2x6T3lLanFMbGxSdzlIc2QKbXVEZVJwNmxIRmlDWHhZdXRQWTRPd2NFbndBWDcyRHRlUzJ5bnBjSUNYTTVTbkEvWlJEeDZOZnlWMWNsb1Zlagprd3grc09mT3pBeXNnN21TS1ZxMkVEZThKU1pFOUpEVCswbm5zaHd5bm5hdXZpbk8zZEF4Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
叢集引數和使用者引數可以同時設定多對,在上下文引數中將叢集引數和使用者引數關聯起來。上面的上下文名稱為kubenetes,叢集為kubenetes,使用者為lulei,表示使用lulei的使用者憑證來訪問kubenetes叢集的default名稱空間,也可以增加--namspace來指定訪問的名稱空間。
最後使用kubectl config use-context kubernetes來使用名為kubenetes的環境項來作為配置。如果配置了多個環境項,可以通過切換不同的環境項名字來訪問到不同的叢集環境。
# 設定當前使用配置
kubectl config use-context kubernetes --kubeconfig=lulei.kubeconfig
[[email protected] rbac]# kubectl config use-context kubernetes --kubeconfig=lulei.kubeconfig
Switched to context "kubernetes".
contexts:
- context:
cluster: kubernetes
user: lulei
name: kubernetes
current-context: kubernetes
kind: Config
preferences: {}
可以看到上面配置是沒有問題的,給出的提示是使用者lulei是不能列出這方面資源的
[[email protected] rbac]# kubectl --kubeconfig=lulei.kubeconfig get pod
Error from server (Forbidden): pods is forbidden: User "lulei" cannot list resource "pods" in API group "" in the namespace "default"
可以看到通過管理員的配置檔案是可以訪問到的
[[email protected] rbac]# kubectl --kubeconfig=/root/.kube/config get pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-drb2s 1/1 Running 3 64d
上面說明了配置檔案是沒有問題的,只是缺少相應的許可權
建立RBAC許可權策略
需要建立role和rolebinding,role是許可權的集合,rolebinding是要將role裡面許可權繫結到指定的使用者,也就是我們上面的lulei使用者
比較常見的授權維度:
• user:使用者名稱
• group:使用者分組
• 資源,例如pod、deployment
• 資源操作方法:get,list,create,update,patch,watch,delete
• 名稱空間
• API組
[[email protected] rbac]# cat rbac.yaml
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: default
name: pod-reader
rules: #配置授權維度
- apiGroups: [""] #裡面為空,為核心的api組,常用的資源都在核心組裡面
resources: ["pods"]
verbs: ["get", "watch", "list"] #資源操作方法
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: read-pods
namespace: default
subjects:
- kind: User
name: lulei
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
[[email protected] rbac]# kubectl apply -f rbac.yaml
role.rbac.authorization.k8s.io/pod-reader created
rolebinding.rbac.authorization.k8s.io/read-pods created
name: lulei和CN裡面名字對應,上面繫結有兩塊,主題即繫結誰,另外和哪個role繫結
[[email protected] rbac]# kubectl --kubeconfig=lulei.kubeconfig get pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-drb2s 1/1 Running 3 64d
[[email protected] rbac]# kubectl --kubeconfig=lulei.kubeconfig get deployment
Error from server (Forbidden): deployments.apps is forbidden: User "lulei" cannot list resource "deployments" in API group "apps" in the namespace "default"
現在準確授權了,只能檢視Pod,其他資源不能檢視
刪除也不行
[[email protected] rbac]# kubectl --kubeconfig=lulei.kubeconfig delete pod nginx-6799fc88d8-drb2s
Error from server (Forbidden): pods "nginx-6799fc88d8-drb2s" is forbidden: User "lulei" cannot delete resource "pods" in API group "" in the namespace "default"
現在要放開許可權
授權的時候還要考慮資源在不在這個組裡面,apps裡面包含了deployment
[[email protected] rbac]# cat rbac.yml
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: ["","apps"]
resources: ["pods","deployments","services"]
verbs: ["get", "watch", "list"]
[[email protected] rbac]# kubectl apply -f rbac.yml
role.rbac.authorization.k8s.io/pod-reader configured
rolebinding.rbac.authorization.k8s.io/read-pods unchanged
[[email protected] rbac]# kubectl --kubeconfig=lulei.kubeconfig get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 64d
[[email protected] rbac]# kubectl --kubeconfig=lulei.kubeconfig get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 64d
nginx NodePort 10.99.50.2 <none> 80:31332/TCP 64d
[[email protected] rbac]# kubectl api-versions 檢視哪些資源組
admissionregistration.k8s.io/v1
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
拿著上面lulei.kubeconfig就可以使用kubectl命令連線叢集了
總結
當使用kucetl呼叫其api或者使用ui其實都是向api server元件傳送的請求。
提取出證書裡面的CN欄位,該欄位作為你的使用者名稱。也就是從證書當中提取出使用者名稱 "CN": "lulei",
也可以基於組 "O": "k8s",
- kind: User
name: lulei
去驗證你的身份是不是有效的,也就是你的證書是不是CA頒發的,驗證客戶端證書沒問題進行下一個階段的授權,也就是建立RBAC,授權之後可以訪問。
使用kubeconfig還需要注意使用者已經經過授權(如RBAC授權),上述例子中使用者的證書中CN欄位為"CN": "lulei"
,kube-apiserver
預定義的 RoleBindingread-pods
將User lulei與 Rolepod-reader
繫結,該 Role 授予了呼叫kube-apiserver
相關 API 的許可權。