如何為共享 Web 流量設定 HAproxy 負載均衡伺服器
本文旨在準備網站的高可用性。那些希望其網站對客戶或使用者始終可用的人也可以使用它,並使用高可用性伺服器並在其伺服器之間共享。它是一個免費的開源應用程式,用作 TCP/HTTP 負載均衡器,它將 Web 流量分發到多臺伺服器,並提高 Web 伺服器的效能和可靠性。
安裝
假設負載均衡器 HA 代理伺服器 IP 地址為 http://192.167.57.150
HAproxy 伺服器詳細資訊
作業系統:Centos 6.7,IP 地址:192.168.57.150
WebServer1
作業系統:Centos 6.7,IP 地址:192.168.57.147
WebServer2
作業系統:Centos 6.7,IP 地址:192.168.57.148
我們需要在兩臺 Web 伺服器(即 192.168.57.151 和 192.168.57.152)上安裝 Apache。
# yum install http*
安裝後,請從 Web 瀏覽器訪問 Apache http://your-server-ip-address
安裝 HAProxy 伺服器
# yum install haproxy openssl-devel
開啟主機檔案並在所有 3 臺伺服器(HAproxy 負載均衡器、webserver1、webserver2)中新增以下行
192.168.87.150 haproxy.demo.com haproxy 192.168.87.151 webserv1.demo.com haproxy 192.168.87.152 webserv2.demo.com haproxy # vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.87.150 haserver.cgi.com haserver 192.168.87.151 ha2.cgi.com ha2 192.168.87.152 ha1.cgi.com ha1
現在,您需要啟用 HAproxy 日誌以識別未來除錯的問題
# vi /etc/haproxy/haproxy.cfg
新增以下行以啟用日誌記錄
log 127.0.0.1 local Needed to enable the UDp SYSLOG reception in /etc/rsyslog.conf # vi /etc/rsyslog.conf #### MODULES #### $ModLoad imuxsock # provides support for local system logging (e.g. Via logger command) $ModLoad imklog # provides kernel logging support (previously done by rklogd) #$ModLoad immark # provides --MARK-- message capability # Provides UDP syslog reception $ModLoad imudp $UDPServerRun 514 # Provides TCP SYSLOG reception #$ModLoad imtcp #$InputTCPServerRun 514
現在在 /etc/rsyslog.d/ 資料夾中建立一個 haproxy.conf 以建立日誌檔案
# vi /etc/rsyslog.d/haproxy.conf
新增以下行以建立檔案
local.* /var/log/haproxy.log
重新啟動 rsyslog 服務以更新我們所做的更改
# service rsyslog restart
配置 HAproxy 全域性設定
we needed to setup the glogal setting in /etc/haproxy/haproxy.cfg # vi /etc/haproxy/haproxy.cfg #--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- 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
在發生故障的情況下,我們必須提供兩臺 Web 伺服器作為前端和後端伺服器的詳細資訊。
frontend LB bind 192.168.87.150:80 reqadd X-Forwarded-Proto: http default_backend LB backend LB 192.168.87.150:80 mode http stats enable stats hide-version stats uri /stats stats realm Haproxy Statistics stats auth haproxy:redhat # Credentials for HAProxy Statistic report page. balance roundrobin # Load balancing will work in round-robin process. option httpchk option httpclose option forwardfor cookie LB insert server ha2 192.168.87.151:80 cookie ha1 check # backend server. server ha1 192.168.87.152:80 cookie ha2 check # backend server.
我們需要重新啟動服務並使更改生效。
# service haproxy restart
要自動啟動服務,請使用以下命令
# chkconfig haproxy on
檢查自動啟動 -
# chkconfig --list haproxy
服務重新啟動後,我們就可以在 http://192.168.87.150:/stats 上訪問負載均衡器。
為了測試 HAproxy 負載均衡器,我們必須在兩臺 Web 伺服器中建立一個包含以下程式碼的 index.html 檔案。
在 webserver1 中建立一個包含以下程式碼的 index.html 檔案。
<html> <head> <title>HAProxy Test Page</title> </head> <body> <!-- Main content --> <h1>My HAProxy Test Page Server 1 </h1> </body> </html>
在 webserver2 中建立一個包含以下程式碼的 index.html 檔案。
<html> <head> <title>HAProxy Test Page</title> </head> <body> <!-- Main content --> <h1>My HAProxy Test Page Server 2 </h1> </body> </html>
現在訪問 **haproxy** 伺服器 IP http://192.168.57.150
預設情況下,您可以看到 webserver 1 的頁面
Webserver1
現在,透過轉到 webserver 1 並停止 HTTPd 伺服器來測試負載均衡
# service httpd stop
然後,訪問 **haproxy** 伺服器 IP,http://192.168.57.150
現在,您將自動看到 webserver2 的網頁
Webserver2
要進行驗證,您可以訪問 HAproxy 負載均衡器統計資訊頁面 http://192.168.87.150/stats
HAproxy
結論
配置後,即使其中一臺伺服器關閉或不可用,您也應該能夠無中斷地訪問您的網站。同樣,我們也可以為 Apache 設定 HAproxy。您還可以觀察到,透過配置上述內容,Webserver1 在 HAproxy 負載均衡器統計資訊頁面中不可訪問或已關閉。您還可以根據需要新增任意數量的伺服器。