nginx+keepalived高可用的并发解决方案(两台虚拟机)
nginx+keepalived高可用的并发解决方案(两台虚拟机)
1.分类安装nginx并搭建tomcat集群
2.执行yum -y install libnl libnl-devel
yum install -y libnfnetlink-devel
3.上传keepalived安装包并解压,解压后安装编译
./configure --prefix=/kht/keepalived
make && make install
启动命令:systemctl start keepalived
4.查看keepalived的实时日志:tail -f /var/log/messages
4.在etc下新建keepalived目录,将安装后的/kht/keepalived目录下的
/etc/keepalived的keepalived.conf配置文件拷贝到/etc/keepalived目录下
5.将安装后的/kht/keepalived目录下的/etc/sysconfig/keepalived
拷贝到/etc/sysconfig/目录
6.执行
ln -s /kht/keepalived/sbin/keepalived /usr/local/bin/
ln -s /kht/keepalived/bin/genhash /usr/local/bin/
8.在/etc/keepalived目录下vim chek_nginx.sh,
为chek_nginx.sh授权必须是744不能是777,内容如下
#!/bin/bash
Ng=`ps -C nginx --no-header | wc -l`
if [ $Ng -eq 0 ];then
#nginx重启命令
/kht/tomcatcolony/nginxtomcat/sbin/nginx
sleep 2
Ng=`ps -C nginx --no-header | wc -l`
if [ $Ng -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
9.vim /etc/keepalived.conf(主机)
global_defs {
router_id keep127
script_user root
enable_script_security
#keep127保持与主机名一致,在/etc/hosts/最下方添加127.0.0.1 keep127
}
vrrp_script chek_nginx {
script "/etc/keepalived/chek_nginx.sh" #检测 nginx 状态的脚本路径
interval 2 # 检测时间间隔
weight -20
}
vrrp_instance VI_1 {
state MASTER #状态
interface ens33 #网卡名称
virtual_router_id 127 #与备主机一致
mcast_src_ip 192.168.2.127
priority 100 #优先级,正比
nopreempt#优先级高的设置,nopreempt解决异常恢复后再次抢占的问题
advert_int 1
#组播信息发送间隔,两个节点设置必须—样,默认为1s
authentication {
auth_type PASS
auth_pass kht #根据情况
}
track_script {
chek_nginx #监控脚本
}
virtual_ipaddress {
192.168.2.131 #VIP虚拟ip,主备一致
}
}
10.vim /etc/keepalived.conf(备机)
global_defs {
router_id keep130
}
vrrp_script chek_nginx {
script "/etc/keepalived/chek_nginx.sh" #检测 nginx 状态的脚本路径
interval 2 # 检测时间间隔
weight -20
}
vrrp_instance VI_1 {
state BACKUP #状态
interface ens33 #网卡名称
virtual_router_id 126 #与备主机一致
priority 90 #优先级,正比
mcast_src_ip 192.168.2.126
advert_int 1
authentication {
auth_type PASS
auth_pass kht #根据情况
}
track_script {
chek_nginx #监控脚本
}
virtual_ipaddress {
192.168.2.131 #VIP
}
}