码迷,mamicode.com
首页 > 其他好文 > 详细

基于haproxy+keepalive+varnish实现lnmp企业级架构

时间:2017-12-10 18:22:19      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:负载均衡高可用

一、实验准备

服务器A:haproxy服务器(主)

服务器B:haproxy服务器(备)

服务器C:LNMP服务器、varnish服务器

服务器D:LNMP服务器、varnish服务器

二、实验要求

前端负载均衡器实现keepalived高可用。并且实现对于图片和静态资源的请求,代理到后端webserver缓存varnish服务上,对于动态请求。直接代理到厚点web服务,后端健康检测基于/index.html(手动创建),监测连续三次监测通过,视为OK,连续5次监测失败,视为fall。

三、实验步骤

1、前端负载均衡器实现VIP的漂移

keepalived(主)
global_defs?{
???notification_email?{
root@localhost???
}
???notification_email_from?root@magedu.com
???smtp_server?127.0.0.1
???smtp_connect_timeout?30
???router_id?zuozuo_LVS
}
vrrp_instance?VI_1?{
????state?MASTER?????//
????interface?eth1
????virtual_router_id?70
????priority?100????//优先级
????advert_int?1
????authentication?{
????????auth_type?PASS
????????auth_pass?zuozuo
????}
????virtual_ipaddress?{
172.17.110.70/16????//VIP
????}
}
keepalived(备)
global_defs?{
???notification_email?{
	root@localhost???
}
???notification_email_from?root@magedu.com
???smtp_server?127.0.0.1
???smtp_connect_timeout?30
???router_id?zuozuo_LVS
}

vrrp_instance?VI_1?{
????state?BACKUP
????interface?eth1
????virtual_router_id?70
????priority?90
????advert_int?1
????authentication?{
????????auth_type?PASS
????????auth_pass?zuozuo
????}
????virtual_ipaddress?{
	172.17.110.70/16????//VIP
????
????}
}

2、前端负载均衡器实现请求的分离

global
????#?to?have?these?messages?end?up?in?/var/log/haproxy.log?you?will
????#?need?to:
????#
????#?1)?configure?syslog?to?accept?network?log?events.??This?is?done
????#????by?adding?the?‘-r‘?option?to?the?SYSLOGD_OPTIONS?in
????#????/etc/sysconfig/syslog
????#
????#?2)?configure?local2?events?to?go?to?the?/var/log/haproxy.log
????#???file.?A?line?like?the?following?can?be?added?to
????#???/etc/sysconfig/syslog
????#
????#????local2.*???????????????????????/var/log/haproxy.log
????#
?log?????????127.0.0.1?local2
????chroot??????/var/lib/haproxy
????pidfile?????/var/run/haproxy.pid
????maxconn?????40000
????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

listen?stats???//检测页面
	mode?http
	bind?0.0.0.0:1080
	stats?enable
	stats?hide-version
	stats?uri?/haproxyadmin
	stats?auth?admin:admin???//登录检测
	stats?admin?if?TRUE
#---------------------------------------------------------------------
#?main?frontend?which?proxys?to?the?backends
#---------------------------------------------------------------------
frontend??static
????mode?http
????bind?*:6000???//标明端口
????acl?url_static??path_beg????-i?/static?/images?/javascript?/stylesheets
????acl?url_static??path_end???????-i?.jpg?.gif?.png?.css?.js

????use_backend?static??????????if?url_static
?
?frontend?server
	bind??*:80
	default_backend??server
#---------------------------------------------------------------------
#?static?backend?for?serving?up?images,?stylesheets?and?such
#---------------------------------------------------------------------
backend?static
????balance?????roundrobin??
????option?httpchk?GET?/index.html
????server??????static1??172.17.252.63:6000?check
????server??????static2??172.17.253.59:6000?check
#---------------------------------------------------------------------
#?round?robin?balancing?between?the?various?backends
#---------------------------------------------------------------------
backend??server
????balance?????roundrobin
????option?httpchk?GET?/index.html
????server??server1?172.17.252.63:80?check?inter?3000??rise?3?fall?5
????server??server2?172.17.253.59:80?check?inter?3000??rise?3?fall?5

3、varnish实现缓存策略

vcl?4.0;?//必须要写,注明版本
import?directors;
probe?check1?{??//制定健康检测策略
????????.url?=?"/index.html";
????????.timeout=1s;
????????.interval=2s;
????????.window=5;
????????.threshold=3;
??????}
backend?server1?{
.host?=?"172.17.252.63";???//lnmp服务器
.port?=?"80";
.probe?=?check1;
}
backend?server2?{
????????.host?=?"172.17.253.59";
????????.port?=?"80";
????????.probe?=?check1;
}
sub?vcl_init?{???//初始化
????new?web_cluster?=?directors.random();
????web_cluster.add_backend(server1,10);
????web_cluster.add_backend(server2,20);
}

?acl??purgers?{????#?定义可访问来源IP
?????????"127.0.0.1";
?????????"172.17.0.0"/16;
?}

sub?vcl_recv?{
	?if?(req.method?==?"GET"?&&?req.http.cookie)?{??
????????return(hash);
}
?if?(req.method?!=?"GET"?&&
???req.method?!=?"HEAD"?&&
???req.method?!=?"PUT"?&&
???req.method?!=?"POST"?&&
???req.method?!=?"TRACE"?&&
???req.method?!=?"OPTIONS"?&&
???req.method?!=?"PURGE"?&&
???req.method?!=?"DELETE")?{
????return?(pipe);
???}
}?
sub?vcl_hash?{??//对访问的URL进行hash
?????hash_data(req.url);
}
sub?vcl_backend_response?{?#?自定义缓存文件的缓存时长,即TTL值
????if?(bereq.url?~?"\.(jpg|jpeg|gif|png)$")?{
????????set?beresp.ttl?=?3600s;??
????}
????if?(bereq.url?~?"\.(html|css|js)$")?{
????????set?beresp.ttl?=?7200s;
????}
?????set?beresp.grace?=?30m;

????????return(deliver);
}
sub?vcl_deliver?{
????if?(obj.hits?>?0)?{????#?为响应添加X-Cache首部,显示缓存是否命中
????????set?resp.http.X-Cache?=?"HIT?from?"?+?server.ip;
????}?else?{
????????set?resp.http.X-Cache?=?"MISS";
????}
????????unset?resp.http.X-Powered-By;
????????unset?resp.http.Via;
}



??





基于haproxy+keepalive+varnish实现lnmp企业级架构

标签:负载均衡高可用

原文地址:http://blog.51cto.com/13136984/2049155

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!