Haproxy+keepalivedphp
原理:html
HAProxy提供高可用性、負載均衡以及基於TCP和HTTP應用的代理,支持虛擬主機,它是免費、快速而且可靠的一種解決方案。根據官方數據,其最高極限支持10G的併發。mysql
HAProxy特別適用於那些負載特大的web站點, 這些站點一般又須要會話保持或七層處理。HAProxy運行在當前的硬件上,徹底能夠支持數以萬計的併發鏈接。而且它的運行模式使得它能夠很簡單安全的整合進您當前的架構中,同時能夠保護你的web服務器不被暴露到網絡上。linux
其支持從4層至7層的網絡交換,即覆蓋全部的TCP協議。就是說,Haproxy 甚至還支持 Mysql 的均衡負載。c++
若是說在功能上,能以proxy反向代理方式實現WEB均衡負載,這樣的產品有不少。包括 Nginx,ApacheProxy,lighttpd,Cheroke等。git
但要明確一點的,Haproxy 並非 Http 服務器。以上提到全部帶反向代理均衡負載的產品,都清一色是 WEB 服務器。簡單說,就是他們能自個兒提供靜態(html,jpg,gif..)或動態(php,cgi..)文件的傳輸以及處理。而Haproxy僅僅,並且專門是一款的用於均衡負載的應用代理。其自身並不能提供http服務。github
keepalived理論工做原理web
keepalived可提供vrrp以及health-check功能,能夠只用它提供雙機浮動的vip(vrrp虛擬路由功能),這樣能夠簡單實現一個雙機熱備高可用功能。redis
keepalived是一個相似於layer3, 4 & 5交換機制的軟件,也就是咱們平時說的第3層、第4層和第5層交換。Keepalived的做用是檢測web 服務器的狀態。 Layer3,4&5工做在IP/TCP協議棧的IP層,TCP層,及應用層,原理分別以下:sql
Layer3:Keepalived使用Layer3的方式工做式時,Keepalived會按期向服務器羣中的服務器
發送一個ICMP的數據包(既咱們平時用的Ping程序),若是發現某臺服務的IP地址沒有激活,Keepalived便報告這臺服務器失效,並將它從服務器羣中剔除,這種狀況的典型例子是某臺服務器被非法關機。Layer3的方式是以服務器的IP地址是否有效做爲服務器工做正常與否的標準。在本文中將採用這種方式。
Layer4:若是您理解了Layer3的方式,Layer4就容易了。Layer4主要以TCP端口的狀態來決定服務器工做正常與否。如web server的服務端口通常是80,若是Keepalived檢測到80端口沒有啓動,則Keepalived將把這臺服務器從服務器羣中剔除。
Layer5:Layer5就是工做在具體的應用層了,比Layer3,Layer4要複雜一點,在網絡上佔用的帶寬也要大一些。Keepalived將根據用戶的設定檢查服務器程序的運行是否正常,若是與用戶的設定不相符,則Keepalived將把服務器從服務器羣中剔除。
vip即虛擬ip,是附在主機網卡上的,即對主機網卡進行虛擬,此IP仍然是佔用了此網段的某個IP。
拓撲圖
1 安裝
#若tar -C無效,能夠用tar -xzf mysql.tar.gz -C /usr/local/mysql --strip-components=1,這個從github上看到的一個實例
#wget 如今好像不能下載了,只能去haproxy 的GitHub上下載了
cd /mnt/ &&wget http://www.haproxy.org/download/1.6/src/haproxy-1.6.5.tar.gz && tar zxf /mnt/haproxy-1.6.5.gz –C /mnt/
make TARGET=linux2628
make install PREFIX=/usr/local/haproxy
mkdir /usr/local/haproxy/conf
2配置
cat >> /usr/local/haproxy/conf/haproxy.cfg<<EOF
global
maxconn 51200
chroot /usr/local/haproxy
uid 99
gid 99
daemon
#quiet
nbproc 1
pidfile /var/run/haproxy.pid
defaults
mode http
#優化選項
retries 3
option redispatch
#option abortonclose
timeout connect 5000ms
timeout client 30000ms
timeout server 30000ms
#timeout check 2000
log 127.0.0.1 local0 info
balance roundrobin
listen admin_stats
bind 0.0.0.0:8888
option httplog
stats refresh 30s
stats uri /stats #監控頁面的url
stats realm Haproxy Manager
stats auth admin:admin #監控頁面的用戶名和密碼
stats hide-version
frontend http-in
bind *:8889
mode http
option httplog
log global
default_backend httpserver
backend httpserver
balance roundrobi
#server web1 192.168.0.78:80 cookie 1 weight 1 check inter 2000 rise 2 fall 3
#server web2 192.168.0.28:80 cookie 1 weight 1 check inter 2000 rise 2 fall 3
option httpchk GET /index.html #後端服務器檢測
#使用stick-table管理客戶端session,保持session會話
stick-table type ip size 5000k expire 5m store conn_cur
stick match src table httpserver
stick on src table httpserver
server s8001 192.168.0.227:800 maxconn 500 weight 10 cookie s8001 check
server s8002 192.168.0.229:800 maxconn 500 weight 10 cookie s8002 check
EOF
3 haproxy日誌
sed -i '13,14 s/^#//' /etc/rsyslog.conf
cat >> /etc/rsyslog.conf <<EOF
local3.* /var/log/haproxy.log
local0.* /var/log/haproxy.log
EOF
service rsyslog restart
4 啓動haproxy
cat >>/etc/init.d/haproxy <<EOF
#!/bin/sh
#chkconfig: 2345 10 90 #description:haproxy
# confit: /usr/local/haproxy/conf/haproxy.cfg
# pidfile: /usr/local/haproxy/logs/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="/usr/local/haproxy/conf/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=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
$exec -c -q -f $config
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
stop
start
}
reload() {
$exec -c -q -f $config
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Reloading $prog: "
$exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)
retval=$?
echo
return $retval
}
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
EOF
chmod u+x /etc/init.d/haproxy
Service haproxy start
Chkconfig haproxy on
5.keepalived
yum install openssl openssl-devel ipvsadm gcc gcc-c++ kernel-devel install libnl* popt* libnfnetlink-devel -y
cd /mnt && wget http://www.keepalived.org/software/keepalived-1.2.20.tar.gz &&tar zxf keepalived-1.2.20.tar.gz -C /mnt
cd keepalived-1.2.20
./configure --prefix=/usr/local/keepalived --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-573.22.1.el6.x86_64/
make && make install
ll /usr/local/keepalived/
ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
cat >>/etc/keepalived/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost #設置郵件
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id HAProxy_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
}
vrrp_script check_haproxy {
script "/mnt/shell/check_haproxy.sh"
#interval 2
#weight 2
}
vrrp_instance VI_1 {
state BACKUP #兩邊state 都爲BACKUP
interface eth0
virtual_router_id 78 #id 必須惟一,且不可衝突
priority 100 #優先級不同,數字越大,優先級越高
nopreempt #不主動搶佔資源,只在master這臺優先級高的設置,backup不設置
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.24
}
track_script {
check_haproxy
}
}
EOF
service keepalived start
chkconfig keepalived on
keepalived 檢測haproxy腳本,兩種方式取其一:
#!/bin/bash
A=`ps -C haproxy --no-header|wc -l`
if [ $A -eq 0 ];then
/etc/init.d/haproxy start
sleep 3
if [ `ps -C haproxy --no-header|wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
#add new other method
A=`curl -I http://192.168.0.24:8888/stats -u admin:admin|grep HTTP|awk '{print $2}'`
if [ $A -ne 200 ];then
/etc/init.d/haproxy start
sleep 3
if [ `curl -I http://192.168.0.24:8888/stats -u admin:admin|grep HTTP|awk '{print $2}'` -ne 200 ];then
/etc/init.d/keepalived sotp
fi
fi