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 caoCâ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:
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
web1
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
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
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
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