cdn网络搭建

CDN分发网络

  1. CDN的全称是Content Delivery Network,即内容分发网络。其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快、更稳定。通过在网络各处放置节点服务器所构成的在现有的互联网基础之上的一层智能虚拟网络,CDN系统能够实时地根据网络流量和各节点的连接、负载状况以及到用户的距离和响应时间等综合信息将用户的请求重新导向离用户最近的服务节点上。其目的是使用户可就近取得所需内容,解决 Internet网络拥挤的状况,提高用户访问网站的响应速度。

VCL 处理流程图

cdn网络搭建

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

当只有一个后端服务器

  1. server1

[[email protected] ~]# ls
varnish-libs-3.0.5-1.el6.x86_64.rpm
varnish-3.0.5-1.el6.x86_64.rpm
[[email protected] ~]# yum install -y varnish-* jemalloc-3.6.0-1.el7.x86_64.rpm
[[email protected] ~]# rpm -qc varnish-3.0.5-1.el6.x86_64 # 查看varnish的配置文件
/etc/logrotate.d/varnish # varnish的日志文件
/etc/sysconfig/varnish # varnish的命令参数配置文件
/etc/varnish/default.vcl # varnish的程序主配置文件
[[email protected] ~]# vim /etc/sysconfig/varnish
NFILES=131072 # 最多打开文件的个数
MEMLOCK=82000 # 最大内存锁定大小
NPROCS=“unlimited” # 进程数,unlimited表示不做限制
[[email protected] ~]# sysctl -a | grep file # 查看系统可以打开文件的个数,发现远小于varnish的最大个数
fs.file-nr = 448 0 98864
fs.file-max = 98864
cdn网络搭建

为了和varnish软件匹配,需要修改文件个数,而文件个数是通过内存大小来控制的,所以我们需要通过加大内存来增加文件个数
cdn网络搭建

  • sysctl -a | grep file # 再次查看,发现可以打开文件的个数可以满足varnish的最大需求

cdn网络搭建

  • ulimit -l # 查看系统允许的进程数,远小于varnish允许数,但是我们不能修改系统的,只能来修改varnish的
    64
  • vim /etc/security/limits.conf # 为了和varnish匹配,修改系统对varnish的默认参数和varnish本身保持一致
    varnish - nofile 131072
    varnish - memlock 82000
    varnish - nproc unlimited
    cdn网络搭建
  • /etc/init.d/varnish start # 开启varnish服务
  • netstat -antlp | grep varnish # 查看varnish的端口
    cdn网络搭建
  • ps aux | grep varnish # 开启varnish服务之后,会自动开启两个进程

cdn网络搭建

  • vim /etc/varnish/default.vcl # 配置后端服务器
    backend default {
    .host = “172.25.78.2”;
    .port = “80”;
  • vim /etc/sysconfig/varnish # 修改varnish的端口,方便连接后端服务器
    VARNISH_LISTEN_PORT=80
  1. server2

[[email protected] ~]# yum install -y httpd
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# vim index.html
www.westos.org —server2
[[email protected] html]# /etc/init.d/httpd start

  1. 在真机测试当访问调度器的时候,会自动跳转到后端服务器上
    [[email protected] ~]# curl 172.25.78.1 # 当访问172.25.78.1时,自动调转到172.25.78.2这台服务器上
    www.westos.org —server2
    cdn网络搭建

server1上查看缓存命中情况

[[email protected] ~]# vim /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);
}

cdn网络搭建

  • varnishadm ban.url .*$ # 清除所有的缓存
  • varnishadm ban.url /index.html # 清除index.html的缓存
  • /etc/init.d/varnish reload # 重新加载服务

真机测试,查看缓存命中情况

  • curl -I 172.25.78.1
    cdn网络搭建
  • curl -I 172.25.78.1

cdn网络搭建

把不同的服务定义到不同的后端服务器上

  • 当访问www.westos.org 就会从web1(172.25.78.2)中取数据缓存到cdn中
  • 当访问bbs.westos.org 就会从web2(172.25.78.3)中取数据缓存到cdn中
  1. 在server1上

[[email protected] ~]# vim /etc/varnish/default.vcl
backend web1 {
.host = “172.25.78.2”;
.port = “80”;
}
backend web2 {
.host = “172.25.78.3”;
.port = “80”;
}
sub vcl_recv {
if (req.http.host ~ “^(www.)?westos.org”) { # 当访问信息以www.开头时,跳转到web1服务器上
set req.http.host = “www.westos.org”;
set req.backend = web1;
return (pass); #为了测试方便,不进行缓存
} elsif (req.http.host ~ “^bbs.westos.org”) { # 当访问信息以bbs开头时,跳转到web2服务器上
set req.backend = web2;
} else {
error 404 “westos cache”; # 其他报错 }
}
}

cdn网络搭建

  1. 在web2服务器上配置

[[email protected] ~]# yum install -y httpd
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# vim index.html
bbs.westos.org — server3
[[email protected] html]# /etc/init.d/httpd start

  1. 真机测试
  • 先写解析(可以通过访问域名来访问服务)
    • vim /etc/hosts
      172.25.78.1 server1 www.westos.org bbs.westos.org
  • 开始测试
    • curl www.westos.org
      www.westos.org —server2
    • bbs.westos.org
      bbs.westos.org — server3

负载均衡

