3. OpenLDAP 安装与配置:基于 Docker 的快速部署与配置方式
OpenLDAP 是 LDAP 协议的主流开源实现,在认证授权平台中承担用户与权限数据的统一存储角色。本章聚焦两种部署与配置路径:基于 Docker 的快速部署(适用于开发/测试环境)与传统 slapd.conf 与动态 cn=config 配置方式(适用于生产环境)。
3.1 基于 Docker 的 OpenLDAP 快速部署
使用 Docker 部署 OpenLDAP 可大幅降低环境搭建门槛,尤其适合开发测试与 CI/CD 流水线。推荐使用 osixia/openldap 官方镜像,该镜像已内置 cn=config 支持。
3.1.1 部署步骤
- 拉取镜像:
docker pull osixia/openldap:1.5.0 - 启动容器(基础参数):
docker run -d \
--name openldap-server \
-p 389:389 \
-p 636:636 \
-e LDAP_ORGANISATION="Example Corp" \
-e LDAP_DOMAIN="example.com" \
-e LDAP_ADMIN_PASSWORD="admin123" \
-e LDAP_CONFIG_PASSWORD="config123" \
--volume openldap_data:/var/lib/ldap \
--volume openldap_config:/etc/ldap/slapd.d \
osixia/openldap:1.5.0
3.1.2 关键参数说明
| 参数 | 说明 | 示例值 |
|---|---|---|
LDAP_ORGANISATION |
组织名称,用于生成默认 DC | Example Corp |
LDAP_DOMAIN |
LDAP 根域名,自动推导 base DN | example.com → dc=example,dc=com |
LDAP_ADMIN_PASSWORD |
管理员 cn=admin,dc=example,dc=com 密码 | admin123 |
LDAP_CONFIG_PASSWORD |
cn=config 管理用户密码 | config123 |
--volume 持久化 |
数据目录与配置目录持久化 | Docker volume 或 bind mount |
💡 生产建议:务必使用
LDAP_CONFIG_PASSWORD 保护 cn=config 配置树,避免未授权修改。同时启用 TLS(端口 636)并挂载证书。
3.1.3 验证部署
# 使用 ldapsearch 查询根 DSE
docker exec openldap-server ldapsearch -x -H ldap://localhost -b "" -s base
# 使用管理员账号查询
docker exec openldap-server ldapsearch -x \
-H ldap://localhost \
-D "cn=admin,dc=example,dc=com" \
-w admin123 \
-b "dc=example,dc=com"
3.2 slapd.conf 传统配置方式
slapd.conf 是 OpenLDAP 2.3 之前的静态配置文件方式,目前仍被部分运维团队沿用。配置修改后需重启 slapd 服务才能生效。
3.2.1 基础配置示例
# /etc/ldap/slapd.conf
include /etc/ldap/schema/core.schema
include /etc/ldap/schema/cosine.schema
include /etc/ldap/schema/inetorgperson.schema
pidfile /var/run/slapd/slapd.pid
argsfile /var/run/slapd/slapd.args
# 数据库定义
database mdb
suffix "dc=example,dc=com"
rootdn "cn=admin,dc=example,dc=com"
rootpw {SSHA}xxxxxxxxxxxxxxxxxxxxx
# 索引配置
index objectClass eq
index uid pres,eq
index cn pres,sub,eq
index sn pres,sub,eq
index mail pres,sub,eq
# 访问控制
access to attrs=userPassword
by self write
by anonymous auth
by dn.base="cn=admin,dc=example,dc=com" write
by * none
access to *
by self write
by dn.base="cn=admin,dc=example,dc=com" write
by * read
3.2.2 密码生成
# 使用 slappasswd 生成 SSHA 密码
slappasswd -s admin123
# 输出:{SSHA}xxxxxxxxxxxxxxxxxxxxx
⚠ 注意:slapd.conf 方式修改配置后必须执行
systemctl restart slapd 或发送 SIGHUP 信号。生产环境变更窗口需提前规划。
3.3 cn=config 动态配置方式
cn=config 是 OpenLDAP 2.3+ 引入的在线配置框架,将配置信息存储在 LDAP DIT(Directory Information Tree)中,支持运行时动态修改,无需重启服务。
3.3.1 配置架构
3.3.2 在线修改配置示例
# 添加索引(无需重启)
cat > add_index.ldif <
3.3.3 两种配置方式对比
| 对比维度 | slapd.conf | cn=config |
|---|---|---|
| 配置存储 | 静态文本文件 | LDAP DIT 目录树 |
| 修改生效 | 需重启 slapd | 在线即时生效 |
| 版本管理 | 适合 Git 管理 | 需 ldapsearch 导出 |
| 容器化适配 | 较差,需挂载文件 | 优秀,支持动态注入 |
| 推荐场景 | 传统物理机/虚拟机 | Docker/K8s/DevOps |
3.4 配置方式选择建议
决策路径:
- 开发/测试环境:使用 Docker + cn=config 组合,快速启动、灵活调整。
- 生产环境(容器化):使用 Docker + cn=config + 持久化卷,配合 ConfigMap 或 Operator 管理。
- 生产环境(传统部署):使用 slapd.conf + Ansible/Puppet 配置管理,变更走审批流程。
3.5 常见问题与排错
| 问题现象 | 可能原因 | 解决方案 |
|---|---|---|
| 容器启动后端口未监听 | 端口冲突或权限不足 | 检查 docker logs,确认宿主端口未被占用 |
| ldapsearch 返回 "No such object" | base DN 与容器参数不匹配 | 核对 LDAP_DOMAIN 与查询的 -b 参数 |
| cn=config 修改被拒绝 | 未使用 EXTERNAL 认证或密码错误 | 使用 -Y EXTERNAL -H ldapi:/// 本地 socket 认证 |
| slapd.conf 语法错误 | 缩进或指令拼写错误 | 使用 slaptest -u -f slapd.conf 检查语法 |
⚠ 安全提醒:无论使用哪种配置方式,务必:
- 禁用匿名绑定(
disallow bind_anon) - 启用 TLS/SSL 加密传输
- 定期备份
/var/lib/ldap数据目录 - 使用强密码策略(ppolicy)