首页 > 其他 > 详细

Nginx + Keepalived实现高可用

时间:2019-10-07 16:12:50      阅读:74      评论:0      收藏:0      [点我收藏+]

实验环境:

192.168.200.111   nginx主+tomcat

192.168.200.112   nginx备+tomcat

在111和112上安装tomcat的目的只是为了测试

也可以在其他两台服务器上安装tomcat

模式一:双机 主从模式

在111和112上首先安装tomcat

yum -y ipvsadm keepalived

源码安装nginx

安完启动服务(省略)

111和112的配置

技术分享图片

 

 技术分享图片

 

 技术分享图片

 

 112的就修改state  BACKUP

vrrp_script chk_http_port {
script "/opt/chk_nginx.sh"
interval 2
weight -5
fall 2
rise 1
}

 

[root@localhost ~]# vim /opt/chk_nginx.sh

#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
echo "$counter"
if [ "${counter}" = "0" ]; then
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/etc/init.d/keepalived stop
fi
fi

chmod 755 /opt/chk_nginx.sh

sh /opt/chk_nginx.sh

vim /usr/local/nginx/conf/nginx.conf

技术分享图片

 

如果是yum安装的nginx 步骤

vim /etc/nginx/conf.d/node1.conf

upstream web1 {
server 192.168.200.111:8080;
server 192.168.200.112:8080;
}
server {
listen 192.168.200.254:80;
server_name www.node.com;
index index.html index.htm;
location / {
proxy_set_header Host $host;
proxy_pass http://web1;
}
}

 

systemctl start keepalived

systemctl start nginx

killall  -1  nginx

测试:

ip  a

systemctl   stop  keepalived

来回测试

echo "111111" > /usr/local/tomcat8/webapps/ROOT/index.jsp (112可以是"22222")

技术分享图片

 

 技术分享图片

 

 

 二。双机 双主模式

111:

vrrp_instance VI_2 {
state BACKUP
interface ens32
virtual_router_id 52
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.253
}
track_script {
chk_http_port
}
}

 

112:

vrrp_instance VI_2 {
state MASTER
interface ens32
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.253
}
track_script {
chk_http_port
}
}

重启服务

ip  a 查看

Nginx + Keepalived实现高可用

原文:https://www.cnblogs.com/maoyanqing/p/11630678.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!