Cân bằng tải máy chủ server sql webserver cài đặt haproxy

Cân bằng tải máy chủ server sql webserver bằng hướng dẫn cài đặt haproxy trên centos 7. Load balancing là kỹ thuật phân phối tải trên các web site có lượng truy cập cao

Cân bằng tải máy chủ server sql webserver bằng hướng dẫn cài đặt haproxy trên centos 7. Load balancing là kỹ thuật phân phối tải trên các web site có lượng truy cập cao. Giải pháp hỗ trợ việc tối ưu hóa tài nguyên, tăng sự đảm bảo, giảm độ trễ trên hệ thống.

Các thuật toán cân bằng tải cơ bản:


HAProxy viết tắt (High Availability Proxy) là phần mềm mã nguồn mở, cung cấp dịch vụ cân bằng tải, proxy cho TCP/HTTP dựa trên request người dùng. HAProxy hiện đã và đang được sử dụng bởi nhiều website lớn như GoDaddy, GitHub, Bitbucket, Stack Overflow, Reddit, Speedtest.net, Twitter và trong nhiều sản phẩm cung cấp bởi Amazon Web Service.

Thiết lập cân bằng tải máy chủ ban đầu

hostnamectl set-hostname loadbalancer
yum update -y

Tắt Firewall và SELinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld

Cấu hình Host file

echo "10.10.11.86 loadbalancer" >> /etc/hosts
echo "10.10.11.87 web1" >> /etc/hosts
echo "10.10.11.88 web2" >> /etc/hosts
Khởi động lại centos
init 6

Tại node web1

Thiết lập hostname, cập nhật hệ thống

hostnamectl set-hostname web1
yum update -y

Tắt Firewall và SELinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld

Cấu hình Host file

echo "10.10.11.86 loadbalancer" >> /etc/hosts
echo "10.10.11.87 web1" >> /etc/hosts
echo "10.10.11.88 web2" >> /etc/hosts

Khởi động lại hệ thống

init 6

Tại node web2

Thiết lập hostname, cập nhật hệ thống

hostnamectl set-hostname web2
yum update -y

Tắt Firewall và SELinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld

Cấu hình Host file

echo "10.10.11.86 loadbalancer" >> /etc/hosts
echo "10.10.11.87 web1" >> /etc/hosts
echo "10.10.11.88 web2" >> /etc/hosts

Khởi động lại hệ thống

init 6

Phần 2. Cài đặt haproxy

Bước 1: Cài đặt HAProxy phiên bản 1.8

Thực hiện tại node loadbalancer

Lưu ý:

Cài đặt

sudo yum install wget socat -y
wget http://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
echo 'global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    stats socket /var/lib/haproxy/stats
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

listen stats
    bind :8080
    mode http
    stats enable
    stats uri /stats
    stats realm HAProxy Statistics

listen webcluster
    bind :80
    balance  roundrobin
    mode  http
    option  forwardfor
    server web1 10.10.11.87:80 check
    server web2 10.10.11.88:80 check' > /etc/haproxy/haproxy.cfg

Khởi động lại dịch vụ HAProxy

sudo systemctl enable haproxy
sudo systemctl restart haproxy

Bước 2: Cài đặt Apache

Thực hiện tại web1

yum install httpd -y
cat /etc/httpd/conf/httpd.conf | grep 'Listen 80'
sed -i "s/Listen 80/Listen 10.10.11.87:80/g" /etc/httpd/conf/httpd.conf

echo 'CHAO MUNG TOI SIEUTOCVIET' > /var/www/html/index.html systemctl start httpd systemctl enable httpd 

Thực hiện tại web2

yum install httpd -y
cat /etc/httpd/conf/httpd.conf | grep 'Listen 80'
sed -i "s/Listen 80/Listen 10.10.11.88:80/g" /etc/httpd/conf/httpd.conf
echo 'CHAO MUNG TOI SIEUTOCVIET' > /var/www/html/index.html systemctl start httpd systemctl enable httpd 


 
CSKH 24/7
(028)66 82 82 99
Kỹ thuật 247
0968 592 399

Tư vấn 1
0939 861 299
Tư vấn 2
0919 383 299
Gọi
 Sales
 Sales
 Tech