作者:李帥
介紹
KubeSphere 多租戶是實際生產使用中非常需要的一個功能,該功能滿足不同用戶登陸 KubeSphere 平臺的需求,比如開發,運維,測驗都需要登陸 KubeSphere 平臺,并且需要為不同身份的用戶配置不同的權限,當公司內需要訪問 KubeSphere 的用戶比較多時,管理員再去手動為用戶創建賬號就不太靈活了,KubeSphere 包含一個內置的 OAuth 服務和帳戶系統,用戶通過獲取 OAuth 訪問令牌以對 API 進行身份驗證,我們可以通過接入 LDAP 或者 OIDC 來提供身份認證資訊,
多租戶方案

認證鑒權鏈路

使用
假設集群內已經最小化安裝 KubeSphere,我們這里使用 OIDC 身份提供者進行認證,通過 Dex 接入到 GitLab 中,使用 GitLab 中的用戶完成認證,
安裝 Dex
Dex 是一種身份認證服務,它使用 OpenID Connect 來驅動其他應用程式的身份驗證,Dex 通過 “connectors” 充當其他身份提供商的門戶, Dex 可以將身份驗證推到 LDAP 服務器、SAML 提供商或已建立的身份提供商(如 GitHub、Gitlab、Google 和 Active Directory等), 客戶端撰寫身份驗證邏輯以與 Dex 互動認證,然后 Dex 通過 connector 轉發到后端用戶認證方進行認證,并回傳給客戶端 Oauth2 Token,與其相似的身份認證服務還有 Keycloak,auth0 等,
首先需要在 gitlab 上創建應用,在范圍里勾選這幾個 read_user profile email openid,創建后需要記住頁面上的應用程式 id 和密碼,后面會用得到,


