概述
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、安裝:
[root@A local]# yum install -y gcc [root@A local]# tar zxvf haproxy-1.6.9.tar.gz [root@A local]# cd haproxy-1.6.9 [root@A local]# make TARGET=linux3100 CPU=x86_64 PREFIX=/usr/local/haprpxy #編譯 uname -r #查看系統內核版本號 [root@A local]# make install PREFIX=/usr/local/haproxy #安裝 #數說明: #TARGET=linux3100 #使用uname -r查看內核,如:2.6.18-371.el5,此時該參數就為linux26 #kernel 大於2.6.28的用:TARGET=linux2628 #CPU=x86_64 #使用uname -r查看系統信息,如x86_64 x86_64 x86_64 GNU/Linux,此時該參數就為x86_64 #PREFIX=/usr/local/haprpxy #/usr/local/haprpxy為haprpxy安裝路徑
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 #編輯,修改
#--------------------------------------------------------------------- # 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
:wq! #保存退出
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
:wq! #保存退出
vi /etc/sysconfig/syslog #編輯修改
SYSLOGD_OPTIONS="-r -m 0" #接收遠程服務器日志
:wq! #保存退出
service syslog restart #重啟syslog
5.瀏覽器打開haproxy的監控頁面
如下:http:
//120
.55.95.103:1080
/stats
//說明:1080即haproxy配置文件中監聽端口,stats 即haproxy配置文件中的監聽名稱
參考博客
http://www.osyunwei.com/archives/7512.html
http://www.cnblogs.com/kgdxpr/p/3272861.html
http://www.cnblogs.com/MacoLee/p/5853413.html