4321oiuy 发表于 2016-9-18 10:54:33

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
# yum install httpd -y
# vim /var/www/html/index.html
                         web1's page   
# /etc/init.d/httpd start
   1.2服务器端2

# yum install httpd -y # vim /var/www/html/index.html                         web2's page    # /etc/init.d/httpd start

2.代理端(缓存端)安装配置varnish
# ls
      varnish-3.0.5-1.el6.x86_64.rpmvarnish-libs-3.0.5-1.el6.x86_64.rpm
# yum install * -y
# vim /etc/sysconfig/varnis
       VARNISH_LISTEN_PORT=80                ##设定varnish的端口为80
# vim /etc/varnish/default.vcl
      backend web1 {
         .host = "172.25.254.10";      ##指定apache所在主机ip(本次实验服务器1/2端都可以)
          .port = "80";                ##apache端口
      }
# /etc/init.d/varnish start
      Starting Varnish Cache:                                    
# 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);
    }
# 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 缓存端,访问缓存不过期页面无法刷新
# curl http://172.25.254.8   
web1's page
# 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
# 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 目录缓存
# curl -Ihttp://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
# varnishadm ban.url /index.html
# curl -Ihttp://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设置
# 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>
# mkdir -p /www/willis.com/html
# mkdir -p /www/linux.com/html
# echo "web1 willis's page ">/www/willis.com/html/index.html
# echo "web1 linux's page ">/www/linux.com/html/index.html
# /etc/init.d/httpd restart
    4.2 服务端2设置

# 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># mkdir -p /www/willis.com/html# mkdir -p /www/linux.com/html# echo "web2 willis's page ">/www/willis.com/html/index.html# echo "web2 linux's page ">/www/linux.com/html/index.html# /etc/init.d/httpd restart    4.3访问端测试
# vim /etc/hosts
172.25.254.8      www.willis.com
172.25.254.8   www.linux.com
###注意,前面指定 .host = "172.25.254.10",所以访问结果为服务器1端的网页内容




5.网页重写
# 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";
      }
}
# /etc/init.d/varnish restart
    访问端测试
   访问 willis.com自动跳转成www.willis.com
   访问 linux.com自动跳转成www.linux.com



6.负载均衡器

已经提前配好好两台服务器,本实验只需配置缓存端
# 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网页重写,刷新可看到内容的变化


刷新:



刷新:



页: [1]
查看完整版本: Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群