背景:
是這樣的一個事情:服務運行于kubernetes集群(騰訊云tke1.20.6),日志采集到了elasticsearch集群and騰訊的cls日志服務中,小伙伴看日志覺得還是不太方便,還是想看控制臺輸出的,給他們分配過一臺服務器(加入到集群中,但是有污點標簽的節點),為了方便他們測驗一下東西,現在想讓他們通過此work節點可以在控制臺查看日志,正常的就是把master節點的/root/.kube/目錄下的config組態檔copy過來就可以了,但是這權限也太大了!重新復習一遍kubeconfig組態檔以及 role rolebinding的知識!
注: namespace為official,想分配的權限是list and log,嗯查看pod串列和查看日志 不能洗掉修改namespace下pod,并且不能查看其他namespace,
Kubernetes之kuberconfig
1. 創建用戶憑證
前提: openssl的安裝就忽略了…
1. 創建用戶證書私鑰
用戶就用我自己名字了,私鑰命名為zhangpeng.key
openssl genrsa -out zhangpeng.key 2048
2. 創建證書簽名請求檔案
使用我們剛剛創建的私鑰創建一個證書簽名請求檔案:zhangpeng.csr,要注意需要確保在-subj引數中指定用戶名和組(CN表示用戶名,O表示組)
openssl req -new -key zhangpeng.key -out zhangpeng.csr -subj "/CN=zhangpeng/O=layabox"
可能你會出現下面的報錯:

注:圖非上面執行命令的截圖,其他環境下操作出現的
解決方式如下:
cd /root
openssl rand -writerand .rnd

然后重新執行命名
openssl req -new -key zhangpeng.key -out zhangpeng.csr -subj "/CN=zhangpeng/O=layabox"
3. 生成最終證書檔案
找到Kubernetes集群的CA,如果你是使用的是kubeadm安裝的集群,CA相關證書位于/etc/kubernetes/pki/目錄下面,如果你是二進制方式搭建的,你應該在最開始搭建集群的時候就已經指定好了CA的目錄,我們會利用該目錄下面的ca.crt和ca.key兩個檔案來批準上面的證書請求,當然了我使用的是騰訊云的tke集群,證書是位于/etc/kubernetes目錄下的 server.crt和server.key,這里就用這兩個檔案生成證書檔案,命令如下:
?
root@ap-shanghai-k8s-master-1:~/ap-shanghai# openssl x509 -req -in zhangpeng.csr -CA /etc/kubernetes/ca.crt -CAkey /etc/kubernetes/ca.key -CAcreateserial -out zhangpeng.crt -days 3650
Signature ok
subject=CN = zhangpeng, O = layabox
Getting CA Private Key
查看我們當前檔案夾下面是否生成了一個證書檔案

4. 在kubernetes集群中創建憑證和背景關系(context)
創建新的用戶憑證
root@ap-shanghai-k8s-master-1:~/ap-shanghai# kubectl config set-credentials zhangpeng --client-certificate=zhangpeng.crt --client-key=zhangpeng.key
User "zhangpeng" set.
為用戶設定Context:
root@ap-shanghai-k8s-master-1:~/ap-shanghai# kubectl config set-context zhangpeng-context --cluster=kubernetes --namespace=official --user=zhangpeng
Context "zhangpeng-context" created.

到這里,zhangpeng用戶的配置就已經創建成功了,現在我們使用當前的這個組態檔來操作kubectl命令的時候,應該會出現錯誤,因為我們還沒有為該用戶定義任何操作的權限:
$ kubectl get pods --context=zhangpeng-context -n official
Error from server (Forbidden): pods is forbidden: User "zhangpeng" cannot list resource "pods" in API group "" in the namespace "official"
2. 創建角色
cat role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: official
name: official-log-role
rules:
- apiGroups: [""]
resources: ["pods", "pods/log"]
verbs: ["get", "list"]
kubectl apply -f role.yaml
注:可參照https://kubernetes.io/zh/docs/reference/access-authn-authz/rbac/ rbac鑒權
3. 創建角色權限系結
cat rolebinding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: ap-shanghai-rolebinding
namespace: official
subjects:
- kind: User
name: zhangpeng
apiGroup: ""
roleRef:
kind: Role
name: official-log-role
apiGroup: ""
kubectl apply -f rolebinding.yaml
4. 測驗
root@ap-shanghai-k8s-master-1:~/ap-shanghai# kubectl get pods --context=zhangpeng-context
The connection to the server localhost:8080 was refused - did you specify the right host or port?
報錯了為什么呢?喵一眼/root/.kube/config檔案:

tke集群默認的cluster是local.我在1.2.4步驟中cluster設定的是kubernetes,這里直接在config檔案中修改了cluster為local,也強調一下在執行1.2.4步驟的時候一定要先確認一下集群的cluster名稱,不要直接copy拿來主義!
?
重新進行測驗:
kubectl get pods --context=zhangpeng-context

由于這些pod都是線上跑的,我就新建一個nginx pod然后進行測驗下是否可以delete and edit
kubectl run nginx --image=nginx -n official
$ kubectl delete pods nginx --context=zhangpeng-context
Error from server (Forbidden): pods "nginx" is forbidden: User "zhangpeng" cannot delete resource "pods" in API group "" in the namespace "official"
$ kubectl edit nginx --context=zhangpeng-context
error: pods "nginx" could not be patched: pods "nginx" is forbidden: User "zhangpeng" cannot patch resource "pods" in API group "" in the namespace "official"
You can run `kubectl replace -f /tmp/kubectl-edit-kp0az.yaml` to try this update again.
嗯 然后將config檔案copy到用戶的跳板機上面/root/.kube/config:

我是那么做的洗掉了原來的集群默認用戶的user and contexts,講contexts默認用戶設定為創建的zhangpeng-context,當然了也記得把client-certificate client-key 檔案copy到對應目錄(當然了自己也可以自定義下,然后修改一下config檔案)

切換一下命名空間namespace試一試:

基本實作了個人的目的,RBAC and安全背景關系還要深入復習一下!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/308719.html
標籤:其他
上一篇:Nginx 詳解
