nginx解决tomcat高可用的思路,是前面加一层负载服务nginx。这种做法,总会有一个前端负载层存在宕机可能,是死循环

nginx 学习-nginx高可用(keepalived)_重启

keepalived来解决。

lvs 思想来解决

 

nginx 学习-nginx高可用(keepalived)_重启_02

keepalived的思路,由 2台服务器软件虚拟出来一台 虚拟网关vip,

此vip由两台机器共同协商生成。当有一台机器宕机时,另一台机器一样能维持vip。这保证了,只要两台机器不同时宕机,vip就存在

c、keepalived的安装(不能用docker,docker中无法正常工作)

下载地址:https://pan.baidu.com/s/1G7sLL-YkZGSMu8G76yz1Rw 密码:adbw。

对应centos6系统

keepalived安装步骤:

  A〉./configure --prefix=/data/program/keepalived --sysconf=/etc   ##因为keepalive启动时候会默认读取/etc/keepalived/keepalived.conf

  B〉make && make install

d、keepalived的配置:

打开/etc/keepalived/keepalived.conf,只需要配置如下一段。(其它是多余配置,删除)

! Configuration File for keepalived

global_defs {
router_id LVS_1
}
vrrp_script chk_http_port {
script "/usr/local/src/chk_nginx_pid.sh" #心跳执行的脚本
interval 2 #(检测脚本执行的间隔,单位是秒)
weight 2
}

vrrp_instance VI_1 {
state MASTER
interface eth0 #系统网卡
virtual_router_id 51 #主备两机器一致
priority 100 #值大的机器,胜出
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #(调用检测脚本)
}
virtual_ipaddress { #可虚拟多个ip
192.168.244.200
}
}

 

nginx 学习-nginx高可用(keepalived)_nginx_03

重启

nginx 学习-nginx高可用(keepalived)_nginx_04

ip addr命令查看网卡

nginx 学习-nginx高可用(keepalived)_重启_05

eth0的网卡上,出现两个ip,244.3是真实ip,244.200是虚拟vip

f、重复c、d、e步骤,再装一台机器。主备两台机器的配置,只有两个配置差异:

state BACKUP            # 指定keepalived的角色,MASTER为主,BACKUP为备

priority 100             # 优先级,数值越大,处理请求的优先级越高。备用数字小些

! Configuration File for keepalived

global_defs {
router_id LVS_2
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.244.200
}
}

g、测试主备keepalived

正常情况下,vip是在主机器一端的,备用机器244.4上不会出现vip。如下图上半部分。

此时,如果我们杀掉主机器上keepalived,备用机器上会出现vip。如下图下半部分。

nginx 学习-nginx高可用(keepalived)_重启_06

h、主机器keepalived再启动后,备机中vip = 200消失了,如下图

nginx 学习-nginx高可用(keepalived)_优先级_07

j、使用keepalived来监控nginx

编辑一个sh监控脚本,sh脚本:
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx #重启nginx
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx重启失败,则停掉keepalived服务,进行VIP转移
killall keepalived #杀掉,vip就漫游到另一台机器
fi
fi

在配置文件中加入以下两处配置:

nginx 学习-nginx高可用(keepalived)_nginx_08

k、重启keepalived,测试监控效果,如下图操作:

nginx 学习-nginx高可用(keepalived)_优先级_09

nginx已变成不死鸟

keepalived安装:

1,下载地址:https://pan.baidu.com/s/1G7sLL-YkZGSMu8G76yz1Rw 密码:adbw。
对应centos6系统

2.keepalived安装步骤:
A〉./configure --prefix=/data/program/keepalived --sysconf=/etc ##因为keepalive启动时候会默认读取/etc/keepalived/keepalived.conf
B〉make && make install

3,修改/etc/keepalived/keepalived.conf配置文件信息

4,启动keepalived
/data/program/keepalived/sbin/keepalived

5,KEEPLIVED主从故障测试
起停主从keepalived,查看对应的vip=192.168.244.200的漫游情况

6,加nginx监控脚本
停止nginx,观察keepalived自动拉起nginx服务

7,查看keepalived日志: tail -f /var/log/messages
配置:
global_defs { #全局配置
router_id nginx_backup #表示运行Keepalived服务器的一个标识,唯一的
}
vrrp_script chk_http_port {
script "/usr/local/src/chk_nginx_pid.sh" #心跳执行的脚本
interval 2 #(检测脚本执行的间隔,单位是秒)
weight 2
}
vrrp_instance VI_1 { #vrrp 实例定义部分
state BACKUP # 指定keepalived的角色,MASTER为主,BACKUP为备
interface eth0 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
virtual_router_id 66 # 虚拟路由编号,主从要一直
priority 100 # 优先级,数值越大,获取处理请求的优先级越高
advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数)
authentication {
auth_type PASS #设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信
auth_pass 1111
}
track_script {
chk_http_port #(调用检测脚本)
}
virtual_ipaddress {
192.168.244.200 # 定义虚拟ip(VIP),可多设,每行一个
}
}

监控脚本:
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx #重启nginx
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx重启失败,则停掉keepalived服务,进行VIP转移
killall keepalived #杀掉,vip就漫游到另一台机器
fi
fi