首页 > 系统相关 >Nginx+keepalived主从双机热备自动切换解决方案

Nginx+keepalived主从双机热备自动切换解决方案

时间:2023-08-10 22:31:51浏览次数:39  
标签:热备 conf -- keepalived 192.168 nginx ff 双机

Nginx+keepalived主从双机热备自动切换解决方案



测试环境如下:


系统:Ceentos 6.4 64位


主nginx服务器:192.168.122.5


备nginx服务器:192.168.122.6


VIP:192.168.122.15


一、Nginx+keepalived 安装—脚本安装



#!/bin/bash
# author: kuangl
# mail: [email protected]
# description: The installation of Nginx files.
# -------------------------------------------------------- #
## Nginx_install
# -------------------------------------------------------- #
# Nginx installation
#CURRENT_PATH=$(pwd)
for  i  in  $(rpm -q gcc gcc-c++ kernel-devel openssl-devel zlib-devel popt-devel popt-static libnl-devel wget  make  | grep  'not installed'  |  awk  '{print $2}' )
do
yum -y  install  $i
done
[ -d  /root/software  ]
[  "$?"  != 0 ] &&  mkdir  /root/software
cd  /root/software
[ !-e pcre-8.33. tar .gz ] && wget  ftp : //ftp .csx.cam.ac.uk /pub/software/programming/pcre/pcre-8 .33. tar .gz
tar  -zxvf pcre-8.33. tar .gz
cd  pcre-8.33
. /configure
make  &&  make  install
echo  $? || [ $? != 0] ||  echo " installation pcrefailed"  ||  exit  1
cd  /root/software
[ ! -e nginx-1.2.9. tar .gz ] && wget http: //nginx .org /download/nginx-1 .2.9. tar .gz
tar  -zxvf nginx-1.2.9. tar .gz
cd  nginx-1.2.9
. /configure--prefix = /usr/local/nginx  --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module--with-http_gzip_static_module
make  &&  make  install
echo  $? || [ $? != 0] ||  echo " installationnginxfailed"  ||  exit  1
# -------------------------------------------------------- #
## Keepalived_intsall
# -------------------------------------------------------- #
# Keepalived installation
cd  /root/softwarae
[ ! -e keepalived-1.2.4. tar .gz ] &&wget http: //www .keepalived.org /software/keepalived-1 .2.4. tar .gz
tar  -zxvf keepalived-1.2.4. tar .gz
cd  keepalived-1.2.4
ln  -s  /usr/src/kernels/ $( uname  -r)  /usr/src/kernels/linux
. /configure  --prefix= /usr--bindir = /usr/bin--sbindir = /usr/bin--libexecdir = /usr/libexec  --localstatedir= /var  --libdir= /lib64--infodir = /usr/share/info--sysconfdir = /etc  --mandir= /usr/local/share/man--with-kernel-dir = /usr/src/kernels/linux
make  &&  make  install
echo  $? || [ $? != 0] || print  " installation keepalivedfailed"  ||  exit  1
chkconfig --add keepalived
chkconfig --level 345 keepalived on


二、主Nginx 配置


[root@node5 conf] # mkdir -p /var/www/html
[root@node5 conf] # cat "192.168.122.5" > /var/www/html/index.html
[root@node5 conf] # vim nginx.conf




[root@node5 conf] # ../sbin/nginx -s reload
[root@node5 conf] # curl http://192.168.122.5
192.168.122.5



三、主Keepalived配置


[root@node6 conf] # vim /etc/keepalived/keepalived.conf
! Configuration File  for  keepalived
global_defs {
notification_email {
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script  "/etc/keepalived/chk_nginx.keepalived.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass kuangling
}
track_script {
chk_nginx.keepalived
}
virtual_ipaddress {
192.168.122.15
}
}



四、备nginx配置

t@node6 conf] # mkdir -p /var/www/html
[root@node6 conf] # cat "192.168.122.6" > /var/www/html/index.html
[root@node6 conf] # vim nginx.conf


[root@node6 conf] # ../sbin/nginx -s reload
[root@node6 conf] # curl http://192.168.122.6
192.168.122.6

五、备keepalived配置


