本文包括三个部分:
- 实验环境
- 配置
- 验证
- 小结
一、实验环境:
CentOS7系统
反向代理服务器Nginx1:172.16.80.100
反向代理服务器Nginx2:172.16.80.101
Nginx1和Nginx2上通过keepalived实现高可用,使用的vip为172.16.80.200静态服务器Nginx3:172.16.80.102
静态服务器Nginx4:172.16.80.103
Nginx3和Nginx4上通过keepalived实现高可用,使用的vip为172.16.80.201
当用户访问172.16.80.200的web服务80端口时,Nginx1或者Nginx2会把请求反向代理到varnish服务器,varnish把动态请求发送到后端的tomcat服务器,而静态请求发送到Nginx3和Nginx4。
二、配置
1、主机172.16.80.100配置
keepalived服务
[root@centos7a ~]#cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keadmin@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id CentOS7A.luo.com
vrrp_mcast_group4 224.0.0.22
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 15
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass hahahaha
}
virtual_ipaddress {
172.16.80.200 <==设置虚拟IP为172.16.80.200
}
}
nginx服务
[root@centos7a ~]#cat /etc/nginx/nginx.conf |grep -v ^[[:space:]]*#|grep -v ^$
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www.server.pools{
server 172.16.80.200:6081; <== 反向代理到172.16.80.200的varnish
}
server {
listen 80;
server_name www.nginx.com;
location / {
proxy_pass http://www.server.pools;
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
varnish服务:
[root@centos7a ~]#cat /etc/varnish/default.vcl |grep -v ^[[:space:]]*#|grep -v ^$
vcl 4.0;
backend nginx {
.host = "172.16.80.201";
.port = "80";
}
backend tomcat {
.host = "172.16.80.201";
.port = "8080";
}
sub vcl_recv {
if (req.url ~ "(?i)\.html$") { <==如果请求的内容为.html文件
set req.backend_hint = nginx; <==就交给nginx处理
} else { <==否则(正常来说,应该判断请求是否php、jsp等文件,这里仅作简单测试,就不判断了。)
set req.backend_hint = tomcat; <==就交给tomcat处理
}
}
sub vcl_backend_response {
}
sub vcl_deliver {
}
2、主机172.16.80.101配置
keepalived服务:
[root@centos7b ~]#cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keadmin@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id CentOS7B.luo.com
vrrp_mcast_group4 224.0.0.22
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 15
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass hahahaha
}
virtual_ipaddress {
172.16.80.200
}
}
nginx服务:
[root@centos7b ~]#cat /etc/nginx/nginx.conf |grep -v ^[[:space:]]*#|grep -v ^$
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www.server.pools{
server 172.16.80.200:6081;
}
server {
listen 80;
server_name www.nginx.com;
location / {
proxy_pass http://www.server.pools;
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
varnish服务:
[root@centos7b ~]#cat /etc/varnish/default.vcl |grep -v ^[[:space:]]*#|grep -v ^$
vcl 4.0;
backend nginx {
.host = "172.16.80.201";
.port = "80";
}
backend tomcat {
.host = "172.16.80.201";
.port = "8080";
}
sub vcl_recv {
if (req.url ~ "(?i)\.html$") {
set req.backend_hint = nginx;
} else {
set req.backend_hint = tomcat;
}
}
sub vcl_backend_response {
}
sub vcl_deliver {
}
3、主机172.16.80.102配置
tomcat服务
安装后启动即可
keepalived服务
和172.16.80.100类似,只要把vip改成172.16.80.201即可
nginx服务
[root@centos7c ~]#cat /etc/nginx/nginx.conf |grep -v ^[[:space:]]*#|grep -v ^$
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.static.com;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@centos7c ~]#cat /usr/share/nginx/html/index.html
Centos7C
static
4、主机172.16.80.103配置
tomcat服务
安装后启动即可
keepalived服务:
和172.16.80.101类似,只要把vip改成172.16.80.201即可
nginx服务:
[root@centos7d ~]#cat /etc/nginx/nginx.conf |grep -v ^[[:space:]]*#|grep -v ^$
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.static.com;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@centos7d ~]#cat /usr/share/nginx/html/index.html
Centos7D
static
三、测试
启动所有服务
由于keepalived配置中,主机172.16.80.100配置的优先级比较高,nginx1会拿到vip172.16.80.200。
同样的主机172.16.80.102也会拿到vip172.16.80.201。
1、验证动静分离:
当用户的请求访问html资源时
当用户请求访问非html资源时:
2、验证高可用
关掉172.16.80.100主机(或者关掉keepalived服务),以此模仿nginx1宕机。
vip172.16.80.200成功漂移到172.16.80.201主机。
客户端依然能正常访问172.16.80.200。
同样的,关掉172.16.80.102上的keepalived服务
此时就由172.16.80.103提供服务了
四、小结
基本上实现了nginx反向代理、keepalived高可用、varnish动静分离这几个功能。而varnish的缓存效果和tomcat会话保持没有讲到,keepalived的双实例双主模式也没有用到,后面有时间再补充一下。