实验环境:
主机名 | IP | 服务 |
---|---|---|
hetoto1 | 172.25.12.1 | varnish |
hetoto2 | 172.25.12.2 | apache |
hetoto3 | 172.25.12.3 | apache |
配置hetoto1:
- 部署varnish服务,可查看上一篇博文
https://blog.csdn.net/hetoto/article/details/90509750
配置hetoto2:
- 1.安装apache
[root@hetoto2 ~]# yum install httpd -y
- 2.编写前端文件
[root@hetoto2 ~]# cd /var/www/html
[root@hetoto2 html]# vim index.html
<h1>www.westos.org - hetoto2</h1>
/etc/init.d/httpd start ##开启httpd服务
配置hetoto3 :同上
[root@hetoto2 ~]# yum install httpd -y
[root@hetoto3 ~]# cd /var/www/html
[root@hetoto3 html]# vim index.html
<h1>bbs.westos.org - hetoto3 </h1>
一、配置多个后端服务的varnish
1.hetoto1:配置varnish的default文件
[root@server1 ~]# vim /etc/varnish/default.vcl
backend web1 { # 后端第一个服务器名web1
.host = "172.25.12.2"; #将hetoto2 www.westos.org作为第一个服务器主机
.port = "80";
}
backend web2 { # 后端第二个服务器名web2
.host = "172.25.12.3"; #将hetoto3 bbs.westos.org作为第二个服务器主机
.port = "80";
}
#当访问 www.westos.org 或 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 = web1;
}elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {
error 404 "westos cache";
}
}
2.重新加载配置文件
[root@hetoto1 ~]# /etc/init.d/varnish reload
3.测试:
添加解析
[root@foundation12 ~]# vim /etc/hosts
172.25.12.1 www.westos.org bbs.westos.org
物理机中的访问测试
[root@foundation12 ~]# curl www.westos.org ##也可加上-I参数,查看cdn缓存
<h1>www.westos.org - hetoto2</h1>
[root@foundation12 ~]# curl bbs.westos.org
<h1>bbs.westos.org - hetoto3</h1>
二、负载均衡
- 负载均衡(Load Balance,简称LB)是一种服务器或网络设备的集群技术。负载均衡将特定的业务(网络服务、网络流量等)分担给多个服务器或网络设备,从而提高了业务处理能力,保证了业务的高可用性。
- 负载均衡算法的种类有很多种,常见的负载均衡算法包括轮询法、随机法、源地址哈希法、加权轮询法、加权随机法、最小连接法等,应根据具体的使用场景选取对应的算法。
在varnish中我们采用的是轮询法,平衡对待每一个后端服务器。
- 配置/etc/varnish/default.vcl
backend web1 {
.host = "172.25.12.2";
.port = "80";
}
backend web2 {
.host = "172.25.12.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";
}
}
- 重新加载配置文件
[root@hetoto1 ~]# /etc/init.d/varnish reload
真机测试:
[root@foundation12 ~]# curl www.westos.org
<h1>www.westos.org - hetoto2</h1>
[root@foundation12 ~]# curl www.westos.org
<h1>bbs.westos.org - hetoto</h1>
[root@foundation12 ~]# curl www.westos.org
<h1>www.westos.org - hetoto2</h1>
[root@foundation12 ~]# curl www.westos.org
<h1>bbs.westos.org - hetoto3</h1>
三、一台主机多个解析(虚拟主机):
- 1.在server3的httpd的默认发布目录中建立两个虚拟主机
[root@hetoto3 ~]# vim /etc/httpd/conf/httpd.conf
990 NameVirtualHost *:80 ##注释打开
##在最后编辑:
<VirtualHost *:80>
DocumentRoot /www
ServerName www.westos.org
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /bbs
ServerName bbs.westos.org
</VirtualHost>
- 2.创建目录/www 和/bbs,并编辑相应的前端文件
[root@hetoto3 ~]# mkdir /www
[root@hetoto3 ~]# mkdir /bbs
[root@hetoto3 ~]# cd /www
[root@hetoto3 www]# vim index.html
<h1>www.westos.org - hetoto3</h1>
[root@hetoto3 www]# cd /bbs
[root@hetoto3 bbs]# vim index.html
<h1>bbs.westos.org - hetoto3</h1>
- 3.重启httpd服务
[root@hetoto3 bbs]# /etc/init.d/httpd restart
- 4.测试: 在主机添加解析
[root@foundation12 ~]# vim /etc/hosts
172.25.12.1 hetoto1
172.25.12.3 www.westos.org bbs.westos.org
- 物理机中的访问测试
[root@foundation12 ~]# curl www.westos.org
<h1>www.westos.org - hetoto3</h1>
[root@foundation12 ~]# curl bbs.westos.org
<h1>bbs.westos.org - hetoto3</h1>