简介: Varnish 是一款高性能且开源的反向代理服务器和 HTTP 加速器,其采用全新的软件体系机构,和现在的硬件体系紧密配合,与传统的 squid 相比,varnish 具有性能更高、速度更快、管理更加方便等诸多优点;
此次试验的目的是让Nginx做前端访问负载均衡,varnish代理后端的web服务器,并缓存结果
1.准备机器,做好时间同步,主机名解析
192.168.42.150 node1 [nginx 负载均衡] 192.168.42.151 node2 [varnish1] 192.168.42.152 node3 [varnish2] 192.168.42.153 node4 [web1 httpd] 192.168.42.154 node5 [web2 httpd]
2.这次我们也是从后面的节点做起吧
node5:
(1).安装httpd
yum install httpd -y
(2).创建网站目录
mkdir /application/test -pv chown -R apache.apache /application/test cd /application/test echo "this is test 2 page." >index.html
(3).配置虚拟主机
cd /etc/httpd/conf.d
vim test.conf
<VirtualHost *:80>
ServerName test.varnish.com
DocumentRoot "/application/test"
<Directory "/application/test">
Options None
AllowOverride None
Require all granted
</Directory>
CustomLog "logs/test.varnish.com_access_log" combined
</VirtualHost>
(4).启动httpd,并测试一下子
systemctl start httpd [root@node5 /]# curl 127.0.0.1 this is test 2 page.
node4同上,只需修改 /application/test/index.html
echo "this is test 1 page." >index.html
启动并测试
3.node3,node2 安装varnish
node3:
(1).安装varnish
yum install varnish -y
(2).varnish服务端配置
cd /etc/varnish/ cp default.vcl default.vcl.back [root@node3 varnish]# ls default.vcl default.vcl.back secret varnish.params [root@node3 varnish]# vim varnish.params VARNISH_LISTEN_PORT=80 #VARNISH_STORAGE="malloc,256M" VARNISH_STORAGE="file,/data/varnish/cache,1G"
(3).创建缓存目录
mkdir /data/varnish/cache -pv chown -R varnish.varnish /data/varnish/cache
(4).启动varnish
systemctl start varnish [root@node3 varnish]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 1024 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 10 127.0.0.1:6082 *:* LISTEN 0 1024 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*
此时我们去浏览器访问是出错的,因此我们需要配置default.vcl,指向后端的web
Error 503 Backend fetch failed Backend fetch failed Guru Meditation: XID: 32796 Varnish cache server
(5).配置default.vcl,先配置一台测试一把
vim default.vcl
backend default {
.host = "192.168.42.153";
.port = "80";
}
(6).重新加载配置问件
varnish_reload_vcl Loading vcl from /etc/varnish/default.vcl Current running config name is Using new config name reload_2017-06-27T09:15:28 VCL compiled. VCL 'reload_2017-06-27T09:15:28' now active available 0 boot active 0 reload_2017-06-27T09:15:28 Done
(7).去浏览器刷新一把,ok
(8).因为我们的后端web是有多台的,因此我们需要把vcl配置成,多台调度 相关常用配置如下:
#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.
# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;
import directors;
#健康状态检查公共变量
probe www_probe {
.url = "/index.html";
.interval = 1s;
.timeout = 1s;
.window = 8;
.threshold = 5;
}
#web1
backend websrv1 {
.host = "192.168.42.153";
.port = "80";
.probe = www_probe;
}
#web2
backend websrv2 {
.host = "192.168.42.154";
.port = "80";
.probe = www_probe;
}
#初始化
sub vcl_init {
new websrvs= directors.round_robin();
websrvs.add_backend(websrv1);
websrvs.add_backend(websrv2);
}
#允许清除缓存的ip
acl purgers{
"127.0.0.1";
"192.168.42.0"/24;
}
sub vcl_recv {
#调用轮询
set req.backend_hint = websrvs.backend();
#对某类资源的请求不检查缓存
if (req.url ~ "(?i)^/(login|admin)") {
return(pass);
}
#将客户端的主机推送给后端的web,写入后端web日志中
if (req.restarts == 0) {
if (req.http.X-Fowarded-For) {
set req.http.X-Forwarded-For = req.http.X-Forwarded-For + "," + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
}
#如果是PURGE就清除缓存
if (req.method == "PURGE") {
if (!client.ip ~ purgers) {
return(synth(405,"Purging not allowed for " + client.ip));
}
return(purge);
}
}
#对于特定类型的资源,例如公开的图片等,取消其私有标识,
#并强行设定其可以由varnish缓存的时长
sub vcl_backend_response {
if (beresp.http.cache-control !~ "s-maxage") {
if (bereq.url ~ "(?i)\.(jpg|jpeg|png|gif|css|js)$") {
unset beresp.http.Set-Cookie;
set beresp.ttl = 3600s;
}
}
}
#自定义显示缓存是否命中
sub vcl_deliver {
if (obj.hits>0) {
set resp.http.X-Cache = "HIT via " + server.ip;
} else {
set resp.http.X-Cache = "MISS via " + server.ip;
}
}
(10).配置完以后,把配置文件推送到node2节点上
scp default.vcl varnish.params 192.168.42.151:/etc/varnish/
在node2节点上操作:创建缓存目录
mkdir /data/varnish/cache -pv chown -R varnish.varnish /data/varnish/cache
启动varnish并查看一下子
[root@node2 varnish]# systemctl start varnish [root@node2 varnish]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 1024 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 10 127.0.0.1:6082 *:* LISTEN 0 1024 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*
(11).在node4,node5上多添加几个虚拟主机
虚拟主机配置如下:
[root@node4 test1]# cat /etc/httpd/conf.d/test.conf
<VirtualHost *:80>
ServerName test.varlish.com
DocumentRoot "/application/test"
<Directory "/application/test">
Options None
AllowOverride None
Require all granted
</Directory>
CustomLog "logs/iounix_access_log" combined
</VirtualHost>
<VirtualHost *:80>
ServerName test.varlish1.com
DocumentRoot "/application/test1"
<Directory "/application/test1">
Options None
AllowOverride None
Require all granted
</Directory>
CustomLog "logs/iounix_access1_log" combined
</VirtualHost>
<VirtualHost *:80>
ServerName test.varlish2.com
DocumentRoot "/application/test2"
<Directory "/application/test2">
Options None
AllowOverride None
Require all granted
</Directory>
CustomLog "logs/iounix_access2_log" combined
</VirtualHost>
<VirtualHost *:80>
ServerName test.varlish3.com
DocumentRoot "/application/test3"
<Directory "/application/test3">
Options None
AllowOverride None
Require all granted
</Directory>
CustomLog "logs/iounix_access3_log" combined
</VirtualHost>
<VirtualHost *:80>
ServerName test.varlish4.com
DocumentRoot "/application/test4"
<Directory "/application/test4">
Options None
AllowOverride None
Require all granted
</Directory>
CustomLog "logs/iounix_access4_log" combined
</VirtualHost>
网站目录如下:
[root@node4 application]# ll /application/ total 0 drwxr-xr-x 2 apache apache 24 Jun 27 10:03 test drwxr-xr-x 2 apache apache 24 Jun 27 15:21 test1 drwxr-xr-x 2 apache apache 24 Jun 27 15:17 test2 drwxr-xr-x 2 apache apache 24 Jun 27 15:17 test3 drwxr-xr-x 2 apache apache 24 Jun 27 15:18 test4
文件结构如下:
[root@node4 application]# tree /application/
/application/
├── test
│ └── index.html
├── test1
│ └── index.html
├── test2
│ └── index.html
├── test3
│ └── index.html
└── test4
└── index.html
每个index.html的内容都需要不一样哦
hosts文件域名解析如下:
192.168.42.153 test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com
访问一下子:
curl test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com this is test 1 page. test1 page test2 page test3 page test4 page
以上是我在node4上的操作,而node5只需要把node4的配置文件和网站目录推送过去稍微改一下即可
scp /etc/hosts 192.168.42.154:/etc/hosts cd /application scp -rp test1 test2 test3 test4 192.168.42.154:/application/ scp /etc/httpd/conf.d/test.conf 192.168.42.154:/etc/httpd/conf.d/
然后去node5操作:
chown -R apache.apache /application/* echo "node5 11111111111111111" > /application/test1/index.html echo "node5 22222222222222222" > /application/test2/index.html echo "node5 33333333333333333" > /application/test3/index.html echo "node5 44444444444444444" > /application/test4/index.html vim /etc/hosts 192.168.42.154 test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com systemctl restart httpd [root@node5 application]# curl test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com this is test 2 page. node5 11111111111111111 node5 22222222222222222 node5 33333333333333333 node5 44444444444444444
现在我们在node4,node5上都弄了5个虚拟主机,并且访问正常,接下来就是让varnish能够正常访问那个5个虚拟主机,其实很简单,直接在varnish机器上 hosts文件添加对应的ip
node3:
vim /etc/hosts 192.168.42.152 test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com
测试一把: (1).每次访问需要清除缓存
curl -X PURGE test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com
(2).访问
curl test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com
ok,可以负载均衡
node2:
vim /etc/hosts 192.168.42.151 test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com
测试一把: (1).每次访问需要清除缓存
curl -X PURGE test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com
(2).访问
curl test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com
ok,也是可以负载均衡
4.node1安装nginx 负载均衡
(1).安装nginx
yum install nginx -y
(2).配置nginx 因为我们的后端做了5个域名的虚拟主机,因此我们的负载均衡器也需要做5个一样的虚拟主机
hosts解析:
vim /etc/hosts 192.168.42.150 test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com
upsteam配置:
vim /etc/nginx/nginx.conf
upstream testsrvs {
server 192.168.42.151:80;
server 192.168.42.152:80;
least_conn;
}
虚拟主机配置:
vim /etc/nginx/conf.d/test.conf
server {
listen 80;
server_name test.varlish.com;
location / {
proxy_pass http://testsrvs;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
add_header X-Via $server_addr;
add_header X-Accel $server_name;
}
}
server {
listen 80;
server_name test.varlish1.com;
location / {
proxy_pass http://testsrvs;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
add_header X-Via $server_addr;
add_header X-Accel $server_name;
}
}
server {
listen 80;
server_name test.varlish2.com;
location / {
proxy_pass http://testsrvs;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
add_header X-Via $server_addr;
add_header X-Accel $server_name;
}
}
server {
listen 80;
server_name test.varlish3.com;
location / {
proxy_pass http://testsrvs;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
add_header X-Via $server_addr;
add_header X-Accel $server_name;
}
}
server {
listen 80;
server_name test.varlish4.com;
location / {
proxy_pass http://testsrvs;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
add_header X-Via $server_addr;
add_header X-Accel $server_name;
}
}
测试一把: 为了能够测出负载均衡的效果,我们先去node2,node3的两个varnish两个节点清除缓存
(1).清除缓存
curl -X PURGE test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com
然后在node1中访问:
第一次:
[root@node1 nginx]# curl test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com this is test 1 page. test1 page node5 22222222222222222 node5 33333333333333333 test4 page
第二次:
[root@node1 nginx]# curl test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com this is test 1 page. node5 11111111111111111 node5 22222222222222222 test3 page test4 page
第三次:
[root@node1 nginx]# curl test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com this is test 2 page. node5 11111111111111111 test2 page test3 page node5 44444444444444444
第四次:
[root@node1 nginx]# curl test.varlish.com test.varlish1.com test.varlish2.com test.varlish3.com test.varlish4.com this is test 2 page. test1 page test2 page node5 33333333333333333 node5 44444444444444444
好了,我们经过四次的访问得到不同的结果,说明负载均衡OK了
5.压测 ab -n 10000 -c 1000 http://test.varlish.com/
原创文章,作者:srayban,如若转载,请注明出处:http://www.178linux.com/78690

