---
slideOptions:
transition: slide
theme: night
tags: LDAP-Series, Configure, Tutorial
type:
title: 3. Config OpenLdap Master-Slave
---
<style>
.reveal section img {
background: transparent!important;
border: none!important;
box-shadow: none!important;
}
</style>
# Cấu hình triển khai OpenLdap theo mô hình Master-Slave
## Tổng quan
Khi triển khai LDAP trong môi trường thực tế, chúng ta phải quan tâm tới giải pháp backup, tăng tính sẵn sàng cho dịch vụ LDAP. Bên cạnh đó là việc mở rộng LDAP (Scale) đáp ứng lưu lượng cao, bảo đảm hiệu năng, tính sẵn sàng dịch vụ. Để giải quyết vấn đề trên, LDAP cung cấp cơ chế replication để backup cũng như tăng tính sẵn sàng cho dịch vụ.
Replication là tiến trình được cấu hình từ 2 hoặc nhiều hơn service LDAP với mục đích chia sẻ chung nội dung dữ liệu được lưu trữ trong LDAP và giữ cho nội dung lưu trữ được đồng bộ, từ đó có thể cải thiện hiệu năng đọc (có nhiều node cho phép đọc dữ liệu từ LDAP, như master node, các replicate node).
Mô hình thông thường bao gồm 1 node master (Master DAS (Directory Server Agent) hoặc node provider). Node sẽ chịu trách nhiệm duy trì phiên bản dữ liệu chuẩn nhất của dịch vụ thư mục.
Bên cạnh master node là 1 hoặc nhiều shadow server (có thể gọi là consumer, replica, slave). các shadow server giữ dữ liệu của master server. Người dùng có thể kết nối vào shadow server để thực hiện search thông tin cây (DIT)

## Mô hình

