HAProxy+Varnish+LNMP实现高可用负载均衡动静分离集群部署

基本信息:
系统平台:VMware WorkStation
系统版本: 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、集群统计页:172.16.39.50:9091/simpletime?admin

缓存服务器:Varnish
1、VarnishServer1:172.16.39.14:9527
2、VarnishServer2:172.16.39.15:9527
3、开启健康状态探测,提供高可用
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#查看状态
~]#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.

##############配置健康状态探测##############
probeHE{#静态检测
.url="/health.html";#指定检测URL
.timeout=2s;#探测超时时长
.window=5;#探测次数
.threshold=2;#探测次数成功多少次才算健康
.initial=2;#Varnish启动探测后端主机2次健康后加入主机
.interval=2s;#探测间隔时长
.expected_response=200;#期望状态响应码
}
probeHC{#动态监测
.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发送过来的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"));
}
###############记录缓存命中状态##############
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状态
~]#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#数据库基本优化
[mysqld]
innodb_file_per_table=ON
skip_name_resolve=ON
~]#mysql#创建wordpress数据库并授权该数据库用户

createdatabasewwwdb;
grantallonwwwdb.*towww@'172.16.39.%'identifiedby"abc.123";
exit
四、部署NFS文件系统
1、后端所有主机安装服务
~]#yuminstallnfs-utils
2、动态资源主机172.16.39.152设为动态web数据共享服务器
DynamicServer2~]#vim/etc/exports
/data/web/172.16.39.151/16(rw,sync)#rw=可读写,sync=内存及硬盘同步写入数据
3、静态主机172.16.39.15设为静态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、客户端同步,动态主机挂载动态服务器共享,静态主机挂载静态服务器共享
~]#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文件系统)
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需添加日志过滤模块,本文主要实现过滤健康状态检测信息;

~]#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、创建动静资源主机组Varnish健康状态探测页面
DynamicServer2~]#echo"<h1>DynamicServerisHealth.</h1>>/data/web/www/health.php
StaticServer2~]#echo"<h1>StaticServerisHealth.</h1>">/data/web/www/health.html
6、在Varnish主机上查看健康状态(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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容

  • 1.介绍 运维日常: 2.Web Page Cache: varnish2.0,3.0处理过程 varnish4....
    尛尛大尹阅读 3,321评论 0 0
  • 缓存的基础知识 1、程序本身具有局部性 时间局部性过去访问到的数据,也有可能被两次访问 空间局部性一个数据被访问到...
    魏镇坪阅读 1,928评论 1 3
  • 本文编译自:users-guide 本节讲述如何使用 VCL 编写处理 HTTP 流量的策略。 Varnish 的...
    C86guli阅读 3,056评论 0 1
  • 一、理论基础 二种常用的开源解决方案squid、varnish 条件式请求解决缓存与后端服务器内容更新不匹配问题的...
    一言不合_c8a0阅读 2,911评论 0 2
  • LULUPARK的奇幻旅程 我原本以为 只要用尽万种风情 你在将来任何不和我在一起的时候 都会内心无法安宁 直到后...
    仨两小事阅读 276评论 0 4