设为首页 收藏本站
查看: 1694|回复: 0

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

[复制链接]

尚未签到

发表于 2017-11-23 11:24:23 | 显示全部楼层 |阅读模式
一、实验准备

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

三、实验步骤
1、前端负载均衡器实现VIP的漂移
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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、前端负载均衡器实现请求的分离
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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实现缓存策略
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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;
}







运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-409819-1-1.html 上篇帖子: LAMP架构介绍、MySQL,MariaDB介绍、MySQL安装 下篇帖子: 基于LNMT的session持久机制的多种方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表