该系列文章只是本人的学习笔记,文章中的文字描述提取自《Linux鸟哥私房菜》《Linux运维之道》等书中的重点内容,部分内容是在培训Linux运维时总结的笔记,化繁为简能够在工作中快速复习掌握重点,并不代表个人立场,但转载请加出处,并注明参考文献。
Varnish 是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang使用3台Varnish代替了原来的12台Squid,性能比以前更好.
Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多.在1975年时,储存媒介只有两种:内存与硬盘.但现在计算机系统的内存除了主存外,还包括了CPU内的L1、L2,甚至有L3快取.硬盘上也有自己的快取装置,因此Squid Cache自行处理物件替换的架构不可能得知这些情况而做到最佳化,但操作系统可以得知这些情况,所以这部份的工作应该交给操作系统处理,这就是 Varnish cache设计架构.
◆编译安装Varnish◆
1.安装依赖包
#yum install -y libtool ncurses-devel pcre-devel libxslt libedit python-imaging python-docutils
yum install -y pcre-devel python-docutils libedit-dev*
2.编译安装Varnish
wget http://varnish-cache.org/_downloads/varnish-6.0.0.tgz
tar -xzvf varnish-6.0.0.tgz
cd varnish-6.0.0/
./configure --prefix=/usr/local/varnish6
make && make install
ln -s /usr/local/varnish6/sbin/* /usr/sbin/
ln -s /usr/local/varnish6/bin/* /usr/local/bin/
cp -a /usr/local/varnish6/share/doc/varnish/example.vcl /usr/local/varnish6/default.vcl
◆Varnish实现负载均衡并实现页面缓存◆
1.编辑Varnish主配置文件,在相应的区域追加写入以下标★语句
vim /usr/local/varnish/default.vcl
15 # Default backend definition. Set this to point to your content server.
16 backend default {
17 .host = "127.0.0.1";
18 .port = "8080";
19 }
20
★ backend web1 { #均衡web主机1
★ .host="192.168.1.13";
★ .port="80"; #指定端口
★ .probe = { #开启健康检查
★ .url = "/"; #请求的URL路径
★ .interval = 5s; #查询间隔时间
★ .timeout = 1s; #超时时间
★ .window = 5; #滑动窗
★ .threshold = 3; #上次检查.window数量的多少
★ }
★ }
★ backend web2 {
★ .host="192.168.1.14"; #均衡web主机2
★ .port="80"; #指定端口
★ .probe = { #开启健康检查
★ .url = "/"; #请求的URL路径
★ .interval = 5s; #查询间隔时间
★ .timeout = 1s; #超时时间
★ .window = 5; #滑动窗
★ .threshold = 3; #上次检查.window数量的多少,
★ }
★ }
★
★ import directors; #加载directors模块
★
★ sub vcl_init { #缓存及加速-03单-高性能缓存服务器
★
★ new bar = directors.round_robin();
★ bar.add_backend(web1);
★ bar.add_backend(web2);
★
★ }
★
★ sub vcl_recv {
★
★ set req.backend_hint = bar.backend(); #指定backend
★
★ }
2.检查配置文件,并启动Varnish
varnishd -C -f /usr/local/varnish6/default.vcl #检查语法
varnishd -f /usr/local/varnish6/default.vcl #启动
pkill varnishd #关闭Varnish
varnishlog #查看Varnish日志
netstat -anpt | grep varnishd #检查是否启动
3.验证环节
elinks http://127.0.0.1/ #varnish服务器会根据算法分配流量