#添加dex的helm倉庫
root@i-tsfhx8p6:~/qke-k8s/dex# helm repo add dex https://charts.dexidp.io
"dex" has been added to your repositories
#下載dex的chart 檔案到本地
root@i-tsfhx8p6:~/qke-k8s/dex# helm pull dex/dex
root@i-tsfhx8p6:~/qke-k8s/dex# ls
dex-0.5.0.tgz
root@i-tsfhx8p6:~/qke-k8s/dex# tar xf dex-0.5.0.tgz
root@i-tsfhx8p6:~/qke-k8s/dex# ls
dex dex-0.5.0.tgz
root@i-tsfhx8p6:~/qke-k8s/dex# ls dex
Chart.yaml LICENSE README.md ci templates values.yaml
修改values.yaml檔案
replicaCount: 1
image:
repository: dexidp/dex
pullPolicy: IfNotPresent
tag: "v2.29.0"
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
hostAliases: []
https:
# -- Enable the HTTPS endpoint.
enabled: false
grpc:
# -- Enable the gRPC endpoint.
# Read more in the [documentation](https://dexidp.io/docs/api/).
enabled: false
configSecret:
# -- Enable creating a secret from the values passed to `config`.
# If set to false, name must point to an existing secret.
create: true
# -- The name of the secret to mount as configuration in the pod.
# If not set and create is true, a name is generated using the fullname template.
# Must point to secret that contains at least a `config.yaml` key.
name: ""
config:
issuer: https://dex-qke.lishuai.fun #修改為你實際的地址
storage:
type: kubernetes
config:
inCluster: true
web:
http: 0.0.0.0:5556
telemetry:
http: 0.0.0.0:5558
expiry:
signingKeys: "6h"
idTokens: "24h"
logger:
level: debug
format: json
oauth2:
responseTypes: ["code", "token", "id_token"]
skipApprovalScreen: true
connectors:
- type: gitlab
id: gitlab
name: GitLab
config:
baseURL: https://gitlab.lishuai.fun #修改為你實際的gitlab 地址
clientID: ca14d16e376b6f6634*********57378d1267e946e9d3e758e2f0 #修改為你gitlab 應用的clientid
clientSecret: 15dcb3501becd17******1b82b05874e2ef893b7a0888fdaaa37885fd9387 #修改為你gitlab 應用的clientsecert
redirectURI: https://dex-qke.lishuai.fun/callback #修改為你實際的地址,格式為(dex issuer)/callback
groups:
- k8s-auth
- k8s-auth/dashboard
- k8s-auth/dashboard/show ##gitlab專案組,只允許dashboard專案組成員訪問
staticClients:
- id: dex-k8s-authenticator
name: dex-k8s-authenticator
secret: generatedLongRandomPhrase
redirectURIs:
- 'http://kubesphere.lishuai.fun/oauth/redirect/dex' #這個是kubesphere的回呼地址,對于dex認證,格式為kubesphere_url/oauth/redirect/dex
volumes: []
volumeMounts: []
envFrom: []
env: {}
serviceAccount:
# -- Enable service account creation.
create: true
# -- Annotations to be added to the service account.
annotations: {}
# -- The name of the service account to use.
# If not set and create is true, a name is generated using the fullname template.
name: "dex-sa"
rbac:
create: true
podAnnotations: {}
podDisruptionBudget:
enabled: false
minAvailable:
maxUnavailable:
priorityClassName: ""
podSecurityContext: {}
# fsGroup: 2000
securityContext: {}
# capabilities:
# drop:
# - ALL
# readOnlyRootFilesystem: true
# runAsNonRoot: true
# runAsUser: 1000
service:
annotations: {}
type: ClusterIP
ports:
http:
port: 5556
nodePort:
https:
port: 5554
nodePort:
grpc:
port: 5557
nodePort:
ingress:
enabled: true
className: ""
annotations:
kubernetes.io/ingress.class: nginx
kubernetes.io/tls-acme: "true"
cert-manager.io/cluster-issuer: tls #集群內已經安裝了cert-manager,通過cert-manager來進行證書簽發
hosts:
- host: dex-qke.lishuai.fun
paths:
- path: /
pathType: ImplementationSpecific
tls:
- secretName: dex-tls
hosts:
- dex-qke.lishuai.fun
resources: {}
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi
autoscaling:
enabled: false
minReplicas: 1
maxReplicas: 100
targetCPUUtilizationPercentage: 80
# targetMemoryUtilizationPercentage: 80
nodeSelector: {}
tolerations: []
affinity: {}
這里有幾點需要注意:
- Gitlab 配置下的 groups 為專案組,需要根據實際情況填寫,此處填寫專案組的成員才會允許通過 Dex 進行身份認證
- 如果集群內沒有安裝 cert-manager,需要手動創建證書的 secert
執行如下命令進行安裝,
root@i-tsfhx8p6:~/qke-k8s/dex# ls
dex dex-0.5.0.tgz
#在當前目錄執行
kubectl create ns dex
kubectl -n dex install dex dex
root@i-tsfhx8p6:~/qke-k8s/dex# kubectl -n dex get pod
NAME READY STATUS RESTARTS AGE
dex-d8c5cdfc-577gf 1/1 Running 0 21h
配置 KubeSphere
安裝好 Dex 后需要修改 cluster-configuration.yaml,
apiVersion: installer.kubesphere.io/v1alpha1
kind: ClusterConfiguration
metadata:
name: ks-installer
namespace: kubesphere-system
labels:
version: v3.1.1
spec:
persistence:
storageClass: "longhorn" # If there is no default StorageClass in your cluster, you need to specify an existing StorageClass here.
authentication:
jwtSecret: ""
authenticateRateLimiterMaxTries: 10
authenticateRateLimiterDuration: 10m0s
oauthOptions:
accessTokenMaxAge: 1h
accessTokenInactivityTimeout: 30m
identityProviders:
- name: dex
type: OIDCIdentityProvider
mappingMethod: auto
provider:
clientID: 'dex-k8s-authenticator'
clientSecret: 'gener*******ongRandomPhrase'
issuer: https://dex-qke.lishuai.fun
redirectURL: http://kubesphere.lishuai.fun/oauth/redirect/dex
scopes:
- openid
- email
......
引數釋意:
authenticateRateLimiterMaxTries:authenticateLimiterDuration指定的期間內允許的最大連續登錄失敗次數,如果用戶連續登錄失敗次數達到限制,則該用戶將被封禁,authenticateRateLimiterDuration: 作用于authenticateRateLimiterMaxTries,loginHistoryRetentionPeriod: 用戶登錄記錄保留期限,過期條目將被自動洗掉,maximumClockSkew: 控制執行對時間敏感的操作(例如驗證用戶令牌的過期時間)時允許的最大時鐘偏移,默認值為 10 秒,multipleLogin: 允許多個用戶同時從不同位置登錄,默認值為true,jwtSecret: 簽發用戶令牌的密鑰,最小長度為 32 個字符,多集群環境需要注意的事項,
oauthOptions: OAuth 設定
accessTokenMaxAge: 訪問令牌有效期,對于多集群環境中的成員集群,默認值為0h,這意味著訪問令牌永不過期,對于其他集群,默認值為2h,accessTokenInactivityTimeout: 令牌空閑超時時間,該值表示令牌過期后,重繪用戶令牌最大的間隔時間,如果不在此時間視窗內重繪用戶身份令牌,用戶將需要重新登錄以獲得訪問權,- identityProviders: 身份提供者設定
name: 身份提供者的名稱,type: 身份提供者的型別,mappingMethod: 帳戶映射方式. 值可以是auto或者lookup,- 默認值為
auto, 通過第三方帳戶登錄時會自動創建關聯帳戶, - 如果值為
lookup, 你需要手動關聯第三方帳戶與KubeSphere帳戶, provider: Identity provider 配置,此部分中的欄位根據身份提供的型別而異
provider:
- ? clientID: OAuth2 客戶端 ID
- ? clientSecret: OAuth2 客戶端secert
- ? issuer: dex的地址
- ? redirectURL:重定向到 ks-console 的 URL ,注意這個地址要在dex里也配置
修改完后執行 kubectl apply -f cluster-configuration.yaml ,
也可以通過以下命令修改認證配置:
kubectl -n kubesphere-system edit cc ks-installer
配置示例:
apiVersion: installer.kubesphere.io/v1alpha1
kind: ClusterConfiguration
metadata:
name: ks-installer
spec:
authentication:
jwtSecret: ********************************
authenticateRateLimiterMaxTries: 10
authenticateRateLimiterDuration: 10m
oauthOptions:
accessTokenInactivityTimeout: 30m
accessTokenMaxAge: 1h
identityProviders:
- mappingMethod: auto
name: github
type: GitHubIdentityProvider
provider:
...
當修改上述配置后,需要等待配置生效,可以通過以下命令查看相關進度及日志:
kubectl -n kubesphere-system logs -l app=ks-installer -f
重新應用后打開登錄頁面會發現登錄頁面多了通過 dex 登陸的按鈕,

點擊后會自動跳轉到 Gitlab 進行認證,第一次登陸需要授權應用可以訪問,這里要注意允許訪問的 KubeSphere 的 Gitlab用戶為 Dex 服務中定義的 Gitlab 專案組中的用戶,實際中我們可以設定特定特定的專案組,將用戶添加到專案組中來實作用戶登陸的限制,

點擊 authorize 允許后,KubeSphere 會讓我們確認賬戶資訊,此時需要合理修改用戶名,


設定完成后,就可以進入 KubeSphere 了,此時該用戶是沒有任何訪問權限的,還需要管理員對該用戶進行授權才能正常進行使用,希望后期 KubeSphere 可以支持在用戶登陸前可以對用戶組進行授權,或者預先通過郵箱對用戶進行收錢,避免用戶登陸后,管理員還需要手動去創建權限,

本文由博客一文多發平臺 OpenWrite 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/418111.html
標籤:其他
