標籤:linux系統負載平衡軟體之haproxy+apache
hproxy提供高可用性、負載平衡和基於TCP和HTTP應用的反向 Proxy,特別適用於那些負載特大的web網站,這些網站通常又需要會話保持或七層處理.haproxy運行在當前的硬體上,完全可以支援數以萬計的並發串連,並且它的運行模式使得它可以很簡單安全的整合到架構中, 同時可以保護你的web伺服器不被暴露到網路上.
環境規劃:
tong1: 192.168.1.247 haproxy
tong2: 192.168.1.248 web1
tong3: 192.168.1.249 web2
1.網路設定
tong1節點:
[[email protected] ~]# hostname
tong1
[[email protected] ~]# ifconfig | grep Mask
inet addr:192.168.1.247 Bcast:192.168.1.255 Mask:255.255.255.0
inet addr:127.0.0.1 Mask:255.0.0.0
[[email protected] ~]# vim /etc/hosts
192.168.1.247 tong1
192.168.1.248 tong2
192.168.1.249 tong3
[[email protected] ~]#
tong2節點和tong3節點一樣配置
2.在tong1節點安裝haproxy軟體,開啟haproxy日誌功能
[[email protected] ~]# yum install haproxy -y
[[email protected] ~]# vim /etc/sysconfig/rsyslog --添加-r參數記錄haproxy日誌
SYSLOGD_OPTIONS=" -r -c 2"
[[email protected] ~]# vim /etc/rsyslog.conf --定義日誌存放的路徑
local2.* /var/log/haproxy.log
[[email protected] ~]# /etc/init.d/rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
[[email protected] ~]#
[[email protected] ~]# cd /etc/haproxy/
[[email protected] haproxy]# vim haproxy.cfg
global
log 127.0.0.1 local2 --開啟日誌功能
chroot /var/lib/haproxy --啟動並執行路徑
pidfile /var/run/haproxy.pid --pid檔案存放處
maxconn 4000 --最大串連數
user haproxy --所屬啟動並執行使用者
group haproxy --所屬啟動並執行使用者組
daemon --後台運行服務
defaults
mode http --處理的模式(http是七層,tcp是四層)
log global --啟用全域日誌記錄
option httplog --日誌類別http格式
option dontlognull --不記錄健全狀態檢查日誌
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch --serverId對應的伺服器掛掉後強制定向到其它伺服器
retries 3 --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 admin_status --自訂監聽名,任意取
bind 0.0.0.0:80 --綁定連接埠
mode http --模式
log 127.0.0.1 local3 err --記錄錯誤記錄檔
stats refresh 20s --每隔20s重新整理
stats hide-version --隱藏haproxy版本資訊
stats uri /haproxy-stats --在網域名稱後面添加/haproxy-stats可以查看haproxy監控狀態
stats auth haproxy:system --使用者名稱和密碼 stats hide-version
stats admin if TRUE --可以手動啟動和停止服務
frontend main_status --定義acl規則
bind 0.0.0.0:80 --綁定到80連接埠
mode http
log global
option httplog
option forwardfor
acl web1 hdr_reg(host) -i ^(www.itnihao.cn|ww1.itnihao.cn)$ --匹配www.itnihao.cn和ww1.itnihao.cn兩個網域名稱就放到web1變數中
acl web2 url_sub -i killall= --請求中包含killall= 就放入到web2變數中
acl web3 path_beg -i /static /images /javascript /stylesheets
use_backend server_web1 if web1 --滿足web1變數的就丟到server_web1裡面的虛擬機器主機
use_backend server_web2 if web2 --滿足web1變數的就丟到server_web1裡面的虛擬機器主機
use_backend server_web3 if web3
default_backend server_web4 --都不滿足就丟到server_web4裡面的虛擬機器主機
backend server_web3
mode http
balance roundrobin
option httpchk GET /test.html --定義首頁 址
server tong2 192.168.1.248:81 check inter 1500 rise 3 fall 3 weight 1 --後端伺服器,inter 1500是檢查心跳頻率,rise 3 是3次正確可用,fall 3是3次失敗不可用,weight 1是權重
server tong3 192.168.1.249:82 check inter 1500 rise 3 fall 3 weight 1
backend server_web4
mode http
balance roundrobin
option httpchk GET /index.html
server tong2 192.168.1.248:80 check inter 1500 rise 3 fall 3 weight 1
server tong3 192.168.1.249:80 check inter 1500 rise 3 fall 3 weight 1
本文出自 “一起走過的日子” 部落格,請務必保留此出處http://tongcheng.blog.51cto.com/6214144/1597364
Linux系統負載平衡軟體之haproxy+apache