[root@node6 conf] # vim /etc/keepalived/keepalived.conf
! Configuration File  for  keepalived
global_defs {
notification_email {
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script  "/etc/keepalived/chk_nginx.keepalived.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass kuangling
}
track_script {
chk_nginx.keepalived
}
virtual_ipaddress {
192.168.122.15
}
}


六、分别在2台nginx服务器上添加检测脚本:


[root@node6 conf] # vim /etc/keepalived/chk_nginx.keepalived.sh
#!/bin/bash
# description:
# 定时查看Nginx是否存在,如果不存在则启动Nginx
# 如果启动失败,则停止keepalived
status=` ps  -C nginx --no-header | wc  -l`
if  [ $status - eq  0 ]; then
/usr/local/nginx/sbin/nginx
sleep  3
if  [ ` ps  -C nginx --no-header | wc  -l` - eq  0 ]; then
killall keepalived
fi
fi
[root@node6 conf] #chmod +x /etc/keepalived/chk_nginx.keepalived.sh

七、测试

分别在2台nginx上启动nginx和keepalived服务,然后分别用ip a 查看ip







nginx+keepalived实现双机热备的高可用

64人收藏此文章, 我要收藏发表于15天前(2013-09-12 23:11) , 已有 664次阅读 ,共 1个评论

目录:[ - ]


keepalived安装

keepalived的配置

keepalived的测试

这篇文章简单介绍利用keepalived软件,实现对nginx服务器的高可用,即实现故障自动切换。假设你已经安装好nginx,下面介绍keepalived的安装和使用。


keepalived安装

yum install openssl-devel

cd /tmp

wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz

tar xzf keepalived-1.2.2.tar.gz

cd keepalived-1.2.2

./configure

make && make install

cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

chmod +x /etc/init.d/keepalived

chkconfig --add keepalived

chkconfig keepalived on

mkdir /etc/keepalived

ln -s /usr/local/sbin/keepalived /usr/sbin/

keepalived的配置

更详细的keepalived配置文件说明可以执行man keepalived.conf查看。

我们假设主服务器IP:192.168.1.103,从服务器ip:192.168.1.101 虚拟ip:192.168.1.110

下面对主服务器的keepalived进行配置:


vi /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_http_port {
                script "/opt/nginx_pid.sh"
                interval 2
                weight 2
}
vrrp_instance VI_1 {
    state MASTER        ############ 辅机为 BACKUP
    interface eth0
    virtual_router_id 51
    mcast_src_ip 192.168.1.103
    priority 102                  ########### 权值要比 back 高
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
track_script { 
        chk_http_port ### 执行监控的服务 
        }
    virtual_ipaddress {
       192.168.1.110
    }
}

从服务器:


global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_http_port {
                script "/opt/nginx_pid.sh"
                interval 2
                weight 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    mcast_src_ip 192.168.1.101
    priority 101              ##########权值 要比 master 低。。
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
track_script { 
        chk_http_port ### 执行监控的服务 
        }
    virtual_ipaddress {
       192.168.1.110
    }
}

之后分别在主从服务器建立nginx的监控脚本:


