1? 概述
本文通過HAProxy,Keepalive,實現(xiàn)動靜分離wordpress,在每臺haproxy和wordpress服務(wù)器之間加入緩存,通過緩存實現(xiàn)加速。具體需求如下
.(1)動靜分離部署wordpress,動靜都要能實現(xiàn)負載均衡
.(2)在haproxy和后端主機之間添加varnish進行緩存
.(3) haproxy的設(shè)定要求:動靜分離
.(4) haproxy高可用
.(5)通過VIP:172.18.50.80來訪問wordpress
前提
1:node1 ip是172.18.50.62,node2 ip 是172.18.50.63.寫入/etc/hosts文件里,node1和node2實現(xiàn)基于ssh-key的驗證。
2:所有主機時間同步。
拓撲圖如下

2? 配置
172.18.50.62? HAproxy配置,主備配置一樣,172.18.50.63不附上代碼
global
log?????????127.0.0.1?local2
chroot??????/var/lib/haproxy
pidfile?????/var/run/haproxy.pid
maxconn?????4000
user????????haproxy
group???????haproxy
daemon
#?turn?on?stats?unix?socket
stats?socket?/var/lib/haproxy/stats
#---------------------------------------------------------------------
#?common?defaults?that?all?the?'listen'?and?'backend'?sections?will
#?use?if?not?designated?in?their?block
#---------------------------------------------------------------------
defaults
mode????????????????????http
log?????????????????????global
option??????????????????httplog
option??????????????????dontlognull
option?http-server-close
option?forwardfor???????except?127.0.0.0/8
option??????????????????redispatch
retries?????????????????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
frontend??http
bind?*:80
acl?url_dyn?path_end?-i?.php
acl?url_stac?path_end?-i??.jpg?.gif?.png?.css?.js?.html?.txt?.ico?.svg
reqadd?sunny-x-via:\?haproxy6b
default_backend?dynamicblog?if?url_dyn
use_backend????staticblog?if?url_stac
option?forwardfor????header?sunny-x-client
rspdel?Server
rspadd??Server:\?Sunny-proxy6b
backend?staticblog
balance?????roundrobin
cookie?WEBSRV?insert?nocache
server??????varnish7b?172.18.50.72:80?check?weight?1???inter?3000?rise?2?fall?2?cookie?cksrv1
backend?dynamicblog
balance?????roundrobin
cookie?WEBSRV?insert?nocache
server?????varnish7c?172.18.50.73:80?check?weight?1??inter?3000?rise?2?fall?2?cookie?cksrv2
listen?stats
bind?:9091
stats?enable
stats?auth??admin:admin
stats?admin??if?TRUE
172.18.50.62? HAproxy配置,主備配置有區(qū)別,區(qū)別在于四點:
notification_email_from node2@localhost
router_id node2
state BACKUP
priority 90
172.18.50.63不附上代碼。
!?Configuration?File?for?keepalived
global_defs?{
notification_email?{
root@localhost
}
notification_email_from?node1@localhost
smtp_server?127.0.0.1
smtp_connect_timeout?30
router_id?node1
vrrp_mcast_group4?224.0.100.100
}
vrrp_script??chk_down?{
script?"[[?-f???/etc/keepalived/down?]]?&&?exit?1?||?exit?0"
interval??1
weight??-20
}
vrrp_script??chk_haproxy?{
script?"killall?-0?haproxy?&&?exit?0?||?exit?1"
interval?1
weight??-20
fall?2
rise?1
}
vrrp_instance?VI_1?{
state?MASTER
interface?eth1
virtual_router_id?50
priority?100
advert_int?1
authentication?{
auth_type?PASS
auth_pass?sunny
}
virtual_ipaddress?{
172.18.50.80
}
notify_master?"/etc/keepalived/vip1_notify.sh?master"
notify_backup?"/etc/keepalived/vip1_notify.sh?backup"
notify_fault??"/etc/keepalived/vip1_notify.sh?fault"
track_script?{
chk_down
chk_haproxy
}
}
172.18.50.62? HAproxy配置里notify.sh腳本如下
#!/bin/bash
contact='root@localhost'
notify()?{
mailsubject="$(hostname)?to?be?$1,?vip
floating"
mailbody="$(date?+'%F?%T'):?vrrp?transition,
$(hostname)?changed?to?be?$1"
echo?"$mailbody"?|?mail?-s
"$mailsubject"?$contact
}
case??$1??in
master)
notify?master
;;
backup)
notify?backup
;;
fault)
notify?fault
;;
*)
echo?"Usage:?$(basename?$0)
{master|backup|fault}"
exit?1
;;
esac
172.18.50.73 varnish配置default.vcl如下,172.18.50.72配置區(qū)別如下
backend static {
.host = "172.18.50.65";
.port = "80";
.probe = check;
}
其他配置一樣,172.18.50.72這里就附上腳本
vcl?4.0;
probe?check?{
.url?=?"/index.html";
.window?=?5;
.threshold?=?4;
.interval?=?2s;
.timeout?=?1s;
}
backend?dynamic?{
.host?=?"172.18.50.75";
.port?=?"80";
.probe?=?check;
}
sub?vcl_recv?{
if?(req.restarts?==?0){
if?(req.http.X-Forwarded-For){
set?req.http.X-Forwarded-For?=?req.http.X-Forwarded-For?+?","?+?client.ip;
}?else?{
set?req.http.X.Forwarded-For?=?client.ip;
}
}
}
sub?vcl_backend_response?{
if?(beresp.http.cache-control?!~?"s-maxage")?{
if?(bereq.url?~?"(?i)\.(jpg|jpeg|png|gif|css|js|xml)$")?{
unset?beresp.http.Set-Cookie;
set?beresp.ttl?=?3600s;
}
}
if?(bereq.url?~?"(?i).*php.*")?{
unset?beresp.http.Set-Cookie;
set?beresp.ttl?=?3600s;
}
}
sub?vcl_deliver?{
if?(obj.hits>0)?{
set?resp.http.X-Cache?=?"Hit?via?"?+?server.ip;
}?else?{
set?resp.http.X-Cache?=?"Miss?from?"?+?server.ip;
}
}
后端的172.18.50.65和172.18.50.75這兩臺wordpress都要安裝wordpress和http,同時172.18.50.75是處理動態(tài)資源的腳本,所以還需要安裝php-fpm.同時在75這臺安裝mysql.這里就不演示了,相關(guān)部署見http://ghbsunny.blog.51cto.com/7759574/1970716文章。j
到這里部署就完成了??梢酝ㄟ^網(wǎng)頁訪問http://172.18.50.80/blog查看功能是否正常。其中,這里的blog是將wordpress安裝包部署后重命名為blog,可根據(jù)實際情況自行設(shè)定。