Hướng dẫn dùng ldap filter trong PHP

Tổng quan

Trong bài blog này, mình sẽ hướng dẫn các bạn cách cấu hình LDAP ở chế độ Multi-Master Master. Mô hình Multi-Master được sử dụng nhằm tăng khả năng mở rộng, tăng hiệu suất đọc ghi dữ liệu vào dịch vụ ldap. Bên cạnh đó, mình sẽ áp dụng Pacemaker và HAProxy để điều phối cân bằng tải dịch vụ giữa các node Master, tăng sự sẵn sàng của dịch vụ, cải thiện khả năng chịu lỗi và vượt qua lỗi trên dịch vụ LDAP.

Mô hình bên dưới rất phức tạp, các bạn tham khảo thêm tại:

  • Chuỗi bài viết về HAProxy
  • Chuỗi bài viết về High Availability
  • Chuỗi bài viết về LDAP

Mô hình

Mô hình triển khai

Mô hình quản trị dịch vụ bằng pacemaker

Chuẩn bị

  • Cài đặt 3 node LDAP với IP như sau

HOSTNAME    ldap_94 [LDAP Master 94]
CPU         2 core
RAM         2 GB
Disk        vda: os
Network     eth0: network access [10.10.10.94]

HOSTNAME    ldap_95 [LDAP Master 95]
CPU         2 core
RAM         2 GB
Disk        vda: os
Network     eth0: network access [10.10.10.95]

HOSTNAME    ldap_96 [LDAP Master 96]
CPU         2 core
RAM         2 GB
Disk        vda: os
Network     eth0: network access [10.10.10.96]

Bổ sung config tại tất cả các node

Cấu hình file host

echo '
10.10.10.94 ldap_94 
10.10.10.95 ldap_95 
10.10.10.96 ldap_96' >> /etc/hosts

Kích hoạt module syncprov

cat > mod_syncprov.ldif  syncprov.ldif  mod_syncprov.ldif  syncprov.ldif  master.ldif  /etc/sysconfig/slapd  master.ldif  /etc/sysconfig/slapd  master.ldif  /etc/sysconfig/slapd  /usr/share/phpldapadmin/config/config.php 
EOF

Tắt dịch vụ HTTP [Dịch vụ HTTP sẽ được xử lý bởi pacemaker]

systemctl stop httpd
systemctl disable httpd

Triển khai HAProxy

Thực hiện trên tất cả các node

Cài đặt

sudo yum install wget socat -y
wget //cbs.centos.org/kojifiles/packages/haproxy/1.8.1/5.el7/x86_64/haproxy18-1.8.1-5.el7.x86_64.rpm 
yum install haproxy18-1.8.1-5.el7.x86_64.rpm -y

Tạo bản backup cho cấu hình mặc định và chỉnh sửa cấu hình HAproxy

cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak

Cầu hình Haproxy

cat > /etc/haproxy/haproxy.cfg > /etc/rsyslog.conf

echo 'local2.*    /var/log/haproxy.log' > /etc/rsyslog.d/haproxy.conf

systemctl restart rsyslog

Bổ sung cấu hình cho phép kernel có thể binding tới IP VIP

echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf

Kiểm tra

$ sysctl -p

net.ipv4.ip_nonlocal_bind = 1

Khởi tạo dịch vụ HAProxy

systemctl stop haproxy
systemctl disable haproxy

Thiết lập Cluster Pacemaker

Bước 1: Cài đặt pacemaker corosync

Thực hiện trên tất cả các node

Cài đặt gói pacemaker pcs

yum -y install pacemaker pcs

systemctl start pcsd 
systemctl enable pcsd

Thiết lập mật khẩu user hacluster

Bước 2: Tạo Cluster

Thực hiện trên 1 node duy nhất ldap_94

Chứng thực các node

pcs cluster auth ldap_94 ldap_95 ldap_96

Kết quả

[root@ldap_94 ~]# pcs cluster auth ldap_94 ldap_95 ldap_96
Username: hacluster
Password: *********
ldap_96: Authorized
ldap_94: Authorized
ldap_95: Authorized

Khởi tạo cấu hình cluster ban đầu

pcs cluster setup --name ha_cluster ldap_94 ldap_95 ldap_96

Kết quả

[root@ldap_94 ~]# pcs cluster setup --name ha_cluster ldap_94 ldap_95 ldap_96

Destroying cluster on nodes: ldap_94, ldap_95, ldap_96...
ldap_95: Stopping Cluster [pacemaker]...
ldap_94: Stopping Cluster [pacemaker]...
ldap_96: Stopping Cluster [pacemaker]...
ldap_95: Successfully destroyed cluster
ldap_94: Successfully destroyed cluster
ldap_96: Successfully destroyed cluster

