Bài viết này sẽ hướng dẫn bạn cách setup một hệ thống load balancer gồm 2 node mạng, sử dụng HAProxy và Keepalived trên CentOS 7. Hệ thống load balancer này sẽ đứng giữa user và 2 ( hoặc hơn ) máy chủ Apache server với nội dung giống nhau. Nếu một trong hai server bị down, thì tất cả các request từ phía người dùng sẽ được tự động redirect sang server còn lại.
Để cấu hình HA- Load balancer, bạn cần 2 máy server ảo/ vật lý đóng vai trò load balancer và 2 máy server ảo/ vật lý cần load balance. Ngoài 4 địa chỉ IP dành cho các máy chủ, chúng ta cần thêm một địa chỉ IP ảo ( VIP ) thứ 5. 2 máy load balancer và địa chỉ IP cần ở trong một phân đoạn mạng.
Trên hệ điều hành CentOS 7 mới nhất, thì công cụ Load balancer Piranha đã được thay thế bởi HAProxy và keepalived. Trong đó, HAProxy là phần mềm được dùng để thực hiện Load- balancing, Keepalived đóng vai trò highavaibility, và Apache là phần mềm cần load balance.
Các máy host :
- Load Balencer 1: haproxy1, IP: 192.168.0.101
- Load Balencer 2: haproxy2, IP: 192.168.0.102
- Web Server 1: httpd1, IP: 192.168.0.103
- Web Server 2: httpd2, IP: 192.168.0.104
Chúng ta cũng cần 1 địa chỉ IP ảo, đứng giữa haproxy1 và haproxy2 : vip, IP: 192.168.0.10.
Dưới đây là danh sách mà chúng ta sẽ ghi vào file /etc/hosts của mỗi server:
192.168.0.100 vip 192.168.0.101 haproxy1 192.168.0.102 haproxy2 192.168.0.103 httpd1 192.168.0.104 httpd2
Chúng ta có thể hình dung bằng sơ đồ :
shared IP=192.168.0.100
192.168.0.101 192.168.0.102 192.168.0.103 192.168.0.104
——————–+———————+——————-+——————-+
| | | |
+–+–+ +–+–+ +—-+—-+ +—-+—-+
| haproxy1 | | haproxy2 | | httpd1 | | httpd2 |
+—–+ +—–+ +———+ +———+
haproxy haproxy 2 web servers (Apache)
keepalived keepalived
Cài đặt HAProxy :
Trên HAProxy1/ HAProxy2 server, làm theo các hướng dẫn này :
Cài đặt HAProxy package :
yum install -y haproxy
Mở /etc/haproxy/haproxy.cfg file bằng cách sử dụng lệnh Vi, thay thế dòng “frontend main *:5000″ bằng dòng “frontend main *:80″ và comment vào dòng “use_backend static if url_static”.
Đi đến cuối file, bỏ đi dòng bắt đầu bằng “ server app “ và thay thế chúng bằng dòng :
server httpd1 192.168.0.103:80 check server httpd2 192.168.0.104:80 check
Start và tự động enable HAProxy mỗi lần hệ thống boot lên :
systemctl enable haproxy systemctl start haproxy
Mở file /etc/firewalld/services/haproxy.xml và paste vào những dòng này :
<?xml version="1.0" encoding="utf-8"?> <service> <short>HAProxy</short> <description>HAProxy load-balancer</description> <port protocol="tcp" port="80"/> </service>
Tiếp theo chúng ta cần gán quyền truy cập cho file haproxy.xml :
cd /etc/firewalld/services restorecon haproxy.xml chmod 640 haproxy.xml
Update cấu hình firewall :
firewall-cmd --permanent --add-service=haproxy firewall-cmd –reload
Cài đặt Keepalived :
Chúng ta cần cài đặt Keepalived trên haproxy1 và haproxy2 :
Cài đặt gói Keepalived :
yum install -y keepalived
Tạo một file mới /etc/keepalived/keepalived.conf và paste những dòng sau vào :
vrrp_script chk_haproxy { script "killall -0 haproxy" # check the haproxy process interval 2 # every 2 seconds weight 2 # add 2 points if OK } vrrp_instance VI_1 { interface eth0 # interface to monitor state MASTER # MASTER on haproxy1, BACKUP on haproxy2 virtual_router_id 51 priority 101 # 101 on haproxy1, 100 on haproxy2 virtual_ipaddress { 192.168.0.100 # virtual ip address } track_script { chk_haproxy } }
Chạy dòng lệnh sau để tự enable dịch vụ keepalived khi hệ thống boot :
systemctl enable keepalived systemctl start keepalived
Tiếp theo, chúng ta cần kiểm tra sự hiện diện của VIP trên máy chủ haproxy1:
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 52:54:00:f7:2a:a9 brd ff:ff:ff:ff:ff:ff inet 192.168.0.101/24 brd 192.168.0.255 scope global eth0 valid_lft forever preferred_lft forever inet 192.168.0.100/32 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fef7:2aa9/64 scope link valid_lft forever preferred_lft forever
Để cài đặt Apache, bạn có thể xem ở link này nhé.
Tạo một file index.html trong thư mục /var/www/html ở server httpd1 và paste những dòng này :
Test httpd1
Thực hiện tương tự trên server httpd2 nhưng thay thế từ “httpd1” bằng “httpd2″ trong file index.html.
Từ một server khác, test cấu hình :
yum install -y elinks elinks http://192.168.0.100
Như vậy là chúng ta đã hoàn thành.
- haproxy centos 7
- keepalived là gì
- keepalive apache
- keepalived haproxy
- keepalived log
- high availability centos 7
- load balancer centos 7