把一个文件发布到两个服务器上面,从而缓解后台服务器的压力,提高数据的稳定性,这
种情况多用于当访问量过大时负载均衡到两个服务器上,即使有一个服务器坏掉了,还有一个服
务器来顶替他的工作。varnish还是可以从终端的服务器中取出客户端访问的数据缓存起来。

  1. 在server1上

[[email protected] ~]# vim /etc/varnish/default.vcl # 把多个后端聚合为一个组,并检测后端健康状况
director lb round-robin {
{.backend = web1;}
{.backend = web2;}
}# 当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 web2 取数据,访问其他页面报错。
sub vcl_recv {
if (req.http.host ~ “^(www.)?westos.org”) {
set req.http.host = “www.westos.org”;
set req.backend = lb;
return (pass); #为了测试方便,不进行缓存,pass生效表示不在cdn中取缓存据,直接在后台取数据,这样的话,了在测试中更好的看到实验效果,实际中不需要添加.实验中当清空了缓存数据,有一端的后台服务器挂掉之后,另一个服务器会顶替他,如果挂掉的服务器数据还在有效期,还是可以在cdn中方问到
} elsif (req.http.host ~ “^bbs.westos.org”) {
set req.backend = web2;
} else {
error 404 “westos cache”;
}
}

cdn网络搭建
测试(实现了负载均衡)

cdn网络搭建
当我们在cdn上把从服务器上缓存清空时,如果有一个服务器坏掉了我们依然能够访问到但此时只有从开启的服务器中缓存数据

一台主机上定义多个域名服务(虚拟主机的搭建)

在server3上

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80 # 打开虚拟主机的端口
<VirtualHost *:80>
DocumentRoot /www S
erverName www.westos.org

<VirtualHost *:80>
DocumentRoot /bbs
ServerName bbs.westos.org

cdn网络搭建

[[email protected] ~]# mkdir /www
[[email protected] ~]# mkdir /bbs
[[email protected] ~]# vim /www/index.html # 写测试页面
www.westos.org – server3
[[email protected] ~]# vim /bbs/index.html # 写测试页面
bbs.westos.org – server3
[[email protected] ~]# /etc/init.d/httpd restart

测试

  • 写解析

    • vim /etc/hosts
      172.25.78.3 server3 bbs.westos.org www.westos.org
  • 真机测试

    • curl www.westos.org
      • www.westos.org – server3
    • curl bbs.westos.org
      • bbs.westos.org – server3

varnish cdn 推送平台(web界面清除缓存)

  1. 在 server1 上

[[email protected] ~]# yum install -y unzip httpd php # 如果下不了,就重启一下真机的httpd
[[email protected] ~]# ls
bansys.zip
[[email protected] ~]# unzip bansys.zip -d /var/www/html # 解压到/var/www/html 下
[[email protected] ~]# cd /var/www/html
[[email protected] html]# ls
bansys
[[email protected] html]# cd bansys/
[[email protected] bansys]# ls
class_socket.php config.php index.php purge_action.php static
[[email protected] bansys]# vim config.php
//varnish主机列表
//可定义多个主机列表
$var_group1 = array(
‘host’ => array(‘172.25.78.1’),
‘port’=>‘8080’,
);
//varnish群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
‘www.westos.org’ => $var_group1,
);
//varnish版本
//2.x和3.x推送命令不一样
$VAR_VERSION = “3”;
cdn网络搭建

[[email protected] bansys]# vim /etc/varnish/default.vcl # 设置访问控制列表,在列表中的主机才能进行推送操作
acl westos {
“127.0.0.1”;
“172.25.78.0”/24
}
sub vcl_recv {
if (req.request == “BAN”) { #BAN为设置缓存
if (!client.ip ~ westos) { #westos为访问控制列表
error 405 “Not allowed.”;
}
ban("req.url ~ " + req.url);
error 200 “ban added”;
}
}
backend web1 {
.host = “172.25.78.2”;
.port = “80”;
}
backend web2 {
.host = “172.25.78.3”;
.port = “80”;
}
director 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;
#return (pass); # 注释掉此行的目的是为了进行缓存
} elsif (req.http.host ~ “^bbs.westos.org”) {
set req.backend = web2;
} else {
error 404 “westos cache”;
}
}
sub vcl_recv {
if (req.http.host ~ “^(www.)?westos.org”) {
set req.http.host = “www.westos.org”;
set req.backend = web1;
#return (pass); #为了测试方便,不进行缓存
} elsif (req.http.host ~ “^bbs.westos.org”) {
set req.backend = web2;
} else {
error 404 “westos cache”;
}
}
cdn网络搭建
cdn网络搭建
cdn网络搭建

[[email protected] bansys]# /etc/init.d/varnish reload
[[email protected] bansys]# vim /etc/httpd/conf/httpd.conf # 修改httpd端口,因为80端口被varnish占用了,所以把httpd的端口改为8080
Listen 8080
[[email protected] bansys]# mv * … # 把bansys所有的配置文件移动到html里
[[email protected] bansys]# /etc/init.d/httpd restart

  1. 真机测试
  • 修改解析
    • vim /etc/hosts
    • 172.25.78.1 server1 bbs.westos.org www.westos.org

cdn网络搭建

  • 返回到shell测试(查看命中情况)
  • curl -I www.westos.org # 推送成功之后,再次查看,没有缓存命中,说明成功

cdn网络搭建