Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群相关的知识,希望对你有一定的参考价值。

实验环境:RHEL6.5

                实验环境4台

                真机进行访问测试     willis.example.com         172.25.254.6

                虚拟机1(缓存端)   varnish.example.com       172.25.254.8

                虚拟机2(服务器端1) web1.example.com     172.25.254.10

                虚拟机3(服务器端2) web2.example.com     172.25.254.20

                两台服务器主要用于负载均衡实验。

实验内容:1.服务器端安装Apache

                 2.代理端(缓存端)安装配置varnish

                 3.缓存无法同步问题

                 4.虚拟主机

                 5.网页重写

                 6.负载均衡器

安装包:varnish-3.0.5-1.el6.x86_64.rpm

             varnish-libs-3.0.5-1.el6.x86_64.rpm


1.服务器端安装Apache

    1.1 服务器端1

[[email protected] ~]# yum install httpd -y 

[[email protected] ~]# vim /var/www/html/index.html

                         web1‘s page    

[[email protected] ~]# /etc/init.d/httpd start

   1.2服务器端2

[[email protected] ~]# yum install httpd -y 

[[email protected] ~]# vim /var/www/html/index.html

                         web2‘s page    

[[email protected] ~]# /etc/init.d/httpd start


2.代理端(缓存端)安装配置varnish

[[email protected] mnt]# ls

        varnish-3.0.5-1.el6.x86_64.rpm  varnish-libs-3.0.5-1.el6.x86_64.rpm

[[email protected] mnt]# yum install * -y

[[email protected] mnt]# vim /etc/sysconfig/varnis

       VARNISH_LISTEN_PORT=80 ##设定varnish的端口为80

[[email protected] mnt]# vim /etc/varnish/default.vcl 

        backend web1 {

         .host = "172.25.254.10";      ##指定apache所在主机ip(本次实验服务器1/2端都可以)

          .port = "80";                ##apache端口

        }

[[email protected] mnt]# /etc/init.d/varnish start

        Starting Varnish Cache:                                    [  OK  ]

[[email protected] mnt]# vim /etc/varnish/default.vcl   ##设置缓存命中信息

sub vcl_deliver{

    if(obj.hits>0){

        set resp.http.X-Cache="HIT from willis cache";  ##缓存命中

        }

     else{

        set resp.http.X-Cache="MISS from willis cache";  ##缓存未命中

        }

    return(deliver);

    }

[[email protected] mnt]# service varnish reload

Loading vcl from /etc/varnish/default.vcl

Current running config name is boot

Using new config name reload_2016-09-15T05:19:54

VCL compiled.

available       0 boot

active          0 reload_2016-09-15T05:19:54

Done


3.缓存无法同步问题

    3.1 缓存端,访问缓存不过期页面无法刷新

[[email protected] ~]# curl http://172.25.254.8     

web1‘s page

[[email protected] ~]# curl -I http://172.25.254.8

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Wed, 14 Sep 2016 21:02:59 GMT

ETag: "1fcb4-c-53c7e0dd4191f"

Content-Type: text/html; charset=UTF-8

Content-Length: 12

Accept-Ranges: bytes

Date: Wed, 14 Sep 2016 21:43:54 GMT

X-Varnish: 1470288177 1470288175

Age: 95

Via: 1.1 varnish

Connection: keep-alive

X-Cache: HIT from willis cache

[[email protected] ~]# curl -I http://172.25.254.8

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Wed, 14 Sep 2016 21:02:59 GMT

ETag: "1fcb4-c-53c7e0dd4191f"

Content-Type: text/html; charset=UTF-8

Content-Length: 12

Accept-Ranges: bytes

Date: Wed, 14 Sep 2016 21:43:59 GMT

X-Varnish: 1470288178 1470288175

Age: 100

Via: 1.1 varnish

Connection: keep-alive

X-Cache: HIT from willis cache


 3.2 通过 varnishadm 手动清除缓存

# varnishadm ban.url .*$   #清除所有

# varnishadm ban.url /index.html  #清除 index.html 页面缓存

# varnishadm ban.url /admin/$ #清除 admin 目录缓存

[[email protected] mnt]# curl -I  http://172.25.254.8

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Wed, 14 Sep 2016 21:02:59 GMT

ETag: "1fcb4-c-53c7e0dd4191f"

Content-Type: text/html; charset=UTF-8

Content-Length: 12

Accept-Ranges: bytes

Date: Wed, 14 Sep 2016 21:52:42 GMT

X-Varnish: 1470288192 1470288185

Age: 51

Via: 1.1 varnish

Connection: keep-alive

X-Cache: HIT from willis cache

[[email protected] mnt]# varnishadm ban.url /index.html

