实验环境(Centos7)
主机名 IP 版本
varnish 192.168.14.210 4.0.5
web1(nginx) 192.168.14.211 nginx/1.12.2
web2(nginx) 192.168.14.213 nginx/1.12.2
client 192.168.14.212
注意:本篇文章是在上一篇文章基础上添加新的功能
一、varnish:192.168.14.210
在第一篇的基础上添加了director、负载均衡调度算法
1、添加多台后端主机
backend websrv1 {
.host = "192.168.14.211"; #后端主机IP地址
.port = "80"; #后端主机监听的端口
}
backend websrv2 {
.host = "192.168.14.213";
.port = "80";
}
2、导入directors,并把多个directors聚合成一个组,当组里一个 backend 挂掉后,可以选择另一个健康的 backend。(也就实现了后端主机负载均衡)
import directors;
sub vcl_init {
new websrv = directors.round_robin(); #设置主机组的调度算法,有两种,另一种为random
websrv.add_backend(websrv1); #将后端主机加入到组中
websrv.add_backend(websrv2); #将后端主机加入到组中
}
varnish 提供多种算法,比如循环、随机、hash路由、client路由、dns等方式
(1))round-robin 采用循环的方式依次选择backend
(2)random 根据所设置的权重(weight)来选择 backend
(3)client 根据请求的客户端属性(IP、cookie、session)来选择 backend
(4) hash 根据hash表来选择
(5))dns 根据dns解析来选择
3、根据正则表达式条件,选择后端主机组(directors会实现负载均衡)
sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
set req.backend_hint = websrv.backend();
if (req.url ~ "(?i)\.jpg$") {
set req.backend_hint = websrv.backend();
}
}
这里有个未解决的问题,懂得请指教:(不用配置)
上面的根据正则表达式匹配结尾是jpg,负载均衡可以生效,web1故障了会自动跳转到web2。但是根据正则表达式匹配结尾是html,再引用后端组,负载均衡却不生效,如下:
sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
if (req.url ~ "(?i)\.html$") {
set req.backend_hint = websrv.backend();
}
}
4、重新加载配置文件
[root@varnish ~]# varnish_reload_vcl
Loading vcl from /etc/varnish/default.vcl
Current running config name is
Using new config name reload_2018-01-24T05:33:07
VCL compiled.
VCL 'reload_2018-01-24T05:33:07' now active
available 3 boot
active 0 reload_2018-01-24T05:33:07
Done
二、web1:192.168.14.211
1、yum安装nginx,修改web页面(具体查看第一篇)
[root@web1 ~]# cat /usr/share/nginx/html/index.html
nginx webserver1
2、创建存放图片目录,并存放一张图片
[root@web1 ~]# mkdir /usr/share/nginx/html/images/
[root@web1 ~]# cd /usr/share/nginx/html/images/
[root@web1 images]# cp /usr/share/backgrounds/day.jpg .
3、重启nginx
[root@web1 ~]# systemctl restart nginx
三、web2:192.168.14.213
1、关闭防火墙和selinux
[root@web2 ~]# systemctl stop firewalld
[root@web2 ~]# systemctl disable firewalld
[root@web2 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
2、配置阿里epel源
[root@web2 ~]# vim /etc/yum.repos.d/epel.repo
[epel]
name=aliyun epel
baseurl=http://mirrors.aliyun.com/epel/7Server/x86_64/
gpgcheck=0
3、安装nginx
[root@web2 ~]# yum install -y nginx
4、修改自定义主页
[root@web2 ~]# echo "nginx webserver2" > /usr/share/nginx/html/index.html
[root@web2 ~]# cat /usr/share/nginx/html/index.html
nginx webserver2
5、创建存放图片目录,并存放一张图片
[root@web2 ~]# mkdir /usr/share/nginx/html/images/
[root@web2 ~]# cd /usr/share/nginx/html/images/
[root@web2 images]# cp /usr/share/backgrounds/day.jpg .
6、启动服务
[root@web2 ~]# systemctl start nginx
[root@web2 ~]# systemctl enable nginx
四、客户端:192.168.14.212
1、添加域名解析(可选),后面可以直接使用server代替IP 192.168.14.210
[root@client ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.14.210 server
2、为了实验结果,varnish先清理所有缓存
[root@varnish ~]# varnishadm ban req.url "~" /
3、client访问*jpg过程
1)首先访问到后端web1的数据
2)然后访问到的是varnish缓存的数据。
3)关闭web1服务,手动模拟后端故障。清理varnish缓存,再次访问就是web2的数据,达到了负载均衡效果
4)最后访问的是varnish缓存的数据
jpg负载均衡效果(点击大图显示)
3、上面提到的未解决的问题(可选查看)
1)关闭web1服务,客户端访问显示503,无法自动跳转到web2
[root@client ~]# curl -I http://server
HTTP/1.1 503 Backend fetch failed
Date: Tue, 23 Jan 2018 22:38:19 GMT
Server: Varnish
Content-Type: text/html; charset=utf-8
Retry-After: 5
X-Varnish: 98691
Age: 0
Via: 1.1 varnish-v4
X-Cache: Miss via 192.168.14.210
Content-Length: 282
Connection: keep-alive
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/95275.html