cdn

1.虚拟机配置

1.建立母镜像

virt-manager
cd /etc/udev/rules.d/
rm -rf 70-* ##防止子快照虚拟机的eth0网卡无法使用

配置yum仓库
配置ip地址
安装vim
hostname server1
vim /etc/sysconfig/network 改hostname
vim /etc/hosts 配本地网关
chkconfig iptables off 关闭防火墙
vim /etc/sysconfig/selinux selinux改为disabled
cd /etc/ssh/
rm -rf ssh_host_*
真机上 virt-sysprep -d rhel6.5.qcow2 清除缓存

2.建立子镜像

真机上 virt-sysprep -d rhel6.5.qcow2 清除缓存
qemu-img create -f qcow2 -b rhel6.5.qcow2 host1
qemu-img create -f qcow2 -b rhel6.5.qcow2 host2
qemu-img create -f qcow2 -b rhel6.5.qcow2 host3
qemu-img create -f qcow2 -b rhel6.5.qcow2 host4

建立四个虚拟机
virt-manager 导入虚拟机
最好删除原rhel6.5(因为如果子镜像同时开启会出bug)
虚拟机配置ip yum仓库(6.x版本有很多仓库,需配全)

vim /etc/yum.repos.d/rhel-source.repo

[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.66.100/rhel6.5
enabled=1
gpgcheck=0

[HighAvailability]
name=HighAvailability
baseurl=http://172.25.66.100/rhel6.5/HighAvailability
gpgcheck=0

[LoadBalancer]
name=LoadBalancer
baseurl=http://172.25.66.100/rhel6.5/LoadBalancer
gpgcheck=0

[ScalableFileSystem]
name=ScalableFileSystem
baseurl=http://172.25.66.100/rhel6.5/ScalableFileSystem
gpgcheck=0

[ResilientStorage]
name=ResilientStorage
baseurl=http://172.25.66.100/rhel6.5/ResilientStorage
gpgcheck=0

实验环境
三台服务器,一台作为 director,两台作为 real server,director 有一个外网网卡(172.25.254.103) 和一个内网ip(172.25.3.1),两个 real server 上只有内网 ip (172.25.3.2) 和 (172.25.3.3),并且需要把两个 real server 的内网网关设置为 director 的内网 ip(172.25.3.1)

2.varnish

Varnish是一款高性能的开源HTTP加速器,用于CDN加速
server1 安装
varnish-3.0.5-1.el6.x86_64.rpm
varnish-libs-3.0.5-1.el6.x86_64.rpm
yum install varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm -y

1.配置文件位置

vim /etc/sysconfig/varnish
其中最大连接数受内核,系统控制

/etc/varnish/default.vcl

sysctl -a | grep file-max ##显示内核最大支持的缓存文件数371487

查看系统打开文件数量
ulimit -n ##系统最大支持缓存文件数1024
其limit的配置文件位置
/etc/security/limits.conf

/etc/pam.d/ 这里边放的是认证文件

/lib64/security/ ##这里边是一些认证模块

/etc/security/ ##这里边是一些认证配置文件

vim /etc/security/limits.conf
--->>最后一行
varnish     -   nofile    65535    ##使用者varnish(/etc/passwd有个非交互式用户varnish   - 软上限和硬上限两种类型   最大文件数 )

vim /etc/sysconfig/varnish ##因为测试http服务所以设置80端口

这里写图片描述

2.启用varnish

vim /etc/varnish/default.vcl ##设置端口号80和后端服务器的ip

这里写图片描述

***server1
/etc/init.d/varnish start
chkconfig varnish on

***server2,server3
yum install httpd -y
echo server2 > /var/www/html/index.html
/etc/init.d/httpd start
chkconfig httpd on

测试:
本地解析中添加vip
curl 172.25.66.1

3.查看缓存命中情况

/etc/varnish/default.vcl

sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}

这里写图片描述

测试:
curl 172.25.66.1
当缓存命中时,执行HIT from westos cache 当缓存不命中时,执行MISS from westos cache

使用 varnishadm ban.url .* 用来清空所有的缓存
使用 varnishadm ban.url .*$
使用 varnishadm ban.url /index.html 用来清空html 缓存

4.域名访问

sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {  此处用于当前面不管是什么域名当后面为westos.org 时,都会缓存到同一个www.westos.org的目录
set req.http.host = "www.westos.org";
set req.backend = web1;
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {error 404 "westos cache";  此处不能显示为IP地址访问,因为其匹配的是域名,而不是IP地址
}
}

这里写图片描述

测试:
添加本地解析
curl www.westos.org
curl westos.org
curl bbs.westos.org

5.轮询

director lb round-robin {   lb  其组的名称   round-robin 为轮询机制
{ .backend=web1; }
{ .backend=web2; }
}

sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;   此为当遇到此域名时,直接交给lb组,进行轮询
return (pass);  表示不缓存,直接访问服务器
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {error 404 "westos cache";  如果都不成立,则直接保404的错误.
}
}

这里写图片描述

测试:
curl www.westos.org

6.varnish cdn 推送平台

安装php插件
将 bansys.zip解压到apache默认发布目录中unzip bansys.zip -d /var/www/html/ 把解压后目录中的所有文件移动到html下
这里写图片描述

bansys 有两种工作模式,分别是:telnet 和 http 模式。
telnet 模式需要关闭 varnish 服务管理端口的验证,注释掉/etc/sysconfig/varnish 文件中的 “ -S ${VARNISH_SECRET_FILE}”这行,重启 varnish 服务即可。如果是 http 模式需要对 varnish 做以下设置: 

这里写图片描述
这里写图片描述
这里写图片描述

测试:
访问172.25.66.1:8080

7.工作原理

这里写图片描述

这里写图片描述

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

cdn原理

基于上述实验说明:1充当了cdn的作用,当客户端51访问172.25.254.1的时候,cdn1主机接收到请求,从cdn中varnish换存中查找,如果有直接返回给客户端,1如果没有cdn去从隔壁cdn站点获取,有返回客户端,如果没有直接去服务源站获取,换存到cdn本地,在返回给客户端:

这里写图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值