基本信息:
系統(tǒng)平臺:VMware WorkStation
系統(tǒng)版本: CentOS Linux release 7.2.1511 (Core)
內核版本: 3.10.0-327.el7.x86_64
集群架構:
前端:HAProxy
1粥诫、虛擬FQDN:www.simpletime.net
2、VIP:192.168.39.1;DIP:172.16.39.50
3烤镐、調度服務器:Varnish1洁闰、Varnish2
4筐付、調度算法:URL_Hash_Consistent
5辩尊、集群統(tǒng)計頁:172.16.39.50:9091/simpletime?admin
緩存服務器:Varnish
1只锻、VarnishServer1:172.16.39.14:9527
2、VarnishServer2:172.16.39.15:9527
3樱溉、開啟健康狀態(tài)探測挣输,提供高可用
4、負載均衡后端Web服務器組
5福贞、動靜分離后端服務器撩嚼,并動靜都提供負載均衡效果
后端服務器:
StaticServer1:172.16.39.14:80
StaticServer2:172.16.39.15:80
DynamicServer1:172.16.39.151
DynamicServer2:172.16.39.152
Mysql服務器:
MysqlServer:172.16.39.150
思考:
1、負載均衡動靜分離后,會話如何保持完丽?
2恋技、負載均衡動靜分離后,存儲如何解決逻族?
3蜻底、該方案適用于什么樣的場景?
4瓷耙、該方案缺陷有哪些朱躺?
5、如何改進搁痛?
一长搀、部署HAProxy
1、安裝HAProxy
~]#yuminstallHAProxy
2鸡典、配置HAProxy
mainfrontendwhichproxystothebackends
frontendweb*:80
aclurl_staticpath_beg-i/static/images/javascript/stylesheets
aclurl_staticpath_end-i.jpg.gif.png.css.js.html.txt.htm
aclurl_dynamicpath_begin-i.php.jsp
default_backendstatic_srvifurl_static
use_backenddynamic_srvifurl_dynamic
use_backendvarnish_srv
---------------------------------------------------------------------
roundrobinbalancingbetweenthevariousbackends
---------------------------------------------------------------------
backendvarnish_srv
balanceuri#使用基于URL的一致性哈希調度算法
hash-typeconsistent
servervarnish1172.16.39.14:9527check
servervarnish2172.16.39.15:9527check
listenstats#開啟HAProxy圖形化Web管理功能
bind:9091
statsenable
statsuri/simpletime?admin
statshide-version
statsauthadmin:abc.123
statsadminifTRUE
3源请、啟動服務
~]#systemctlstarthaproxy
~]#systemctlstatushaproxy#查看狀態(tài)
~]#ss-tnlp#查看80和9091端口是否啟用
~]#systemctlenablehaproxy#設置開機啟動
二、部署Varnish彻况,兩臺配置一致(172.16.39.14|15)
1谁尸、安裝及配置
~]#yuminstallvarnish-y
~]#vim/etc/varnish/varnish.params
VARNISH_LISTEN_PORT=9527#更改默認端口
~]#systemctlstartvarnish
~]#systemctlenablevarnish
~]#vim/etc/varnish/default.vcl
vcl4.0;
##############啟用負載均衡模塊###############
importdirectors;
################定義Purge-ACL控制#######################
aclpurgers{
"127.0.0.1";
"172.16.39.0"/16;
}
Defaultbackenddefinition.Setthistopointtoyourcontentserver.
##############配置健康狀態(tài)探測##############
probeHE{#靜態(tài)檢測
.url="/health.html";#指定檢測URL
.timeout=2s;#探測超時時長
.window=5;#探測次數
.threshold=2;#探測次數成功多少次才算健康
.initial=2;#Varnish啟動探測后端主機2次健康后加入主機
.interval=2s;#探測間隔時長
.expected_response=200;#期望狀態(tài)響應碼
}
probeHC{#動態(tài)監(jiān)測
.url="/health.php";
.timeout=2s;
.window=5;
.threshold=2;
.initial=2;
.interval=2s;
.expected_response=200;
}
#############添加后端主機################
backendweb1{
.host="172.16.39.151:80";
.port="80";
.probe=HC;
}
backendweb2{
.host="172.16.39.152:80";
.port="80";
.probe=HC;
}
backendapp1{
.host="172.16.39.14:80";
.port="80";
.probe=HE;
}
backendapp2{
.host="172.16.39.15:80";
.port="80";
.probe=HE;
}
#############定義負載均衡及算法###############
subvcl_init{
newwebcluster=directors.round_robin();
webcluster.add_backend(web1);
webcluster.add_backend(web2);
newappcluster=directors.round_robin();
appcluster.add_backend(app1);
appcluster.add_backend(app2);
}
################定義vcl_recv函數段######################
subvcl_recv{
ACL未授權,不允許PURGE纽甘,并返回405#####
if(req.method=="PURGE"){
if(!client.ip~purgers){
return(synth(405,"Purgingnotallowedfor"+client.ip));
}
return(purge);
}
添加首部信息良蛮,使后端服務記錄訪問者的真實IP
if(req.restarts==0){
setreq.http.X-Forwarded-For=req.http.X-Forwarded-For+","+client.ip;
}else{
setreq.http.X-Forwarded-For=client.ip;
}
setreq.backend_hint=webcluster.backend();
setreq.backend_hint=appcluster.backend();
注:因為Varnish不是一級代理,配置forward只能取到上級代理IP悍赢,而上級代理IP决瞳,本身就包含在HAProxy發(fā)送過來的Forward里,所以沒必要配置左权,而后端服務器只要日志格式有啟用記錄Forward信息皮胡,并且上級代理沒有做限制,那么赏迟,就能獲取到客戶端真實IP屡贺;
動靜分離#####
if(req.url~"(?i).(php|asp|aspx|jsp|do|ashx|shtml)($|?)"){
setreq.backend_hint=appcluster.backend();
}
不正常的請求不緩存#####
if(req.method!="GET"&&
req.method!="HEAD"&&
req.method!="PUT"&&
req.method!="POST"&&
req.method!="TRACE"&&
req.method!="OPTIONS"&&
req.method!="PATCH"&&
req.method!="DELETE"){
return(pipe);
}
如果請求不是GET或者HEAD,不緩存#####
if(req.method!="GET"&&req.method!="HEAD"){
return(pass);
}
如果請求包含Authorization授權或Cookie認證锌杀,不緩存#####
if(req.http.Authorization||req.http.Cookie){
return(pass);
}
啟用壓縮甩栈,但排除一些流文件壓縮#####
if(req.http.Accept-Encoding){
if(req.url~".(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$"){
unsetreq.http.Accept-Encoding;
}elseif(req.http.Accept-Encoding~"gzip"){
setreq.http.Accept-Encoding="gzip";
}elseif(req.http.Accept-Encoding~"deflate"){
setreq.http.Accept-Encoding="deflate";
}else{
unsetreq.http.Accept-Encoding;
}
}
return(hash);
}
####################定義vcl_pipe函數段#################
subvcl_pipe{
return(pipe);
}
subvcl_miss{
return(fetch);
}
####################定義vcl_hash函數段#################
subvcl_hash{
hash_data(req.url);
if(req.http.host){
hash_data(req.http.host);
}else{
hash_data(server.ip);
}
if(req.http.Accept-Encoding~"gzip"){
hash_data("gzip");
}elseif(req.http.Accept-Encoding~"deflate"){
hash_data("deflate");
}
}
##############設置資源緩存時長#################
subvcl_backend_response{
if(beresp.http.cache-control!~"s-maxage"){
if(bereq.url~"(?i).(jpg|jpeg|png|gif|css|js|html|htm)$"){
unsetberesp.http.Set-Cookie;
setberesp.ttl=3600s;
}
}
}
################啟用Purge#####################
subvcl_purge{
return(synth(200,"Purged"));
}
###############記錄緩存命中狀態(tài)##############
subvcl_deliver{
if(obj.hits>0){
setresp.http.X-Cache="HITfrom"+req.http.host;
setresp.http.X-Cache-Hits=obj.hits;
}else{
setresp.http.X-Cache="MISSfrom"+req.http.host;
}
unsetresp.http.X-Powered-By;
unsetresp.http.Server;
unsetresp.http.Via;
unsetresp.http.X-Varnish;
unsetresp.http.Age;
}
2、加載配置糕再,因為還沒有配置后端應用服務器谤职,可以看到后端主機健康檢測全部處于Sick狀態(tài)
~]#varnishadm-S/etc/varnish/secret-T127.0.0.1:6082
varnish>vcl.loadconf1default.vcl
200
VCLcompiled.
varnish>vcl.useconf1
200
VCL'conf1'nowactive
varnish>backend.list
200
BackendnameRefsAdminProbe
web1(172.16.39.151,,80)15probeSick0/5
web2(172.16.39.152,,80)15probeSick0/5
app1(172.16.39.14,,80)15probeSick0/5
app2(172.16.39.15,,80)15probeSick0/5
三、部署Mysql(172.16.39.150)
~]#yuminstallmariadb.server
~]#rpm-qemariadb-server
mariadb-server-5.5.44-2.el7.centos.x86_64
~]#vim/etc/my.cnf#數據庫基本優(yōu)化
[mysqld]
innodb_file_per_table=ON
skip_name_resolve=ON
~]#mysql#創(chuàng)建wordpress數據庫并授權該數據庫用戶
createdatabasewwwdb;
grantallonwwwdb.*towww@'172.16.39.%'identifiedby"abc.123";
exit
四亿鲜、部署NFS文件系統(tǒng)
1、后端所有主機安裝服務
~]#yuminstallnfs-utils
2、動態(tài)資源主機172.16.39.152設為動態(tài)web數據共享服務器
DynamicServer2~]#vim/etc/exports
/data/web/172.16.39.151/16(rw,sync)#rw=可讀寫蒿柳,sync=內存及硬盤同步寫入數據
3饶套、靜態(tài)主機172.16.39.15設為靜態(tài)web數據共享服務器
StaticServer2~]#vim/etc/exports
/data/web/172.16.39.14/16(rw,sync)#rw=可讀寫,sync=內存及硬盤同步寫入數據
~]#systemctlstartnfs-server#啟動服務
DynamicServer2~]#exportfs-avr#重載配置
exporting172.16.39.151/16:/data/web
StaticServer2~]#exportfs-avr#重載配置
exporting172.16.39.14/16:/data/web
4垒探、兩臺服務端設為開機啟動
~]#systemctlenablenfs-server
5妓蛮、客戶端同步,動態(tài)主機掛載動態(tài)服務器共享,靜態(tài)主機掛載靜態(tài)服務器共享
~]#showmount-e172.16.39.152
Exportlistfor172.16.39.152:
/data/web172.16.39.151/16
~]#mount-tnfs172.16.39.15:/data/web/data/web
五圾叼、部署后端主機(注意:已經部署了NFS文件系統(tǒng))
1蛤克、安裝及配置(DynamicServer2:172.16.39.152)
~]#yuminstallnginxphp-fpmphp-mysql-y
~]#mkdir/data/web/www-pv
~]#vim/etc/nginx/conf.d/www.simple.com.conf
server{
listen80;
root/data/web/www;
server_namewww.simple.com;
indexindex.htmlindex.htmindex.php;
location~[^/].php(/|$){
try_files$uri=404;
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
includefastcgi.conf;
access_log_bypass_if($uri='/health.php');
}
}
備注:access_log_bypass_if需添加日志過濾模塊,本文主要實現過濾健康狀態(tài)檢測信息夷蚊;
~]#systemctlstartnginxphp-fpm
2构挤、部署wordpress應用
~]#unzipwordpress-4.3.1-zh_CN.zip
~]#mvwordpress/*/data/web/www/
www]#cpwp-config{-sample,}.php
www]#vimwp-config.php
define('DB_NAME','wwwdb');
define('DB_USER','www');
define('DB_PASSWORD','abc.123');
define('DB_HOST','172.16.39.150');
3、設置facl權限
~]#idapache
~]#setfacl-mu:apache:rwx/data/web/www
4惕鼓、拷貝web數據至StaticServer2,另兩臺后端主機掛載的是兩臺NFS服務端的數據文件筋现,web數據數完成
~]#tar-jcvfweb.tar.gz/data/web/www
~]#scpweb.tar.gz172.16.39.15:
~]#setfacl-mu:apache:rwx/data/web/www
StaticServer2~]#tar-xfweb.tar.gz-C/data/web
5、創(chuàng)建動靜資源主機組Varnish健康狀態(tài)探測頁面
DynamicServer2~]#echo"<h1>DynamicServerisHealth.</h1>>/data/web/www/health.php
StaticServer2~]#echo"<h1>StaticServerisHealth.</h1>">/data/web/www/health.html
6箱歧、在Varnish主機上查看健康狀態(tài)(172.16.39.14|15矾飞,也就是StaticServer主機)
StaticServer2~]#varnishadm-S/etc/varnish/secret-T127.0.0.1:6082
varnish>backend.list#后端Web主機正常
200
BackendnameRefsAdminProbe
web1(172.16.39.151,,80)15probeHealthy5/5
web2(172.16.39.152,,80)15probeHealthy5/5
app1(172.16.39.14,,80)15probeHealthy5/5
app2(172.16.39.15,,80)15probeHealthy5/5
7、web訪問172.16.39.50完成wordpress配置
參考文件:http://architecture.callback001.cn/loadbalance/18399521581039620778.html