Cấu hình cân bằng tải HAPROXY

0
4593

Mô hình cơ bản :

  • HAProxy server (cài haproxy)
  • web server 01 (đã cài apache hoặc nginx) – 192.168.1.201
  • web server 02 ((đã cài apache hoặc nginx)- 192.168.1.202
    # yum install haproxy

    Tập tin cấu hình chính của HAProxy là  /etc/haproxy/haproxy.cfg

    # nano /etc/haproxy/haproxy.cfg

     

    Xóa hết nội dung và nhập nội dung bên dưới:

    global
            daemon
            maxconn 256
    
        defaults
            mode http
            timeout connect 5000ms
            timeout client 50000ms
            timeout server 50000ms
    
        frontend http-in
            bind *:80
            default_backend servers
    
        backend servers
            balance roundrobin
            server webserver1 192.168.1.201:80 check
            server webserver2 192.168.1.202:80 check

    Cấu hình trên là cấu hình basic cho một loadbalancer,  chỉ cần quan tâm đến phần

    backend servers
            balance roundrobin   (thuật toán cân bằng tải)
            server webserver1 192.168.1.201:80 check
            server webserver2 192.168.1.202:80 check

     

    Khởi động dịch vụ HAproxy

    service haproxy start

    Tiến hành truy cập địa chỉ IP haproxy , F5 nhiều lần để kiểm tra

Cấu hình giao diện web GUI của HAproxy

Thêm dòng

listen stats *:8080
    stats enable
    stats uri /
    #stats hide-version
    #stats auth user:pass   (kích hoạt đăng nhập)

Truy cập qua : IP:8080 để vào giao diện web GUI

Cấu hình giao diện web GUI đa chức năng hơn:
Tham khảo tại:  https://docs.datadoghq.com/integrations/haproxy/#installation

https://www.datadoghq.com/blog/monitor-haproxy-with-datadog/

Giải pháp cho tên miền sử dụng SSL

Đối với tên miền sử dụng SSL , cần thêm tham số sau vào haproxy.cfg

Tham số

bind *:443 ssl crt /home/test.pem
    option forwardfor
    reqadd X-Forwarded-Proto:\ https

Như vậy cấu hình mới sẽ có dạng :

global
        daemon
        maxconn 256

    defaults
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms

   # frontend http-in
   #    bind *:80
frontend localhost
    bind *:80
    bind *:443 ssl crt /home/test.pem     (tham số SSL)
    option forwardfor
    reqadd X-Forwarded-Proto:\ https
    mode http
       default_backend servers

    backend servers
        balance roundrobin
        server webserver1 192.168.1.201:80 check
        server webserver2 192.168.1.202:80 check

test.pem là file chứa certificate và private key ứng với tên miền sử dụng SSL

Đối với trường hợp có nhiều tên miền sử dụng SSL

bind *:443 ssl crt web1.pem crt web2.pem crt web(n).pem

Hoặc

bind *:443 ssl crt-list crt-list.txt

file crt-list.txt chưa tập hợp các đường dẫn của .pem
VD:

/etc/ssl/private/mydomain.pem
/etc/ssl/private/myotherdomain.pem

 

Thực hiện restart dịch vụ haproxy

service haproxy restart

 

Tại 2 máy chủ webserver , thực hiện cấu hình SSL cho tên miền

Bước cuối cùng , Thực hiện cấu hình trỏ DNS tên miền về IP của HA proxy

Bonus dành cho mã nguồn wordpress:

Trường hợp truy cập wp-admin bị loop dẫn tới lỗi  ERR_TOO_MANY_REDIRECTS

Thêm đoạn sau vào wp-config.php , phía dưới dòng:/* That’s all, stop editing! Happy publishing. */

/** SSL */  
define('FORCE_SSL_ADMIN', true);  
// in some setups HTTP_X_FORWARDED_PROTO might contain  
// a comma-separated list e.g. http,https  
// so check for https existence  
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)  
    $_SERVER['HTTPS']='on';

 

 

Giải pháp đồng bộ dữ liệu , sử dụng Lsyncd 

