本文包括三個部分:
- 實(shí)驗(yàn)環(huán)境
- 配置
- 驗(yàn)證
- 小結(jié)
一、實(shí)驗(yàn)環(huán)境:
CentOS7系統(tǒng)
反向代理服務(wù)器Nginx1:172.16.80.100
反向代理服務(wù)器Nginx2:172.16.80.101
Nginx1和Nginx2上通過keepalived實(shí)現(xiàn)高可用痕慢,使用的vip為172.16.80.200靜態(tài)服務(wù)器Nginx3:172.16.80.102
靜態(tài)服務(wù)器Nginx4:172.16.80.103
Nginx3和Nginx4上通過keepalived實(shí)現(xiàn)高可用尚揣,使用的vip為172.16.80.201
當(dāng)用戶訪問172.16.80.200的web服務(wù)80端口時,Nginx1或者Nginx2會把請求反向代理到varnish服務(wù)器掖举,varnish把動態(tài)請求發(fā)送到后端的tomcat服務(wù)器快骗,而靜態(tài)請求發(fā)送到Nginx3和Nginx4。
二塔次、配置
1方篮、主機(jī)172.16.80.100配置
keepalived服務(wù)
[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 <==設(shè)置虛擬IP為172.16.80.200
}
}
nginx服務(wù)
[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服務(wù):
[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$") { <==如果請求的內(nèi)容為.html文件
set req.backend_hint = nginx; <==就交給nginx處理
} else { <==否則(正常來說,應(yīng)該判斷請求是否php励负、jsp等文件藕溅,這里僅作簡單測試,就不判斷了熄守。)
set req.backend_hint = tomcat; <==就交給tomcat處理
}
}
sub vcl_backend_response {
}
sub vcl_deliver {
}
2蜈垮、主機(jī)172.16.80.101配置
keepalived服務(wù):
[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服務(wù):
[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服務(wù):
[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、主機(jī)172.16.80.102配置
tomcat服務(wù)
安裝后啟動即可
keepalived服務(wù)
和172.16.80.100類似裕照,只要把vip改成172.16.80.201即可
nginx服務(wù)
[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攒发、主機(jī)172.16.80.103配置
tomcat服務(wù)
安裝后啟動即可
keepalived服務(wù):
和172.16.80.101類似,只要把vip改成172.16.80.201即可
nginx服務(wù):
[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
三晋南、測試
啟動所有服務(wù)
由于keepalived配置中惠猿,主機(jī)172.16.80.100配置的優(yōu)先級比較高,nginx1會拿到vip172.16.80.200负间。
同樣的主機(jī)172.16.80.102也會拿到vip172.16.80.201偶妖。
1姜凄、驗(yàn)證動靜分離:
當(dāng)用戶的請求訪問html資源時
當(dāng)用戶請求訪問非html資源時:
2、驗(yàn)證高可用
關(guān)掉172.16.80.100主機(jī)(或者關(guān)掉keepalived服務(wù))趾访,以此模仿nginx1宕機(jī)态秧。
vip172.16.80.200成功漂移到172.16.80.201主機(jī)。
客戶端依然能正常訪問172.16.80.200扼鞋。
同樣的申鱼,關(guān)掉172.16.80.102上的keepalived服務(wù)
此時就由172.16.80.103提供服務(wù)了
四、小結(jié)
基本上實(shí)現(xiàn)了nginx反向代理云头、keepalived高可用捐友、varnish動靜分離這幾個功能。而varnish的緩存效果和tomcat會話保持沒有講到溃槐,keepalived的雙實(shí)例雙主模式也沒有用到匣砖,后面有時間再補(bǔ)充一下。