一·Varinsh
Varnish是一款高性能的开源HTTP加速器 ,挪威最大的在线报纸 Verdens Gang
使用3台Varnish代替了原来的12台Squid 性能比以前更好。
Varnish 的作者Poul-Henning
Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多。在1975年时,储存媒介只有两种:内存与硬盘。但现在计算机系统的内存除了主存外,还包括了CPU内的L1、L2,甚至有L3快取。硬盘上也有自己的快取装置,因此Squid
Cache自行处理物件替换的架构不可能得知这些情况而做到最佳化,但操作系统可以得知这些情况,所以这部份的工作应该交给操作系统处理,这就是
Varnish cache设计架构
VCL 处理流程图
处理过程大致分为如下几个步骤:
(1)Receive 状态,也就是请求处理的入口状态,根据 VCL 规则判断该请求应该是 Pass 或 Pipe,或者进入Lookup(本地查询)。
(2)Lookup 状态,进入此状态后,会在 hash 表中查找数据,若找到,则进入 Hit 状态,否则进 入
(3)Pass 状态,在此状态下,会进入后端请求,即进入 fetch 状态。
(4)Fetch 状态,在 Fetch
(5)Deliver 状态,将获取到的数据发送给客户端,然后完成本次请求
二·封装虚拟机
1.建立母盘虚拟机
(1)安装镜像以及永久挂载
[root@foundation71 Desktop]# mkdir /var/www/html/source6.5
[root@foundation71 Desktop]# mv rhel-server-6.5-x86_64-dvd.iso /iso
[root@foundation71 Desktop]# cd /iso
[root@foundation71 iso]# ls
rhel-server-6.5-x86_64-dvd.iso
[root@foundation71 iso]# mount /iso/rhel-server-6.5-x86_64-dvd.iso /var/www/html/source6.5
[root@foundation71 iso]# df
[root@foundation71 iso]# vim /etc/rc.d/rc.local
[root@foundation71 iso]# chmod +x /etc/rc.d/rc.local
(2)安装母盘虚拟机
步骤如下:
之后进入界面需要手动选择:
(3)配置母盘
[root@localhost ~]# cd /etc/udev/rules.d/
[root@localhost rules.d]# ls
[root@localhost rules.d]# rm -f 70-pe ##删除
[root@localhost rules.d]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
[root@localhost network-scripts]# vi ifcfg-eth0 ##修改网络
[root@localhost yum.~]# /etc/init.d/network restart ##重启网络
[root@localhost network-scripts]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
[root@localhost yum.repos.d]# vi rhel-source.repo ##修改yum源
[root@localhost yum.repos.d]# yum repolist
[root@localhost yum.repos.d]# yum install vim lftp -y ##安装
[root@localhost yum.repos.d]# cd
[root@localhost yum.~]# hostname server1 ##更改hostname
[root@localhost yum.~]# vim /etc/hosts ## 在解析文件中添加子盘虚拟机ip和hostname
[root@localhost yum.~]# chkconfig iptables off ##关闭防火墙(以物理形式)
[root@localhost yum.~]# /etc/init.d/iptables stop ##关闭防火墙(以在企业6的形式)
[root@localhost yum.~]# vim /etc/selinux/config ##更改selinux
修改网络:
配置yum源:
添加解析文件:
更改selinux:
注意:
当yum repolist刷新不了时,需要在物理机上执行关闭火墙、打开httpd、重启httpd
在做实验时刷新不成功:
由于镜像挂载的问题,所以刷新失败:
修改后刷新成功:
建立好母盘不能再次打开,防止自己打开,可以删除母盘(不删除磁盘,只删除图标)
(4)建立子盘虚拟机test1和test2
三·CDN节点中的Varnish
CDN
CDN的全称是Content Delivery Network,即内容分发网络。其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快、更稳定。通过在网络各处放置节点服务器所构成的在现有的互联网基础之上的一层智能虚拟网络,CDN系统能够实时地根据网络流量和各节点的连接、负载状况以及到用户的距离和响应时间等综合信息将用户的请求重新导向离用户最近的服务节点上。其目的是使用户可就近取得所需内容,解决 Internet网络拥挤的状况,提高用户访问网站的响应速度。
1.配置后端服务器
(1)在server1Varnish端中:
将在物理机上把两个varnish的安装包传到server1中
[root@server1 ~]# ls
[root@server1 ~]# yum install *.rpm
安装包: varnish-3.0.5-1.el6.x86_64.rpm
varnish-libs-3.0.5-1.el6.x86_64.rpm
[root@server1 ~]# cd /etc/varnish/
[root@server1 varnish]# ls
[root@server1 varnish]# vim /etc/sysconfig/varnish ##修改程序端口
VARNISH_LISTEN_PORT=80 ##修改
[root@server1 varnish]# vim /etc/security/limits.coif ##限制
## 添加内容
(内核 操作系统 程序)
varnish - nofile 131073
varnish - memlosk 82000
varnish - nproc unlimited
[root@server1 varnish]# vim default.vcl ##配置后端服务器
[root@server1 varnish]# /etc/init.d/varnish start ##打开varnish
[root@server1 varnish]# /etc/init.d/varnish reload ##刷新
[root@server1 varnish]# /etc/init.d/varnish restart ##重启
在/etc/sysconfig/varnish文件:
在/etc/security/limits.coif文件中:
在default.vcl文件中:
(2)在server2后端中
[root@server2 ~]# cd /var/www/html
[root@server2 html]# ls
[root@server2 html]# vim index.html
[root@server2 html]# cat index.html
www.westos.org
[root@server2 html]# /etc/init.d/iptables stop
[root@server2 html]# chkconfig iptables off
[root@server2 html]# cat index.html
[root@server2 html]# /etc/init.d/httpd restart
(3)在物理机中需要添加解析文件
[root@foundation71 images]# vim /etc/hosts ##解析文件
172.25.71.1 www.westos.org ##添加内容
(4)在网页中测试:
2.查看缓存命中情况
(1)在server1Varnish端中:
[root@server1 varnish]# vim default.vcl
[root@server1 varnish]# /etc/init.d/varnish restart
在配置文件中添加内容:
(2)物理机上进行测试:
3.定义多个不同域名站点的后端服务器
需要在建立一个server3后端
(1)在server3后端中:
[root@server3 ~]# yum install httpd -y
[root@server3 ~]# /etc/init.d/httpd start
[root@server3 ~]# cd /var/www/html
[root@server3 html]# vim index.html
[root@server3 html]# cat index.html
bbs.westos.org
[root@server3 html]# /etc/init.d/httpd start
(2)在server1Varnish中:
[root@server1 varnish]# vim default.vcl
[root@server1 varnish]# /etc/init.d/varnish reload
[root@server1 varnish]# /etc/init.d/varnish restart
在配置文件中:
(3)在物理机需要添加解析文件
[root@foundation71 images]# vim /etc/hosts ##添加解析文件
172.25.71.1 www.westos.org bbs.westos.org
(4)在物理机测试:
4.定义负载均衡
(1)在server3后端中:
[root@server3 html]# vim /etc/httpd/conf/httpd.conf
## 打开 NameVirtualHost *:80
##在最后编辑内容
[root@server3 html]# mkdir /www1
[root@server3 html]# cd /www1
[root@server3 www1]# ls
[root@server3 www1]# vim index.html
[root@server3 www1]# cat index.html
www.westos.org---server3
[root@server3 www1]# /etc/init.d/httpd restart
在http配置文件中
编辑内容:
(2)在server2后端中修改配置文件
[root@server2 html]# vim index.html
[root@server2 html]# cat index.html
www.westos.org----server2 ##为了方便实验可以分辨清楚
[root@server2 html]# /etc/init.d/httpd restart
(3)在server1Varnish端中
[root@server1 varnish]# vim default.vcl ##在配置文加添加内容
[root@server1 varnish]# /etc/init.d/varnish restart
(4)在物理机中测试
(5)负载均衡
在server1Varnish端中:
[root@server1 varnish]# vim default.vcl
## 添加return (pass);
[root@server1 varnish]# /etc/init.d/varnish restart
在物理机中测试
5.varnish cdn 推送平台
安装包:
bansys.zip
(1)在物理机,两个服务后端都需要开启服务
[root@foundation41 kiosk]# systemctl start httpd
[root@foundation41 kiosk]# systemctl stop firewalld
[root@server2 ~]# /etc/init.d/httpd start
[root@server3 ~]# /etc/init.d/httpd start
[root@server1 etc]# /etc/init.d/varnish start
(2)在server1Varnish端
[root@server1 etc]# yum install -y httpd
[root@server1 etc]# yum install -y php
[root@server1 etc]# vim /etc/httpd/conf/httpd.conf ## 把端口改为8080
[root@server1 etc]# /etc/init.d/httpd start
[root@server1 etc]# cd
[root@server1 ~]# ls
[root@server1 ~]# cd /home/
[root@server1 home]# ls
[root@server1 home]# yum install unzip -y ##安装解压软件
[root@server1 home]# unzip bansys.zip -d /var/www/html
[root@server1 home]# cd /var/www/html
[root@server1 html]# ls
[root@server1 html]# cd bansys/
[root@server1 bansys]# ls
[root@server1 bansys]# mv * .. ##移动到当前
[root@server1 bansys]# ls
[root@server1 bansys]# cd ..
[root@server1 html]# ls
[root@server1 html]# vim config.php ##修改配置文件
[root@server1 html]# /etc/init.d/httpd start ##开启服务
[root@server1 html]# netstat -antlp ##查看端口
[root@server1 html]# cd /etc/varnish/
[root@server1 varnish]# ls
[root@server1 varnish]# vim default.vcl ##编辑配置文件
[root@server1 varnish]# /etc/init.d/varnish reload ##刷新
[root@server1 varnish]# /etc/init.d/varnish restart ##重启
在网页中分别输入172.25.71.1:8080和www.westos.org
在/etc/httpd/conf/httpd.conf文件中修改端口
在config.php文件中修改内容
在default.vcl配置文件中修改内容
在网页中输入172.25.71.1:8080和www.westos.org
(3)CDN推送管理
每推送一次,在物理机上测试就会跳转一次