[[email protected] mnt]# curl -I  http://172.25.254.8

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Wed, 14 Sep 2016 21:02:59 GMT

ETag: "1fcb4-c-53c7e0dd4191f"

Content-Type: text/html; charset=UTF-8

Content-Length: 12

Accept-Ranges: bytes

Date: Wed, 14 Sep 2016 21:53:24 GMT

X-Varnish: 1470288193 1470288185

Age: 93

Via: 1.1 varnish

Connection: keep-alive

X-Cache: HIT from willis cache


4.虚拟主机

    4.1服务端1设置

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf 

<VirtualHost *:80>

    ServerAdmin /www/willis.com/html

    DocumentRoot www.willis.com

</VirtualHost>

<VirtualHost *:80>

    ServerAdmin /www/linux.com/html

    DocumentRoot www.linux.com

</VirtualHost>

[[email protected] ~]# mkdir -p /www/willis.com/html

[[email protected] ~]# mkdir -p /www/linux.com/html

[[email protected] ~]# echo "web1 willis‘s page ">/www/willis.com/html/index.html

[[email protected] ~]# echo "web1 linux‘s page ">/www/linux.com/html/index.html

[[email protected] ~]# /etc/init.d/httpd restart

    4.2 服务端2设置

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf 

<VirtualHost *:80>

    ServerAdmin /www/willis.com/html

    DocumentRoot www.willis.com

</VirtualHost>

<VirtualHost *:80>

    ServerAdmin /www/linux.com/html

    DocumentRoot www.linux.com

</VirtualHost>

[[email protected] ~]# mkdir -p /www/willis.com/html

[[email protected] ~]# mkdir -p /www/linux.com/html

[[email protected] ~]# echo "web2 willis‘s page ">/www/willis.com/html/index.html

[[email protected] ~]# echo "web2 linux‘s page ">/www/linux.com/html/index.html

[[email protected] ~]# /etc/init.d/httpd restart

    4.3访问端测试

[[email protected] ~]# vim /etc/hosts

172.25.254.8      www.willis.com

172.25.254.8     www.linux.com

  ###注意,前面指定 .host = "172.25.254.10",所以访问结果为服务器1端的网页内容  


技术分享

技术分享



5.网页重写

[[email protected] mnt]# vim /etc/varnish/default.vcl 

backend web1 {

  .host = "172.25.254.10";

  .port = "80";

}

backend web2 {

   .host="172.25.254.20";

   .port="80";

}

sub vcl_recv { ##网页缓存       

   if (req.http.host ~ "^(www.)?willis.com" ) {      ##访问中是否带www

        set req.http.host = "www.willis.com";        ##都定向到www上

        set req.backend = web1;                  ##访问web1

        }

        elsif (req.http.host ~ "^(www.)?linux.com" ) {

        set req.http.host = "www.linux.com";

        set req.backend = web1;

        }

        else {error 404 "westos cache";

        }

}

[[email protected] mnt]# /etc/init.d/varnish restart

    访问端测试

   访问 willis.com自动跳转成www.willis.com

   访问 linux.com自动跳转成www.linux.com


技术分享

技术分享



6.负载均衡器

已经提前配好好两台服务器,本实验只需配置缓存端

[[email protected] mnt]# vim /etc/varnish/default.vcl 

backend web1 {

  .host = "172.25.254.10";

  .port = "80";

}

backend web2 {

   .host="172.25.254.20";

   .port="80";

}

director willislb round-robin {

        { .backend = web1; }

        { .backend = web2; }

}

sub vcl_recv {

        if (req.http.host ~ "^(www.)?westos.com" ) {

        set req.http.host = "www.westos.com";

        set req.backend = willislb;                 ##修改

       return (pass);                                ##方便测试,不缓存

        }

        elsif (req.http.host ~ "^(www.)?linux.com" ) {

        set req.http.host = "www.linux.com";

        set req.backend = willislb;                 ##修改

      return (pass);                                 ##方便测试,不缓存

        }

        else {error 404 "westos cache";

        }

}


访问端测试:

linux.com或者willis.com  网页重写,刷新可看到内容的变化


技术分享

刷新:

技术分享


技术分享

刷新:

技术分享




本文出自 “技术人生,简单不简单” 博客,请务必保留此出处http://willis.blog.51cto.com/11907152/1852967

以上是关于Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群的主要内容,如果未能解决你的问题,请参考以下文章

Linux 缓存服务varnish

Varnish缓存机制详细介绍及简单配置

HTTP加速器——VARNISH

Varnish是一款高性能的开源HTTP加速器

高性能HTTP加速器varnish实践

高性能HTTP加速器Varnish安装与配置(包含常见错误)