apache + varnish 实现负载均衡

环境: redhat 6.5

机器及软件:
server1 172.25.12.1 varnish
server2 172.25.12.2 apache 还做了虚拟主机
server3 172.25.12.3 apache

varnish软件包:
varnish-3.0.5-1.el6.x86_64.rpm
varnish-libs-3.0.5-1.el6.x86_64.rpm

apache安装: yum install httpd -y

简介:

当大量的请求访问到达一台服务器后,容易造成服务器宕机,在服务器前加一台(批)缓存机器,将大量请求分散到各个缓存机器上,就能大大减少服务器工作量.
简而言之,varnish会将服务器中的一部分内容缓存到自己的缓存空间中,客户对服务器的访问会先到达varnish服务器,当varnish里有客户需要的东西,就直接给予回应,如果没有,varnish会向服务器请求这部分内容,缓存在自己身上,从而减少后台的访问量.
流程如下图:
这里写图片描述

varnish里策略都是写在vcl文件里: /etc/varnish/default.vcl
这个文件里写的都是vcl函数
这里写图片描述

处理过程大致分为如下几个步骤:
(1)Receive 状态,也就是请求处理的入口状态,根据 VCL 规则判断该请求应该是 Pass 或
Pipe,或者进入 Lookup(本地查询)。
(2)Lookup 状态,进入此状态后,会在 hash 表中查找数据,若找到,则进入 Hit 状态,否则进
入 miss 状态。
(3)Pass 状态,在此状态下,会进入后端请求,即进入 fetch 状态。
(4)Fetch 状态,在 Fetch 状态下,对请求进行后端的获取,发送请求,获得数据,并进行本地
的存储。
(5)Deliver 状态, 将获取到的数据发送给客户端,然后完成本次请求。

varnish的安装:

server1配置:

yum install -y varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm

更改配置文件:
vim /etc/sysconfig/varnish

66 VARNISH_LISTEN_PORT=80

这个是更改varnish的默认端口
因为http访问时默认访问80 端口 为了测试方便 就把varnish端口改为80端口
而且这次搭建服务 server1 上不安装 apache ,所以不用担心端口冲突

vim /etc/varnish/default.vcl

#配置一个后端服务器
7 backend default {
8   .host = "172.25.12.2";
9   .port = "80";
10 }
11 
#查看缓存命中情况
12 sub vcl_deliver {
13 if (obj.hits > 0) {
14 set resp.http.X-Cache = "HIT from varnish cache";
15 }
16 else {
17 set resp.http.X-Cache = "MISS from varnish cache";
18 }
19 return (deliver);
20 }

配置文件参数具体详解请点击传送门
http://blog.csdn.net/keda8997110/article/details/8777153

server2配置:

yum install httpd -y
/etc/init.d/httpd start
echo server2 > /var/www/html/index.html

测试varnish安装情况:

测试缓存命中:
curl -I 172.25.12.1

测试结果图:
这里写图片描述

通过 varnishadm 手动清除缓存

varnishadm ban.url .*$             #清除所有
varnishadm ban.url /index.html     #清除 index.html 页面缓存
varnishadm ban.url /admin/$        #清除 admin 目录缓存

负载均衡配置:

server1配置:

vim /etc/varnish/default.vcl

  7 backend web2 {
  8   .host = "172.25.12.2";
  9   .port = "80";
 10 }
 11 
 12 backend web3 {
 13   .host = "172.25.12.3";
 14   .port = "80";
 15 }
 16 
 #将多个后端聚合为一个组,进行轮询
 17 director lb round-robin {
 18 { .backend = web2; }
 19 { .backend = web3; }
 20 }
 21 
 22 sub vcl_recv {
 23 if (req.http.host ~ "^(www.)?test.org") {
 24 set req.http.host = "www.test.org";
 25 set req.backend = lb;
 26 return (pass);  ##为了轮询效果明显,不进行缓存,实际应用中肯定要进行缓存
 27 } elsif (req.http.host ~ "^bbs.test.org") {
 28 set req.backend = web2;
 29 } else {error 404 "varnish cache";
 30 }
 31 } 

server2配置:

vim /etc/httpd/conf/httpd.conf

 990 NameVirtualHost *:80

1011 <VirtualHost *:80>
1012         DocumentRoot /var/www/html
1013         ServerName   server2.example.com
1014 </VirtualHost>
1015 
1016 
1017 <VirtualHost *:80>
1018         DocumentRoot /web1
1019         ServerName www.test.org
1020 </VirtualHost>
1021 
1022 <VirtualHost *:80>
1023         DocumentRoot /web2
1024         ServerName bbs.test.org
1025 </VirtualHost>

mkdir /web{1..2}
echo www.test.org > /web1/index.html
echo bbs.test.org > /web2/index.html

测试虚拟主机:
curl www.test.org
curl bbs.test.org

server3配置:

yum install httpd -y
/etc/init.d/httpd start
echo server3 > /var/www/html/index.html

负载均衡测试:

打开浏览器
输入 www.test.org 或者 test.org f5刷新时就开始进行轮询
界面显示分别为:
www.test.org (server2上的apache虚拟主机)
server3 (server3的apache)

输入 bbs.test.org
界面显示:
bbs.test.org (seerver2上的虚拟主机)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值