## Chuẩn bị
- Cài trên 2 server tương đương với 2 node với cấu hình như sau:
- <b>Node 1:</b>
> HostName: ldap-01(Master)
> CPU: 2 cores
> Ram: 2Gb
> Disk: 30Gb
> Network: eth0: Network access (10.10.10.94)
- <b>Node 2:</b>
> HostName: ldap-02(Master)
> CPU: 2 cores
> Ram: 2Gb
> Disk: 30Gb
> Network: eth0: Network access (10.10.10.96)
----
## Cài đặt
### Cài đặt OpenLdap
- Cài đặt OpenLdap theo hướng dẫn tại đây: [Ubuntu](https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-openldap-and-phpldapadmin-on-ubuntu-16-04) | [Windows](https://www.maxcrc.de/userbooster/)
----
### Tại Node Master
- Bước 1: Kích hoạt <b><i>module syncprov</i></b>
```ssh
echo '
dn: cn=module,cn=config
objectClass: olcModuleList
cn: module
olcModulePath: /usr/lib64/openldap
olcModuleLoad: syncprov.la' > mod_syncprov.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f mod_syncprov.ldif
```
Kết quả:
```ssh
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "cn=module,cn=config"
```
- Bước 2: Cho phép <b><i>module syncprov</i></b> hoạt động trên DB LDAP
```ssh
echo '
dn: olcOverlay=syncprov,olcDatabase={2}hdb,cn=config
objectClass: olcOverlayConfig
objectClass: olcSyncProvConfig
olcOverlay: syncprov
olcSpSessionLog: 100' > syncprov.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f syncprov.ldif
```
Kết quả:
```ssh
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "olcOverlay=syncprov,olcDatabase={2}hdb,cn=config"
```
----
### Tại Node Slave
- Bước 1: Thiết lập <b><i>module syncrepl</i></b>
```ssh
cat <<EOF > syncrepl.ldif
dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcSyncRepl
olcSyncRepl: rid=001
provider=ldap://10.10.10.94:389/
bindmethod=simple
binddn="cn=Manager,dc=nhanhoa,dc=local"
credentials=Cloud365a@123
searchbase="dc=nhanhoa,dc=local"
scope=sub
schemachecking=on
type=refreshAndPersist
retry="30 5 300 3"
interval=00:00:05:00
EOF
ldapadd -Y EXTERNAL -H ldapi:/// -f syncrepl.ldif
```
Kết quả:
```ssh
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "olcDatabase={2}hdb,cn=config"
```
<b><u>Lưu ý:</u></b>
- ```provider```: Địa chỉ và port tới dịch vụ thư mục LDAP Master
- ```bìndmethod```: Loại xác thực
- ```binddn```: DN tài khoản xác thực
- ```credentials```: Mật khẩu tài khoản xác thực
- ```searchbase```: Đồng bộ các Entry theo filter
- ```type```: Phương thức đồng bộ
----
## Kiểm tra
### Bổ sung Entry mới vào Node Master
Thêm mới ou
```ssh
echo '
dn: ou=Tests,dc=nhanhoa,dc=local
ou: Tests
objectclass: organizationalUnit' > test_structure.ldif
ldapadd -x -D cn=Manager,dc=nhanhoa,dc=local -W -f test_structure.ldif
```
Kết quả:
```ssh
adding new entry "ou=Tests,dc=nhanhoa,dc=local"
```
Bổ sung user
```ssh
echo '
dn: cn=thanhbaba,ou=Tests,dc=nhanhoa,dc=local
objectClass: person
objectClass: inetOrgPerson
mail: thanhbaba@test.com
userPassword:: b3BlbnN0YWNr
cn: thanhbaba
sn: thanhbaba' > user_test_thanhbaba.ldif
ldapadd -x -D cn=Manager,dc=nhanhoa,dc=local -W -f user_test_thanhbaba.ldif
```
Kết quả
```ssh
adding new entry "cn=thanhbaba,ou=Tests,dc=nhanhoa,dc=local"
```
### Kiểm tra lại Slave
```ssh
ldapsearch -x -h 10.10.10.96 -b "dc=nhanhoa,dc=local" -s sub "(objectclass=person)"
```
Kết quả
```ssh
# extended LDIF
#
# LDAPv3
# base <dc=nhanhoa,dc=local> with scope subtree
# filter: (objectclass=person)
# requesting: ALL
#
# thanhbaba, Tests, nhanhoa.local
dn: cn=thanhbaba,ou=Tests,dc=nhanhoa,dc=local
objectClass: person
objectClass: inetOrgPerson
mail: thanhbaba@test.com
cn: thanhbaba
sn: thanhbaba
# search result
search: 2
result: 0 Success
# numResponses: 2
# numEntries: 1
```
### Tắt node Master, kiểm tra lại node Slave
Tại node Master
```ssh
[root@ldap_94 ~]# init 0
```
Tại node Slave
```ssh
ldapsearch -x -h 10.10.10.96 -b "dc=nhanhoa,dc=local" -s sub "(objectclass=person)"
```
Kết quả
```ssh
# extended LDIF
#
# LDAPv3
# base <dc=nhanhoa,dc=local> with scope subtree
# filter: (objectclass=person)
# requesting: ALL
#
# thanhbaba, Tests, nhanhoa.local
dn: cn=thanhbaba,ou=Tests,dc=nhanhoa,dc=local
objectClass: person
objectClass: inetOrgPerson
mail: thanhbaba@test.com
cn: thanhbaba
sn: thanhbaba
# search result
search: 2
result: 0 Success
# numResponses: 2
# numEntries: 1
```
<b><u>Lưu ý:</u></b> Với mô hình LDAP Master-Slave chỉ có thể ghi duy nhất tại Master, tại node Slave dữ liệu chỉ có thể đọc, không thể ghi
----
## Tổng kết
Như vậy ngoài mô hình LDAP master-master thì trong bài viết này mình đã triển khai mô hình LDAP master-slave. Ở bài viết tiếp theo mình sẽ hướng dẫn cách cấu hình LDAP với keepalive để tăng khả năng chịu lỗi của hệ thống.
---
## Tài liệu tham khảo
https://www.server-world.info/en/note?os=CentOS_7&p=openldap&f=5