misterli's Blog.

使用gitlab账号登陆Kubesphere

字数统计: 2.1k阅读时长: 9 min
2021/08/02

介绍

我们之前已经介绍了如何最小化安装kubesphere 以及 接入自定义的prometheus监控,这里看一下kubeshere中的多租户,多租户是我们实际生产使用中很需要的一个功能,这样能满足我们不同用户去登陆kubesphere平台,比如我们开发,运维,测试同学都需要登陆kubesphere平台,并且我们需要为不同身份的同学配置不同的权限,当公司内需要访问kubesphere的同学比较多时,管理员再去手动的为用户创建账号就比较呆板了,KubeSphere 包含一个内置的 OAuth 服务和帐户系统。用户通过获取 OAuth 访问令牌以对 API 进行身份验证,我们可以通过接入ldap或者oidc来提供身份认证信息。

多租户方案

image-20210802131844011

认证鉴权链路

image-20210802133353633

使用

集群内已经最小化安装kubesphere,安装可以参考kubesphere 3.1.1 接入自定义 Prometheus

我们这里使用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和密码,后面会用得到

image-20210802001203688

image-20210802001215836

1
2
3
4
5
6
7
8
9
10
11
12
13
#添加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文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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: ca14d16e376b6f6634416d27fa760605932feb57378d1267e946e9d3e758e2f0 #修改为你gitlab 应用的clientid
clientSecret: 15dcb3501becd1773f40d9bb1b82b05874e2ef893b7a0888fdaaa37885fd9387 #修改为你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 为项目组,根据自己的gitlab去实际填写,此处填写项目组的成员才会允许通过dex 进行身份认证
  • 如果集群内没有安装cert-manager,需要手动创建证书的secert

执行如下命令进行安装

1
2
3
4
5
6
7
8
9
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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: 'generatedLongRandomPhrase'
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

我们也可以通过以下命令修改认证配置:

1
kubectl -n kubesphere-system edit cc ks-installer

配置示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:
...

当您修改上述配置后,需要等待配置生效,可以通过以下命令查看相关进度及日志:

1
kubectl -n kubesphere-system logs -l app=ks-installer -f

重新应用后我们打开登录页面会发现登录页面多了通过dex登陆的按钮

image-20210802172827663

点击后会自动跳转到gitlab进行认证,第一次登陆需要我们授权应用可以访问,这里要注意允许访问的kubesphered的gitlab用户为我们dex服务中定义的gitlab项目组中的用户,实际中我们可以设置特定特定的项目组,将用户添加到项目组中来实现用户登陆的限制

image-20210802173147353

点击authorize允许后,kubespher会让我们确认我们的账户信息,此时需要合理修改用户名

image-20210802173707924

image-20210802173828610

设定完成后,我们就可以进入kubesphere中了,此时该用户是没有任何访问权限的,还需要管理员对该用户进行授权才能正常进行使用,希望后期kubespher 可以支持在用户登陆前可以对用户组进行授权,或者预先通过邮箱对用户进行收钱,避免用户登陆后,管理员还需要手动去创建权限。

image-20210802231454038

CATALOG
  1. 1. 介绍
    1. 1.1. 多租户方案
    2. 1.2. 认证鉴权链路
  2. 2. 使用
    1. 2.1. 安装dex
    2. 2.2. 配置kubesphere