Rsync nhẹ, cho tốc độ nhanh, nhưng có một điểm yếu là chưa tích hợp tính năng Live, tức là mọi thay đổi chưa được đồng bộ ngay lập tức. Rsync cũng chưa cấu hình được để đồng bộ 2 chiều trong trường hợp Load Balancing. Và để thực hiện tự động thì cần đặt lịch (tạo cron) cho Rsync.

Để giải quyết các tình trạng mà Rsync không làm được, chúng ta có thể sử dụng Lsyncd

Lsyncd (Live Syncing Daemon), sử dụng tiện ích rsync, kết hợp với SSH. Lsyncd nhẹ, cài đặt và cấu hình đơn giản. Tôi sẽ sử dụng Lsyncd để đồng bộ dữ liệu giữa 2 servers CentOS

Chú ý: Thực hiện cài đặt SSH key 2 chiều cho  Server01 và Server02

1. Cài đặt Rsync và  Lsyncd cho cả 2 server

Yêu cầu:  Tham khảo hướng dẫn cài đặt SSH giữa server

sudo yum -y install rsync
sudo yum -y install lsyncd

Tạo thư mục và file chứa cấu hình, log:

mkdir -p /var/log/lsyncd
touch /var/log/lsyncd/lsyncd.{log,status}

2. Cấu hình Lsyncd

Chỉnh sửa file sudo vi  /etc/lsyncd.conf

a. Server01

Nội dung như sau:

----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
--
settings {
 logfile = "/var/log/lsyncd.log", -- Sets the log file
 statusFile = "/var/log/lsyncd-status.log" -- Sets the status log file
 }
 sync{
 default.rsyncssh, -- uses the rsyncssh defaults Take a look here: https://github.com/axkibe/lsyncd/blob/master/default-rsyncssh.lua
 delete = true, -- Doesn't delete files on the remote host eventho they're deleted at the source. This might be beneficial for some not for others
 source="/home/test.vn/", -- Your source directory to watch
 host="192.168.1.202", -- The remote host (use hostname or IP)
 exclude={"/storage"}, -- To exclude file & folder (not sync)
 targetdir="/home/test.vn/", -- the target dir on remote host, keep in mind this is absolute path
 rsync = {
 archive = true, -- use the archive flag in rsync
 perms = true, -- Keep the permissions
 owner = true, -- Keep the owner
 _extra = {"-a"}, -- Sometimes permissions and owners isn't copied correctly so the _extra can be used for any flag in rsync
 },
 delay = 5, -- We want to delay the syncing for 5 seconds so we queue up the events
 maxProcesses = 1, -- We only want to use a maximum of 4 rsync processes at same time
 ssh = {
 port =22 -- This section allows configuration for ssh specific stuff such as a different port
 }
 }

 

b. Server02

Nội dung như sau:

----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
--
settings {
 logfile = "/var/log/lsyncd.log", -- Sets the log file
 statusFile = "/var/log/lsyncd-status.log" -- Sets the status log file
 }
 sync{
 default.rsyncssh, -- uses the rsyncssh defaults Take a look here: https://github.com/axkibe/lsyncd/blob/master/default-rsyncssh.lua
 delete = true, -- Doesn't delete files on the remote host eventho they're deleted at the source. This might be beneficial for some not for others
 source="/home/test.vn/", -- Your source directory to watch
 host="192.168.1.201", -- The remote host (use hostname or IP)
 exclude={"/storage"}, -- To exclude file & folder (not sync)
 targetdir="/home/test.vn/", -- the target dir on remote host, keep in mind this is absolute path
 rsync = {
 archive = true, -- use the archive flag in rsync
 perms = true, -- Keep the permissions
 owner = true, -- Keep the owner
 _extra = {"-a"}, -- Sometimes permissions and owners isn't copied correctly so the _extra can be used for any flag in rsync
 },
 delay = 5, -- We want to delay the syncing for 5 seconds so we queue up the events
 maxProcesses = 1, -- We only want to use a maximum of 4 rsync processes at same time
 ssh = {
 port =22 -- This section allows configuration for ssh specific stuff such as a different port
 }
 }

c. Khởi động dịch vụ và check log

systemctl start lsyncd
tail -f /var/log/lsyncd.log

Như vậy khi 1 trong 2 server có sự thay đổi cập nhật dữ liệu sẽ tự động được đồng bộ sang server còn lại

Leave a Reply

avatar
  Subscribe  
Notify of