Kubeflow 在新的 Namespace 中修改默认的用户名和密码
在 Kubeflow 中,用户管理主要依赖其认证系统,通常通过 Dex(一个支持多种身份提供者的开源身份服务)与 Kubernetes 的 RBAC(基于角色的访问控制)结合实现。Kubeflow 支持多用户隔离,每个用户通常关联一个 Kubernetes Namespace(通过 Profile 资源实现)。以下是如何在 Kubeflow 中管理用户(包括修改现有用户的 email 或密码、新增用户等)的详细说明:
1. Kubeflow 用户管理概述
- 认证方式:Kubeflow 使用 Dex 提供认证,支持多种身份提供者(如 LDAP、OIDC、GitHub、Google 等)。用户登录后,Kubeflow 会自动为其创建对应的 Profile 和 Namespace(如果配置了自动创建)。
- Profile 资源:Kubeflow 中的 Profile 是 Kubernetes 的自定义资源(CRD),用于定义用户及其关联的 Namespace。每个 Profile 通常对应一个用户或用户组,并绑定到 Kubernetes RBAC 策略。
- 权限管理:用户权限通过 Kubernetes RBAC 管理,管理员可以为用户的 Namespace 设置只读或可写权限。
用户数据存储:用户凭据(如 email 和密码)通常存储在 Dex 的配置文件中(静态用户)或外部身份提供者(如 LDAP)中。
2. 修改现有用户的 Email 或密码
在 Kubeflow 中,修改用户的 email 或密码需要根据使用的认证方式进行操作。以下以 Dex 的静态用户配置为例(如果使用 LDAP 或 OIDC,请参考对应身份提供者的文档):
步骤 1:访问 Dex 配置
Dex 的用户数据存储在 Kubernetes ConfigMap 中,位于 auth Namespace 的 dex ConfigMap 内。
获取 Dex 配置:
kubectl get configmap dex -n auth -o jsonpath='{.data.config\.yaml}' > dex-config.yaml
编辑 dex-config.yaml 文件,找到 staticPasswords 部分。示例:
staticPasswords:
- email: "user1@example.com"
hash: "$2y$12$..."
username: "user1"
- 修改 Email:直接更改 email 字段,例如将 user1@example.com 改为 newuser1@example.com。
- 修改密码:
使用 Python 的 passlib 库生成新的 bcrypt 密码哈希:
python3 -c 'from passlib.hash import bcrypt; import getpass; print(bcrypt.using(rounds=12, ident="2y").hash(getpass.getpass()))'
输入新密码后,获取生成的哈希值。
将 hash 字段替换为新生成的哈希值。
更新 Dex ConfigMap:
kubectl create configmap dex -n auth --from-file=config.yaml=dex-config.yaml --dry-run=client -o yaml | kubectl apply -f -
重启 Dex Pod 使配置生效:
kubectl delete pod -n auth -l app=dex
注意事项
- 如果用户通过 LDAP 或 OIDC 认证,需在相应的外部身份提供者系统中修改 email 或密码,而不是直接修改 Dex 配置。
- 修改 email 后,可能需要更新对应的 Profile 资源(见下文)。
更新 Profile 的 Email
如果修改了用户的 email,需要同步更新 Kubeflow Profile 资源:
编辑 Profile:
kubectl edit profile <profile-name> -n kubeflow
更新 spec.owner.name 字段为新的 email,例如:
apiVersion: kubeflow.org/v1beta1
kind: Profile
metadata:
name: user1-profile
spec:
owner:
kind: User
name: newuser1@example.com
应用更改后,Kubeflow 会更新用户与 Namespace 的关联。
3. 新增用户
在 Kubeflow 中新增用户需要创建用户凭据并为用户分配 Profile 和 Namespace。
步骤 1:添加用户到 Dex
编辑 Dex ConfigMap(如上所述):
kubectl get configmap dex -n auth -o jsonpath='{.data.config\.yaml}' > dex-config.yaml
在 staticPasswords 部分添加新用户:
staticPasswords:
- email: "newuser@example.com"
hash: "$2y$12$..." # 使用 passlib 生成的哈希
username: "newuser"
生成密码哈希的命令:
python3 -c 'from passlib.hash import bcrypt; import getpass; print(bcrypt.using(rounds=12, ident="2y").hash(getpass.getpass()))'
更新 ConfigMap 并重启 Dex:
kubectl create configmap dex -n auth --from-file=config.yaml=dex-config.yaml --dry-run=client -o yaml | kubectl apply -f -
kubectl delete pod -n auth -l app=dex
步骤 2:创建 Profile
为新用户创建 Profile 以分配 Namespace 和权限:
创建 Profile YAML 文件(例如 newuser-profile.yaml):
apiVersion: kubeflow.org/v1beta1
kind: Profile
metadata:
name: newuser-profile
spec:
owner:
kind: User
name: newuser@example.com
应用 Profile:
kubectl apply -f newuser-profile.yaml
验证 Namespace 是否创建:
kubectl get namespace newuser-profile
自动创建 Profile
如果 Kubeflow 配置了自动创建 Profile(从 v0.6.2 开始支持),用户首次登录时会自动生成 Profile 和 Namespace,无需手动创建。确保 Dex 配置正确即可。
4. 删除用户
删除用户需要从 Dex 和 Kubeflow 中移除用户数据,并清理相关资源。
步骤 1:从 Dex 中删除用户
编辑 Dex ConfigMap,移除 staticPasswords 中对应的用户条目。
更新 ConfigMap 并重启 Dex:
kubectl create configmap dex -n auth --from-file=config.yaml=dex-config.yaml --dry-run=client -o yaml | kubectl apply -f -
kubectl delete pod -n auth -l app=dex
步骤 2:删除 Profile 和 Namespace
删除 Profile:
kubectl delete profile <profile-name> -n kubeflow
删除对应的 Namespace:
kubectl delete namespace <profile-name>
注意事项
- 如果 Namespace 被自动重建,可能是 Dex 或 LDAP 中仍有用户数据。确保从身份提供者中完全移除用户。
- 删除 Namespace 前,检查是否有重要资源(如 Jupyter Notebooks 或 Pipelines)需要备份。
5. 其他用户管理操作
权限管理:
使用 Kubernetes RBAC 控制用户权限。例如,为用户授予特定 Namespace 的读写权限:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: user-read-write
namespace: newuser-profile
subjects:
- kind: User
name: newuser@example.com
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: edit
apiGroup: rbac.authorization.k8s.io
应用 RoleBinding:
kubectl apply -f rolebinding.yaml
共享 Profile:
用户可以通过 Profile 共享 Namespace 的访问权限(只读或可写)。编辑 Profile 的 spec 添加共享用户:
spec:
owner:
kind: User
name: newuser@example.com
resourceQuota:
hard:
cpu: "4"
memory: 16Gi
使用外部身份提供者:
如果使用 LDAP 或 OIDC,需在 Dex 配置中添加连接器(connectors 字段),并在外部系统中管理用户。例如,LDAP 配置:
connectors:
- type: ldap
name: OpenLDAP
id: ldap
config:
host: ldap.example.com:636
bindDN: cn=admin,dc=example,dc=com
bindPW: adminpassword
6. 注意事项与最佳实践
- 安全性:避免使用简单的静态密码,优先集成 LDAP 或 OIDC 以提高安全性。
- 备份:在修改 Dex 配置或删除用户前,备份 ConfigMap 和相关资源。
- 日志监控:检查 Dex 和 Kubeflow 的日志以排查认证问题:
kubectl logs -n auth -l app=dex
- 版本兼容性:不同 Kubeflow 版本的 Dex 配置可能略有差异,参考官方文档。
- 自动化:对于大规模用户管理,考虑使用脚本或 CI/CD 流水线来管理 Dex 配置和 Profile 资源。
总结
修改现有用户名和密码
- 步骤1:生成新的密码,通过 UI 或 yaml 文件进行替换新的密码
python3 -c 'from passlib.hash import bcrypt; import getpass; print(bcrypt.using(rounds=12, ident="2y").hash(getpass.getpass()))'
类似如下:
$2y$12$eRxphHBSoP/5R6L8ArPW7uk8UZjwIsY9hMLQaUoVaZUxNzm1IKkPO
- 步骤2:修改 data->config.yaml->staticPasswords->email 为新的 email
kubectl edit configmap dex -n auth
- 步骤3:修改 spec->owner->name 为新的 email
kubectl edit profile <profile> -n kubeflow
- 步骤4:所在的 namespace 中修改 subjects->name 为新的 email
kubectl edit rolebinding namespaceAdmin -n <namespace>