Sending 'pacemaker_remote authkey' to 'ldap_94', 'ldap_95', 'ldap_96'
ldap_94: successful distribution of the file 'pacemaker_remote authkey'
ldap_96: successful distribution of the file 'pacemaker_remote authkey'
ldap_95: successful distribution of the file 'pacemaker_remote authkey'
Sending cluster config files to the nodes...
ldap_94: Succeeded
ldap_95: Succeeded
ldap_96: Succeeded

Synchronizing pcsd certificates on nodes ldap_94, ldap_95, ldap_96...
ldap_96: Success
ldap_94: Success
ldap_95: Success
Restarting pcsd on the nodes in order to reload the certificates...
ldap_96: Success
ldap_94: Success
ldap_95: Success

Khởi động Cluster

Kết quả

[root@ldap_94 ~]# pcs cluster start --all

ldap_94: Starting Cluster [corosync]...
ldap_95: Starting Cluster [corosync]...
ldap_96: Starting Cluster [corosync]...
ldap_95: Starting Cluster [pacemaker]...
ldap_94: Starting Cluster [pacemaker]...
ldap_96: Starting Cluster [pacemaker]...

Cho phép cluster khởi động cùng OS

Kết quả

[root@ldap_94 ~]# pcs cluster enable --all 

ldap_94: Cluster Enabled
ldap_95: Cluster Enabled
ldap_96: Cluster Enabled

Bước 3: Thiết lập Cluster

Bỏ qua cơ chế STONITH

pcs property set stonith-enabled=false

Cho phép Cluster chạy kể cả khi mất quorum

pcs property set no-quorum-policy=ignore

Hạn chế Resource trong cluster chuyển node sau khi Cluster khởi động lại

pcs property set default-resource-stickiness="INFINITY"

Tạo Resource IP VIP Cluster

pcs resource create Virtual_IP ocf:heartbeat:IPaddr2 ip=10.10.10.97 cidr_netmask=24 op monitor interval=30s

Tạo Resource quản trị dịch vụ HAProxy

pcs resource create Loadbalancer_HaProxy systemd:haproxy op monitor timeout="5s" interval="5s"

Tạo Resource quản trị dịch vụ HTTPD

pcs resource create Web_Cluster systemd:httpd op monitor timeout="5s" interval="5s"
pcs resource clone Web_Cluster globally-unique=true

Ràng buộc thứ tự khởi động dịch vụ, khởi động dịch vụ Virtual_IP sau đó khởi động dịch vụ Loadbalancer_HaProxy

pcs constraint order start Virtual_IP then Loadbalancer_HaProxy kind=Optional

Ràng buộc resource Virtual_IP phải khởi động cùng node với resource Loadbalancer_HaProxy

pcs constraint colocation add Virtual_IP Loadbalancer_HaProxy INFINITY

Ràng buộc thứ tự khởi động dịch vụ, khởi động dịch vụ Virtual_IP sau đó khởi động dịch vụ Web_Cluster

pcs constraint order Virtual_IP then Web_Cluster-clone

Kiểm tra dịch vụ

Kiểm tra dịch vụ HAProxy

Thêm dữ liệu vào LDAP

Thêm dữ liệu vào node Master 96

echo '
dn: cn=thanhbaba,ou=Tests,dc=nhanhoa,dc=local
objectClass: person
objectClass: inetOrgPerson
mail: 
userPassword:: b3BlbnN0YWNr
cn: thanhbaba
sn: thanhbaba' > user_test_thanhbaba.ldif

ldapadd -x -h 10.10.10.97 -D cn=Manager,dc=nhanhoa,dc=local -W -f user_test_thanhbaba.ldif

Kết quả

adding new entry "cn=thanhbaba,ou=Tests,dc=nhanhoa,dc=local"

Kiểm tra dữ liệu trên node Master 94

[root@ldap_94 ~]# ldapsearch -x -h 10.10.10.97 -b "dc=nhanhoa,dc=local" -s sub "[objectclass=person]"
# extended LDIF
#
# LDAPv3
# base  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: 
cn: thanhbaba
sn: thanhbaba

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

Truy vấn dữ liệu từ LDAP

Thử truy vấn dữ liệu bằng ldapclient

ldapsearch -x -h 10.10.10.97 -b "dc=nhanhoa,dc=local" -s sub "[ou=*]"

Kết quả

[root@ldap_94 ~]# ldapsearch -x -h 10.10.10.97 -b "dc=nhanhoa,dc=local" -s sub "[ou=*]"
# extended LDIF
#
# LDAPv3
# base  with scope subtree
# filter: [ou=*]
# requesting: ALL
#

# People, nhanhoa.local
dn: ou=People,dc=nhanhoa,dc=local
objectClass: organizationalUnit
ou: People