vi /opt/nginx_pid.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`               
if [ $A -eq 0 ];then                                       
                /usr/local/nginx/sbin/nginx
                sleep 3
                if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
                       killall keepalived
                fi
fi

然后分别启动主从服务器的keepalived:


service keepalived start

keepalived的测试

我们在主服务器上执行命令ip a,显示如下:


2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000

   link/ether 00:0c:29:aa:a1:e4 brd ff:ff:ff:ff:ff:ff

   inet 192.168.1.103/24 brd 255.255.255.255 scope global eth0

   inet 192.168.1.110/32 scope global eth0

证明主服务器已经绑定了虚拟ip 192.168.1.110

在从服务器上执行命令ip a,显示如下:


2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000

   link/ether 00:0c:29:2b:94:3b brd ff:ff:ff:ff:ff:ff

   inet 192.168.1.101/24 brd 255.255.255.255 scope global eth0

显示表明从服务器上没有绑定vip 192.168.1.110,只有本机真实ip192.168.1.101

下面我们停止主服务器的nginx进程,再看看ip绑定情况:

主服务器的情况:


2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000

   link/ether 00:0c:29:aa:a1:e4 brd ff:ff:ff:ff:ff:ff

   inet 192.168.1.103/24 brd 255.255.255.255 scope global eth0

从服务器的情况:


2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000

   link/ether 00:0c:29:2b:94:3b brd ff:ff:ff:ff:ff:ff

   inet 192.168.1.101/24 brd 255.255.255.255 scope global eth0

   inet 192.168.1.110/32 scope global eth0

由此可见vip已经指向了从服务器。


标签:热备,conf,--,keepalived,192.168,nginx,ff,双机
From: https://blog.51cto.com/u_16087105/7040790

相关文章

  • 防火墙双机热备之主备和主主相关知识
    参考:https://kb.hillstonenet.com/cn/ha-dual-redundancy-inspection/https://support.huawei.com/hedex/hdx.do?docid=EDOC1100149311&id=ZH-CN_CONCEPT_0178924792https://www.h3c.com/cn/d_201405/922032_30005_0.htmActive-Passive模式Active-Passive(A/P)模式:在HA簇中配置两台设......
  • mysql双主机热备
    1,主库配置cp-f~/files/my.cnf.master/opt/my.cnfservicemysqlrestartmysql-uroot-pcreateuser'repl'@'%'identifiedwithmysql_native_passwordby'repl';GRANTREPLICATIONSLAVEON*.*TO'repl'@'%';FLUSHPR......
  • keepalived 邮件通知无法发送邮件问题解决【亲测有效,没有效果来找我】
    环境keepalivedkeepalived-2.2.7操作系统cenos7安装方式源码编译安装问题最近在安装keepalived高可用服务,环境是安装完了,但是我想要使用邮件通知这个功能,通过网上捞针怎么也不成功,真是绝绝子,折磨我1天多。终于在刚刚得到了解决办法解决在vrrp_instance自定义的名字中添加......
  • centos7 源码安装keepalived 【亲测有效,没有效果你来找我】
    环境keepalived版本:keepalived-2.2.7操作系统:cenos7安装方式:源码编译安装环境安装#安装包下载wgethttps://keepalived.org/software/keepalived-2.2.7.tar.gz#安装编译源码所需依赖yum-yinstallgccopenssl-devellibnfnetlink-devellibnllibnl-develpopt-devel......
  • 基于keepalived+gtid半同步主从复制的MySQL集群
    mysql的安装(二进制安装)[root@my-slv1~]#catmysql_install.sh#!/bin/bash#解决软件的依赖关系yuminstallcmakencurses-develgccgcc-c++vimlsofbzip2openssl-develncurses-compat-libs-y#解压mysql二进制安装包tarxfmysql-5.7.38-linux-glibc2.12-x86_64.ta......
  • 第二节:KeepAlived详解、Lvs+KeepAlived实战
    一.        二.        三.         !作       者:Yaopengfei(姚鹏飞)博客地址:http://www.cnblogs.com/yaopengfei/声     明1:如有错误,欢迎讨论,请勿谩骂^_^。声     明2:原创博客请在转载......
  • Nginx+Keepalived实现站点高可用
    公司内部OA系统要做线上高可用,避免单点故障,所以计划使用2台虚拟机通过Keepalived工具来实现nginx的高可用(HighAvaiability),达到一台nginx入口服务器宕机,另一台备机自动接管服务的效果。(nginx做反向代理,实现后端应用服务器的负载均衡)快速搭建请直接跳至第2节。1.Keepalived......
  • 使用Rsync进行双机热备
              ......
  • Keepalived脑裂的解决和预防(附脚本)
    在实际生产环境中,我们可以从以下几个方面来防止裂脑问题的发生:同时使用串行电缆和以太网电缆连接,同时用两条心跳线路,这样一条线路坏了,另一个还是好的,依然能传送心跳消息。当检测到裂脑时强行关闭一个心跳节点(这个功能需特殊设备支持,如Stonith、feyce)。相当于备节点接收不到心跳消患......
  • Keepalived非抢占模式配置
    Keepalived非抢占模式配置1、前言非抢占模式,这种模式主要是针对主机崩溃,集群已经重新选出新的主机,并且原来的主机重新上线后并不争夺主机的情况。这种模式适合那些倾向于认为崩溃的主机即便上线还是会出现崩溃的场景。这种模式的核心思想是将所有节点的优先级(priority)值设为相......