圈子太小,做人留一面,日后好相見。
其實這個文章就是用戶用jumpserver登錄到k8s master節點
然后執行kubectl的時候只有自己namespaces的所有權限。
背景
1,k8s 有一天突然kubectl 執行任何命令都報權限不對。
2,最后查明是因為有一個開發不小心把admin的ClusterRoleBinding給刪除了。
3,jumpserver給所有開發的權限都是同一個普通用戶。
4,因為我們是一個k8s集群,然后用不同的namespaces區分不同的業務小組。每個小組有固定的node機器。
5,kubectl 的配置文件都是用的同一個,權限也是admin的。
要求
1,領導要求業務小組只有自己namespaces的權限。
2,業務小組不能有一個集群的大的權限,比如刪除node,不能更新master node的一些信息。
總體思路
1,在jumpserver上面新建各業務方系統用戶。
2,把系統用戶綁定給各業務方用戶組。(關於Jumpserver 系統用戶和管理用戶介紹,后期出一篇單獨的文章。)
3,在k8s master1節點上面的各個業務方系統用戶家目錄下面 新建不同的.kube/config文件。
一、.kube/config 文件生成
1,這里我就用我這邊的需求寫了,有關於k8s rbac的分享,我在后期會寫一篇單獨的文章。https://www.cnblogs.com/fanfanfanlichun/p/15005427.html
2,業務方有:axer,paas。這是我公司這邊的兩個業務方。
3,其實k8s的rbac確實非常的復雜,難理解,實現權限划分的方式
1.1 權限划分思路
1,首先是各種資源的權限,k8s資源又分兩種,一種是集群的資源,一種是ns資源。
2,然后是將權限和誰綁定起來。 大概就是兩步:權限和綁定權限。
3,我這里是用了兩個ClusterRole,一個是集群的資源的權限,一個是ns資源的權限
4,我選擇的是sa來做授權的對象。然后用這個sa的token去做kubectl的權限驗證
5,多個RoleBinding,每一個RoleBinding把ns的資源權限固定在一個ns里面。你想要這個sa有哪些ns的權限,就寫幾個RoleBinding。
6,一個ClusterRoleBinding,用於綁定sa對集群資源的權限。
1.2 創建ClusterRole
1.2.1 集群資源權限
也就是說整個k8s集群的一些權限。比如:get ns,get node,get node status,git ns status......
# 其實這里就是設置對k8s集群的一些權限的
apiVersion: rbac.authorization.k8s.io/v1 # api
kind: ClusterRole # 資源類型
metadata: # 元數據 ClusterRole 不受ns的限制,所以不用寫ns
name: business-axer-get-auth # ClusterRole 的名稱,能區分就行
rules:
- apiGroups: # apiGroups 就是api資源組,你kubectl get apiservice 就可以看到集群所有的api組
- "" # 我這里代表為空,就是api組里面有一個v1. 這樣的
resources: # 就是k8s資源的名稱。kubectl api-resources 這個命令可以查看到,第一列是資源名稱,就是可以寫在這里的。
# 第二列是簡寫,kubectl get 后面的可以簡寫。
# 第三列是APIGROUP組
# 第四列是是否屬於NAMESPACED資源,就是你可以在ns下面看到的資源
# 第五列是kind的時候寫的名稱
# 資源還分子資源,后期會寫一篇專門的文章介紹
- namespaces/status # 這個是ns狀態
- namespaces # 這個是ns
- persistentvolumes # pv
verbs: # verbs是定義動作的
- get # 就是可以查看ns的權限
- list
- watch
- apiGroups:
- ""
resources: # 這里定義的是可以查看node的權限,更新node的權限。
- nodes
- nodes/status
verbs:
- get
- list
- watch
- patch
- update
- apiGroups:
- "storage.k8s.io"
resources: # 這里定義的是可以查看sc的權限,因為我們有后端的存儲集群,他們可以對sc的所有權限
- storageclasses
- storageclasses/status
resourceNames: # 因為sc屬於集群資源,不同的業務方需要對自己的sc才有 全部權限。
- axersc # 所有這里可以指定對哪一個sc有全部權限
verbs:
- create
- delete
- deletecollection
- get
- list
- patch
- update
- watch
1.2.2 ns資源權限
就是定義業務方對自己ns的所有權限,自己的ns里面的所有資源都要有權限。
點擊查看代碼
點擊查看代碼
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: business-axer-admin-auth
rules:
- apiGroups:
- ""
resources: # 對pod的一些權限。
- pods/attach
- pods/exec # exec pod
- pods/portforward # 設置pod的轉發
- pods/proxy
- secrets # secrets的權限
- services/proxy
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- serviceaccounts # sa的權限
verbs:
- impersonate
- apiGroups:
- ""
resources:
- pods
- pods/attach
- pods/exec
- pods/portforward
- pods/proxy
verbs:
- create
- delete
- deletecollection
- patch
- update
- apiGroups:
- ""
resources:
- configmaps
- endpoints
- persistentvolumeclaims
- replicationcontrollers
- replicationcontrollers/scale
- secrets
- serviceaccounts
- services
- services/proxy
verbs:
- create
- delete
- deletecollection
- patch
- update
- apiGroups:
- apps
resources:
- daemonsets
- deployments
- deployments/rollback
- deployments/scale
- replicasets
- replicasets/scale
- statefulsets
- statefulsets/scale
verbs:
- create
- delete
- deletecollection
- patch
- update
- apiGroups:
- autoscaling
resources:
- horizontalpodautoscalers
verbs:
- create
- delete
- deletecollection
- patch
- update
- apiGroups:
- batch
resources:
- cronjobs
- jobs
verbs:
- create
- delete
- deletecollection
- patch
- update
- apiGroups:
- extensions
resources:
- daemonsets
- deployments
- deployments/rollback
- deployments/scale
- ingresses
- networkpolicies
- replicasets
- replicasets/scale
- replicationcontrollers/scale
verbs:
- create
- delete
- deletecollection
- patch
- update
- apiGroups:
- policy
resources:
- poddisruptionbudgets
verbs:
- create
- delete
- deletecollection
- patch
- update
- apiGroups:
- networking.k8s.io
resources:
- ingresses
- networkpolicies
verbs:
- create
- delete
- deletecollection
- patch
- update
- apiGroups:
- metrics.k8s.io
resources:
- pods
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- configmaps
- endpoints
- persistentvolumeclaims
- pods
- replicationcontrollers
- replicationcontrollers/scale
- serviceaccounts
- services
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- bindings
- events
- limitranges
- pods/log
- pods/status
- replicationcontrollers/status
- resourcequotas
- resourcequotas/status
verbs:
- get
- list
- watch
- apiGroups:
- apps
resources:
- controllerrevisions
- daemonsets
- deployments
- deployments/scale
- replicasets
- replicasets/scale
- statefulsets
- statefulsets/scale
verbs:
- get
- list
- watch
- apiGroups:
- autoscaling
resources:
- horizontalpodautoscalers
verbs:
- get
- list
- watch
- apiGroups:
- batch
resources:
- cronjobs
- jobs
verbs:
- get
- list
- watch
- apiGroups:
- extensions
resources:
- daemonsets
- deployments
- deployments/scale
- ingresses
- networkpolicies
- replicasets
- replicasets/scale
- replicationcontrollers/scale
verbs:
- get
- list
- watch
- apiGroups:
- policy
resources:
- poddisruptionbudgets
verbs:
- get
- list
- watch
- apiGroups:
- networking.k8s.io
resources:
- ingresses
- networkpolicies
verbs:
- get
- list
- watch
- apiGroups:
- authorization.k8s.io
resources:
- localsubjectaccessreviews
verbs:
- create
- apiGroups:
- rbac.authorization.k8s.io
resources:
- rolebindings
- roles
verbs:
- create
- delete
- deletecollection
- get
- list
- patch
- update
- watch
這樣兩個權限的ClusterRole就創建好了。其實就是定義權限。有哪些權限。
1.3 創建sa ServiceAccount
我選擇的是sa做角色,當然也可以選擇user,或者usergroup。就是K8s實現權限划分有很多方式。看大家的選擇需求了。
apiVersion: v1
kind: ServiceAccount
metadata:
name: business-axer-serviceaccount # sa的名稱。隨便取,當然你要能認識區分
namespace: business-auth # sa的ns。我這里單獨的建了一個ns,就是為了做權限划分的ns。
1.4 創建ClusterRoleBinding
ClusterRoleBinding是用來綁定集群權限的。
也就是說把我上面定義的集群權限ClusterRole綁定給sa。這樣這個sa就有操作集群的一些字眼的權限了。
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding # ClusterRoleBinding 用於綁定集群權限的
metadata:
name: business-axer:get # 名稱 ClusterRoleBinding 不受ns的限制,所以沒有ns
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole # 上面定義的ClusterRole名稱
name: business-axer-get-auth
subjects:
- kind: ServiceAccount # 上面定義的sa名稱
name: business-axer-serviceaccount
namespace: business-auth
1.5 創建RoleBinding
這個RoleBinding就是綁定ns里面的資源的權限的。
也就是說我把上面定義的ClusterRole權限綁定給sa。然后這個權限只能在這個RoleBinding所在的ns里面才有權限。
想要授權給業務放哪個ns的權限 就要在哪個ns里面創建一個RoleBinding。
假如說一個業務放有10個ns的權限,那么久需要創建10個RoleBinding。
# 我這下面是給這個sa了兩個ns的所有權限
# ai-platform-deploy和ai-platform-test命名空間的所有權限
# 如果需要別的ns的權限那就定義多個RoleBinding。
# 當然這個RoleBinding必須要在ns里面。
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: business-axer:ai-platform-deploy
namespace: ai-platform-deploy
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: business-axer-admin-auth
subjects:
- kind: ServiceAccount
name: business-axer-serviceaccount
namespace: business-auth
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: business-axer:ai-platform-test
namespace: ai-platform-test
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: business-axer-admin-auth
subjects:
- kind: ServiceAccount
name: business-axer-serviceaccount
namespace: business-auth
權限和綁定都做完了,剩下的就是生成config文件了
1.6 生成config文件
我這里選擇的是token的驗證方式。
我是寫了一個簡單的腳本。
#!/bin/bash
sa_name="business-axer-serviceaccount" # 這個是sa的名稱
user="business-axer" # 這個是user,對於我使用token的驗證方式,這里的user沒啥用,隨便寫
context_name="business-axer-ct" # 對於我使用token的驗證方式,這個也是可以隨便寫,
cp ./demo.config ./guest.config # 拷貝一份demo.config
# 這里是獲取sa的secret名稱,每一個sa都會有。
secret_name=`kubectl get sa -n business-auth $sa_name -o go-template='{{range .secrets}}{{.name}}{{end}}'`
# 這里是獲取secret的token
TOKEN=`kubectl -n business-auth get secret $secret_name -o go-template='{{.data.token}}'`
# 這下面是設置一些上下文
kubectl config set-credentials $user --token=`echo ${TOKEN} | base64 -d` --kubeconfig=./guest.config
kubectl config set-context $context_name --cluster=kubernetes --user=$user --kubeconfig=./guest.config
kubectl config use-context $context_name --kubeconfig=./guest.config
demo.config文件
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUCg== 這個是固定的,你查看一下線上的,其實就是https安全驗證,ca證書
server: https://10.19.250.206:8443 # k8s集群apiservice的端口
name: kubernetes # 集群的名稱
kind: Config
最終的guest.config文件,我的下面的除了token和ca我刪了,其他的都是正確的。
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LShRTl0tLS0tCg==
server: https://10.19.250.206:8443
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: business-axer
name: business-axer-ct
current-context: business-axer-ct
kind: Config
preferences: {}
users:
- name: business-axer
user:
token: eyJhbGciOiJSUzI1NiIsImtpZC-iyX0M6m5nEXqC6w
解讀如下:
clusters記錄了 clusters(一個或多個 K8S 集群)信息:
name是這個 cluster(K8S 集群)的名稱代號
server是這個 cluster(K8S 集群)的訪問方式,一般為 IP+PORT
certificate-authority-data是證書數據,只有當 cluster(K8S 集群)的連接方式是 https 時,為了安全起見需要證書數據
users記錄了訪問 cluster(K8S 集群)的賬號信息:
name是用戶賬號的名稱代號
user/token是用戶的 token 認證方式,token 不是用戶認證的唯一方式,其他還有賬號+密碼等。
contexts是上下文信息,包括了 cluster(K8S 集群)和訪問 cluster(K8S 集群)的用戶賬號等信息:
name是這個上下文的名稱代號
cluster是 cluster(K8S 集群)的名稱代號
user是訪問 cluster(K8S 集群)的用戶賬號代號
current-context記錄當前 kubectl 默認使用的上下文信息
kind和apiVersion都是固定值,用戶不需要關心
preferences則是配置文件的其他設置信息,我沒有使用過,暫時不提。
1.7 config文件放入業務方用戶的家目錄下面的.kube/config。權限要644的。
多個業務放就按照上面的步驟創建就行,可以把業務放理解成sa。
二、配置jumpserver
其實就是在jumpserver上面創建系統用戶,然后把這個系統用戶綁定到用戶組。
那么用戶組里面的用戶登錄k8s master1機器的時候就是他們自己的系統用戶了。
然后這個用戶執行kubectl 的時候 默認會去家目錄下面的.kube找config文件。
config文件又是我們生成的。
這樣就可以把用戶限制在ns里面了。
歡迎大佬留言評論,有什么不足的望各位大佬指出。