# Group, nhanhoa.local
dn: ou=Group,dc=nhanhoa,dc=local
objectClass: organizationalUnit
ou: Group

# search result
search: 2
result: 0 Success

# numResponses: 3
# numEntries: 2

Kiểm tra failover

Tắt 1 LDAP Master 94, check HAProxy stats page

Trạng thái HAProxy stats page

Ghi dữ liệu vào LDAP qua IP VIP

echo '
dn: cn=thanhbaba6,ou=People,dc=nhanhoa,dc=local
objectClass: person
objectClass: inetOrgPerson
mail: 
userPassword:: b3BlbnN0YWNr
cn: thanhbaba6
sn: thanhbaba6' > user_test_thanhbaba6.ldif

ldapadd -x -h 10.10.10.97 -D cn=Manager,dc=nhanhoa,dc=local -W -f user_test_thanhbaba6.ldif

Kết quả

[root@ldap_95 ~]# ldapadd -x -h 10.10.10.97 -D cn=Manager,dc=nhanhoa,dc=local -W -f user_test_thanhbaba6.ldif
Enter LDAP Password: 
adding new entry "cn=thanhbaba6,ou=People,dc=nhanhoa,dc=local"

Sang node LDAP Master 96

ldapsearch -x -h 10.10.10.97 -b "dc=nhanhoa,dc=local" -s sub "[cn=thanhbaba6]"

Kết quả

[root@ldap_96 ~]# ldapsearch -x -h 10.10.10.97 -b "dc=nhanhoa,dc=local" -s sub "[cn=thanhbaba6]"
# extended LDIF
#
# LDAPv3
# base  with scope subtree
# filter: [cn=thanhbaba6]
# requesting: ALL
#

# thanhbaba6, People, nhanhoa.local
dn: cn=thanhbaba6,ou=People,dc=nhanhoa,dc=local
objectClass: person
objectClass: inetOrgPerson
mail: 
cn: thanhbaba6
sn: thanhbaba6

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

Dữ liệu đọc ghi bình thường, cụm LDAP không xảy ra vấn đề gì khi 1 node gặp sự cố

Tài liệu tham khảo

//blog.cloud365.vn/search/?q=haproxy

//blog.cloud365.vn/search/?q=High+Availability

Thực hiện bởi cloud365.vn

Written by Nguyễn Bá Thành

Chuỗi bài viết về LDAP - Series

Chuỗi bài viết về LDAP và tích hợp LDAP với các phần mềm nguồn mở khác.

  • 1. LDAP [Part 1] - Giới thiệu về LDAP

    Giới thiệu và tìm hiểu về LDAP.

  • 2. LDAP [Part 2] - Cài đặt OpenLDAP trên CentOS 7

    Cài đặt OpenLDAP trên CentOS 7

  • 3. LDAP [Part 3] - Hướng dẫn cài đặt phpLDAPadmin

    Hướng dẫn cài đặt phpldapadmin trên CentOS 7

  • 4. LDAP [Part 4] - Xác thực SSH với LDAP

    Xác thực SSH với LDAP

  • 5. LDAP [Part 5] - Tích hợp LDAP với Pfsense

    Tích hợp LDAP với Pfsense

  • 6. LDAP [Part 6] - OpenVPN Pfsense đối với user được tích hợp từ LDAP

    OpenVPN Pfsense đối với user được tích hợp từ LDAP

  • 7. LDAP [Part 7] - Tích hợp LDAP với phpIPAM

    Tích hợp LDAP với phpIPAM

  • 8. LDAP [Part 8] - Tích hợp LDAP với Check_MK

    Tích hợp LDAP với Check_MK

  • 9. LDAP [Part 9] - Tích hợp LDAP với Graylog

    Tích hợp LDAP với Graylog

  • 10. LDAP [Part 10] - Hướng dẫn triển khai LDAP Master - Master trên CentOS 7

    Hướng dẫn triển khai LDAP Master - Master trên CentOS 7

  • 11. LDAP [Part 11] - Hướng dẫn triển khai LDAP Master - Slave trên CentOS 7

    Hướng dẫn triển khai LDAP Master - Slave trên CentOS 7

  • 12. LDAP [Part 12] - Hướng dẫn cấu hình Keepalive cho mô hình LDAP Master Master

    Hướng dẫn cấu hình Keepalive cho mô hình LDAP Master Master

  • 13. LDAP [Part 13] - Hướng dẫn triển khai LDAP Multi Master Haproxy Pacemaker trên CentOS 7

    Hướng dẫn triển khai LDAP Multi-Master Haproxy Pacemaker trên CentOS 7

  • 14. LDAP [Part 14] - Hướng dẫn backup và restore trên CentOS 7

    Hướng dẫn backup và restore trên CentOS 7

Chủ Đề