執行個體1: 配置檔案:haproxy.conf
global
log 127.0.0.1local0
log 127.0.0.1local1 notice
#log loghostlocal0 info
maxconn 65535
#chroot /usr/share/haproxy
user haproxy
group haproxy
daemon
nbproc 10
#debug
#quiet
defaults
logglobal
modetcp
retries3
option redispatch
option dontlognull
#option forwardfor
maxconn65535
contimeout5000
clitimeout50000
srvtimeout50000
listen web_1 0.0.0.0:80
mode tcp
server nginx_1 *:80
server nginx_2 *:80
listen web_2 0.0.0.0:18000
server nginx_1 *:18000
server nginx_2 *:18000
一、環境說明
實驗環境
OS CentOS5.4
192.168.0.14 proxy
192.168.0.24 web1
192.168.0.64 web2

官方位址:http://haproxy.1wt.eu/
下載下傳位址:http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz
二、軟體安裝
點選(此處)折疊或打開
[root@cacti src]# tar xzvf haproxy-1.4.24.tar.gz
[root@cacti haproxy-1.4.24]# make TARGET=linux26 PREFIX=/usr/local/haproxy
[root@cacti haproxy-1.4.24]# make install PREFIX=/usr/local/haproxy
三、建立配置檔案
haproxy 預設是沒有配置檔案的,需要自己手機建立
vim /etc/haproxy.cfg
log 127.0.0.1 local3
maxconn 20480
chroot /usr/local/haproxy
uid 1004 #1004為haproxy 使用者的uid ,haproxy使用者需要自己手動建立
gid 1004
daemon
quiet
nbproc 1
pidfile /var/run/haproxy.pid
log global
mode http
option httplog
option httpclose
option forwardfor
option dontlognull
option redispatch
retries 3
balance roundrobin
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web_poll 192.168.0.14:80
option logasap
# option httpchk GET /index.html
server web1 192.168.0.24:80 cookie 1 check inter 2000 rise 3 fall 3
server web2 192.168.0.64:80 cookie 1 check inter 2000 rise 3 fall 3
listen status 192.168.0.14:8080
stats enable
stats uri /stats
stats auth admin:123456
stats realm (Haproxy\ statistic)
四、添加日志
[root@cacti ~]# vim /etc/syslog.conf
添加:
local3.* /var/log/haproxy.log
local0.* /var/log/haproxy.log
[root@cacti ~]# vim /etc/sysconfig/syslog
修改:
SYSLOGD_OPTIONS="-r -m 0"
service syslog restart
五、建立haproxy啟動腳本
[root@cacti ~]# vim /etc/init.d/haproxy
#!/bin/bash
#
# haproxy
# chkconfig: 35 85 15
# description: HAProxy is a free, very fast and reliable solution \
# offering high availability, load balancing, and \
# proxying for TCP and HTTP-based applications
# processname: haproxy
# config: /etc/haproxy.cfg
# pidfile: /var/run/haproxy.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
config="/etc/haproxy.cfg"
exec="/usr/local/haproxy/sbin/haproxy"
prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
lockfile=/var/lock/subsys/haproxy
check() {
$exec -c -V -f $config
}
start() {
$exec -c -q -f $config
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Starting $prog: "
# start it up here, usually something like "daemon $exec"
daemon $exec -D -f $config -p /var/run/$prog.pid
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
stop() {
echo -n $"Stopping $prog: "
# stop it here, often "killproc $prog"
killproc $prog
[ $retval -eq 0 ] && rm -f $lockfile
restart() {
stop
start
reload() {
echo -n $"Reloading $prog: "
$exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)
force_reload() {
restart
fdr_status() {
status $prog
case "$1" in
start|stop|restart|reload)
$1
;;
force-reload)
force_reload
checkconfig)
check
status)
fdr_status
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}"
exit 2
esac
六、啟動
[root@cacti etc]# /etc/init.d/haproxy start
Starting haproxy: [ OK ]
[root@cacti etc]# ps -ef | grep haproxy
100 2305 1 0 17:55 ? 00:00:00 /usr/local/sbin/haproxy -D -f /etc/haproxy.cfg -p /var/run/haproxy.pid
root 2308 774 0 17:55 pts/0 00:00:00 grep haproxy
[root@cacti etc]#
報錯:
[root@cacti ~]# /etc/init.d/haproxy start
Starting haproxy: [ALERT] 177/105503 (18602) : Starting proxy cacti: cannot bind socket
[FAILED]
産生這個錯誤可能有兩個原因,
1)沒有加入核心參數 (net.ipv4.ip_nonlocal_bind=1)
2)端口沖突
檢視狀态頁面
http://192.168.0.14:8080/stats
測試:
打開http:192.168.0.14
可以輪詢到兩個web伺服器上。
haproxy 配置檔案說明
來源http://www.linuxidc.com/Linux/2012-07/65350.htm
####################全局配置資訊########################
#######參數是程序級的,通常和作業系統(OS)相關#########
global
maxconn 20480 #預設最大連接配接數
log 127.0.0.1 local3 #[err warning info debug]
chroot /var/haproxy #chroot運作的路徑
uid 99 #所屬運作的使用者uid
gid 99 #所屬運作的使用者組
daemon #以背景形式運作haproxy
nbproc 1 #程序數量(可以設定多個程序提高性能)
pidfile /var/run/haproxy.pid #haproxy的pid存放路徑,啟動程序的使用者必須有權限通路此檔案
ulimit-n 65535 #ulimit的數量限制
#####################預設的全局設定######################
##這些參數可以被利用配置到frontend,backend,listen元件##
defaults
log global
mode http #所處理的類别 (#7層 http;4層tcp )
maxconn 20480 #最大連接配接數
option httplog #日志類别http日志格式
option httpclose #每次請求完畢後主動關閉http通道
option dontlognull #不記錄健康檢查的日志資訊
option forwardfor #如果後端伺服器需要獲得用戶端真實ip需要配置的參數,可以從Http Header中獲得用戶端ip
option redispatch #serverId對應的伺服器挂掉後,強制定向到其他健康的伺服器
option abortonclose #當伺服器負載很高的時候,自動結束掉目前隊列處理比較久的連接配接
stats refresh 30 #統計頁面重新整理間隔
retries 3 #3次連接配接失敗就認為服務不可用,也可以通過後面設定
balance roundrobin #預設的負載均衡的方式,輪詢方式
#balance source #預設的負載均衡的方式,類似nginx的ip_hash
#balance leastconn #預設的負載均衡的方式,最小連接配接
contimeout 5000 #連接配接逾時
clitimeout 50000 #用戶端逾時
srvtimeout 50000 #伺服器逾時
timeout check 2000 #心跳檢測逾時
####################監控頁面的設定#######################
listen admin_status #Frontend和Backend的組合體,監控組的名稱,按需自定義名稱
bind 0.0.0.0:65532 #監聽端口
mode http #http的7層模式
log 127.0.0.1 local3 err #錯誤日志記錄
stats refresh 5s #每隔5秒自動重新整理監控頁面
stats uri /admin?stats #監控頁面的url
stats realm itnihao\ itnihao #監控頁面的提示資訊
stats auth admin:admin #監控頁面的使用者和密碼admin,可以設定多個使用者名
stats auth admin1:admin1 #監控頁面的使用者和密碼admin1
stats hide-version #隐藏統計頁面上的HAproxy版本資訊
stats admin if TRUE #手工啟用/禁用,後端伺服器(haproxy-1.4.9以後版本)
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
#################HAProxy的日志記錄内容設定###################
capture request header Host len 40
capture request header Content-Length len 10
capture request header Referer len 200
capture response header Server len 40
capture response header Content-Length len 10
capture response header Cache-Control len 8
#######################網站監測listen配置#####################
###########此用法主要是監控haproxy後端伺服器的監控狀态############
listen site_status
bind 0.0.0.0:1081 #監聽端口
mode http #http的7層模式
log 127.0.0.1 local3 err #[err warning info debug]
monitor-uri /site_status #網站健康檢測URL,用來檢測HAProxy管理的網站是否可以用,正常傳回200,不正常傳回503
acl site_dead nbsrv(server_web) lt 2 #定義網站down時的政策當挂在負載均衡上的指定backend的中有效機器數小于1台時傳回true
acl site_dead nbsrv(server_blog) lt 2
acl site_dead nbsrv(server_bbs) lt 2
monitor fail if site_dead #當滿足政策的時候傳回503,網上文檔說的是500,實際測試為503
monitor-net 192.168.16.2/32 #來自192.168.16.2的日志資訊不會被記錄和轉發
monitor-net 192.168.16.3/32
########frontend配置############
#####注意,frontend配置裡面可以定義多個acl進行比對操作########
frontend http_80_in
bind 0.0.0.0:80 #監聽端口,即haproxy提供web服務的端口,和lvs的vip端口類似
log global #應用全局的日志配置
option httplog #啟用http的log
option httpclose #每次請求完畢後主動關閉http通道,HA-Proxy不支援keep-alive模式
option forwardfor #如果後端伺服器需要獲得用戶端的真實IP需要配置次參數,将可以從Http Header中獲得用戶端IP
########acl政策配置#############
acl itnihao_web hdr_reg(host) -i ^(www.itnihao.cn|ww1.itnihao.cn)$
#如果請求的域名滿足正規表達式中的2個域名傳回true -i是忽略大小寫
acl itnihao_blog hdr_dom(host) -i blog.itnihao.cn
#如果請求的域名滿足www.itnihao.cn傳回true -i是忽略大小寫
#acl itnihao hdr(host) -i itnihao.cn
#如果請求的域名滿足itnihao.cn傳回true -i是忽略大小寫
#acl file_req url_sub -i killall=
#在請求url中包含killall=,則此控制政策傳回true,否則為false
#acl dir_req url_dir -i allow
#在請求url中存在allow作為部分位址路徑,則此控制政策傳回true,否則傳回false
#acl missing_cl hdr_cnt(Content-length) eq 0
#當請求的header中Content-length等于0時傳回true
########acl政策比對相應#############
#block if missing_cl
#當請求中header中Content-length等于0阻止請求傳回403
#block if !file_req || dir_req
#block表示阻止請求,傳回403錯誤,目前表示如果不滿足政策file_req,或者滿足政策dir_req,則阻止請求
use_backend server_web if itnihao_web
#當滿足itnihao_web的政策時使用server_web的backend
use_backend server_blog if itnihao_blog
#當滿足itnihao_blog的政策時使用server_blog的backend
#redirect prefix http://blog.itniaho.cn code 301 if itnihao
#當通路itnihao.cn的時候,用http的301挑轉到http://192.168.16.3
default_backend server_bbs
#以上都不滿足的時候使用預設server_bbs的backend
##########backend的設定##############
#下面我将設定三組伺服器 server_web,server_blog,server_bbs
##################backend server_web####################
backend server_web
balance roundrobin #負載均衡的方式,roundrobin平均方式
cookie SERVERID #允許插入serverid到cookie中,serverid後面可以定義
option httpchk GET /index.html #心跳檢測的檔案
server web1 192.168.16.2:80 cookie web1 check inter 1500 rise 3 fall 3 weight 1
#伺服器定義,cookie 1表示serverid為web1,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,
#fall 3是3次失敗認為伺服器不可用,weight代表權重
server web2 192.168.16.3:80 cookie web2 check inter 1500 rise 3 fall 3 weight 2
#伺服器定義,cookie 1表示serverid為web2,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,
###################backend server_blog######################
backend server_blog
server blog1 192.168.16.2:80 cookie blog1 check inter 1500 rise 3 fall 3 weight 1
#伺服器定義,cookie 1表示serverid為blog1,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,fall 3是3次失敗認為伺服器不可用,weight代表權重
server blog2 192.168.16.3:80 cookie blog2 check inter 1500 rise 3 fall 3 weight 2
#伺服器定義,cookie 1表示serverid為blog2,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,fall 3是3次失敗認為伺服器不可用,weight代表權重
##################backend server_bbs########################
backend server_bbs
server bbs1 192.168.16.2:80 cookie bbs1 check inter 1500 rise 3 fall 3 weight 1
#伺服器定義,cookie 1表示serverid為bbs1,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,fall 3是3次失敗認為伺服器不可用,weight代表權重
server bbs2 192.168.16.3:80 cookie bbs2 check inter 1500 rise 3 fall 3 weight 2
#伺服器定義,cookie 1表示serverid為bbs2,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,fall 3是3次失敗認為伺服器不可用,weight代表權重
本文轉自 蔡小趙 51CTO部落格,原文連結:http://blog.51cto.com/zhaopeiyan/1830270