<a href="http://www.cnblogs.com/xibei666/p/5877548.html" target="_blank">http://www.cnblogs.com/xibei666/p/5877548.html</a>
概述
Haproxy下載下傳位址:
http://pkgs.fedoraproject.org/repo/pkgs/haproxy/
關閉SElinux、配置防火牆
1、vi /etc/selinux/config
#SELINUX=enforcing #注釋掉
#SELINUXTYPE=targeted #注釋掉
SELINUX=disabled #增加
:wq! #儲存退出
setenforce 0 #使配置立即生效
2、vi /etc/sysconfig/iptables #編輯
-A RH-Firewall-1-INPUT -d 224.0.0.18 -j ACCEPT #允許多點傳播位址通信
-A RH-Firewall-1-INPUT -p vrrp -j ACCEPT #允許VRRP(虛拟路由器備援協)通信
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #允許80端口通過防火牆
:wq! #儲存退出
/etc/init.d/iptables restart #重新開機防火牆使配置生效
安裝HAProxy
1、建立HAProxy運作賬戶群組
groupadd haproxy #添加haproxy組
useradd -g haproxy haproxy -s /bin/false #建立nginx運作賬戶haproxy并加入到haproxy組,不允許haproxy使用者直接登入系統
2、安裝:
3、設定HAProxy
mkdir -p /usr/local/haproxy/conf #建立配置檔案目錄
mkdir -p /etc/haproxy #建立配置檔案目錄
touch /usr/local/haproxy/conf/haproxy.cfg #建立配置檔案
ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg #添加配置檔案軟連接配接
cp -r /usr/local/src/haproxy-1.6.9/examples/errorfiles /usr/local/haproxy/errorfiles #拷貝錯誤頁面
ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles #添加軟連接配接
mkdir -p /usr/local/haproxy/log #建立日志檔案目錄
touch /usr/local/haproxy/log/haproxy.log #建立日志檔案
ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log #添加軟連接配接
cp /usr/local/src/haproxy-1.6.9/examples/haproxy.init /etc/rc.d/init.d/haproxy #拷貝開機啟動檔案
chmod +x /etc/rc.d/init.d/haproxy #添加腳本執行權限
chkconfig haproxy on #設定開機啟動
ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin #添加軟連接配接
4、配置haproxy.cfg參數
cp /usr/local/haproxy/conf/haproxy.cfg /usr/local/haproxy/conf/haproxy.cfg-bak #備份
vi /usr/local/haproxy/conf/haproxy.cfg #編輯,修改
按 Ctrl+C 複制代碼
#---------------------------------------------------------------------
# Global settings
global
log 127.0.0.1 local2 ###[err warning info debug]
chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid ###haproxy的pid存放路徑,啟動程序的使用者必須有權限通路此檔案
maxconn 4000 ###最大連接配接數,預設4000
user haproxy
group haproxy
daemon ###建立1個程序進入deamon模式運作。此參數要求将運作模式設定為"daemon"
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
defaults
mode http ###預設的模式mode { tcp|http|health },tcp是4層,http是7層,health隻會傳回OK
log global ###采用全局定義的日志
option dontlognull ###不記錄健康檢查的日志資訊
option httpclose ###每次請求完畢後主動關閉http通道
option httplog ###日志類别http日志格式
option forwardfor ###如果後端伺服器需要獲得用戶端真實ip需要配置的參數,可以從Http Header中獲得用戶端ip
option redispatch ###serverId對應的伺服器挂掉後,強制定向到其他健康的伺服器
timeout connect 10000 #default 10 second timeout if a backend is not found
timeout client 300000 ###用戶端連接配接逾時
timeout server 300000 ###伺服器連接配接逾時
maxconn 60000 ###最大連接配接數
retries 3 ###3次連接配接失敗就認為服務不可用,也可以通過後面設定
####################################################################
listen stats
bind 0.0.0.0:1080 #監聽端口
stats refresh 30s #統計頁面自動重新整理時間
stats uri /stats #統計頁面url
stats realm Haproxy Manager #統計頁面密碼框上提示文本
stats auth admin:admin #統計頁面使用者名和密碼設定
#stats hide-version #隐藏統計頁面上HAProxy的版本資訊
# main frontend which proxys to the backends
frontend main
bind 0.0.0.0:80
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static if url_static ###滿足政策要求,則響應政策定義的backend頁面
default_backend dynamic ###不滿足則響應backend的預設頁面
# static backend for serving up images, stylesheets and such
backend static
balance roundrobin ###負載均衡模式輪詢
server static 127.0.0.1:80 check ###後端伺服器定義
backend dynamic
balance roundrobin
server websrv1 10.252.97.106:80 check maxconn 2000
server websrv2 10.117.8.20:80 check maxconn 2000
# round robin balancing between the various backends
#errorloc 503 http://www.osyunwei.com/404.html
errorfile 403 /etc/haproxy/errorfiles/403.http
errorfile 500 /etc/haproxy/errorfiles/500.http
errorfile 502 /etc/haproxy/errorfiles/502.http
errorfile 503 /etc/haproxy/errorfiles/503.http
errorfile 504 /etc/haproxy/errorfiles/504.http
service haproxy start #啟動
service haproxy stop #關閉
service haproxy restart #重新開機
5、設定HAProxy日志
vi /etc/syslog.conf #編輯,在最下邊增加
# haproxy.log
local0.* /var/log/haproxy.log
local3.* /var/log/haproxy.log
vi /etc/sysconfig/syslog #編輯修改
SYSLOGD_OPTIONS="-r -m 0" #接收遠端伺服器日志
service syslog restart #重新開機syslog
5.浏覽器打開haproxy的監控頁面
如下:<code>http:</code><code>//120</code><code>.55.95.103:1080</code><code>/stats </code>//說明:1080即haproxy配置檔案中監聽端口,stats 即haproxy配置檔案中的監聽名稱
本文轉自flayber 51CTO部落格,原文連結:http://blog.51cto.com/406647516/1916011
,如需轉載請自行聯系原作者