Nginx + keepalived 双主解决方案

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx + keepalived 双主解决方案相关的知识,希望对你有一定的参考价值。

   工作原理:两台nginx通过Keepalived生成二个实例,二台Nginx的VIP互为备份,任何一台Nginx机器如果发生硬件损坏,Keepalived会自动将它的VIP地址切换到另一台机器,不影响客户端的访问。


IP 信息列表:

名称     IP
-----------------------------------
VIP1      192.168.200.254
VIP2      192.168.200.253
Nginx1     192.168.200.202
Nginx2     192.168.200.203

1、在Nginx1/2上编译安装nginx服务
首先搭建Nginx1,
[[email protected] ~]# yum -y install pcre-devel zlib-devel
[[email protected] ~]# useradd -M -s /sbin/nologin  nginx
[[email protected] ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src
[[email protected] ~]# cd /usr/src/nginx-1.6.2
[[email protected] nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[[email protected] nginx-1.6.2]# cd /usr/local/nginx/html/
[[email protected] html]# echo "server 192.168.200.202" > index.html
[[email protected] html]# /usr/local/nginx/sbin/nginx
[[email protected] html]# netstat -anpt |grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4503/nginx 

搭建Nginx2, 同Nginx1搭建方式是一样的。
与Nginx1唯一不同的是:
[[email protected] html]# echo "server 192.168.200.203" > index.html


2、在Nginx1/2上编译安装keepalived服务
[[email protected] ~]# yum -y install kernel-devel openssl-devel

[[email protected] ~]# tar xf keepalived-1.2.13.tar.gz
[[email protected] ~]# cd keepalived-1.2.13
[[email protected] keepalived-1.2.13]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-i686 && make && make install

[[email protected] ~]# chkconfig --add keepalived
[[email protected] ~]# chkconfig keepalived on
[[email protected] ~]# chkconfig --list keepalived
[roo[email protected] ~]# service keepalived start|stop   

3、修改keepalived配置文件
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
        [email protected]
}
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}


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

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123
    }
    virtual_ipaddress {
        192.168.200.253
    }
}


==================================================================
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
        [email protected]
}
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}


vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123
    }
    virtual_ipaddress {
        192.168.200.254
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123
    }
    virtual_ipaddress {
        192.168.200.253
    }
}

[[email protected] ~]# service keepalived start
[[email protected] ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2d:3d:97 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.202/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.254/32 scope global eth0
    inet6 fe80::20c:29ff:fe2d:3d97/64 scope link
       valid_lft forever preferred_lft forever


[[email protected] ~]# service keepalived start
[[email protected] ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:6f:7d:87 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.203/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.253/32 scope global eth0
    inet6 fe80::20c:29ff:fe6f:7d87/64 scope link
       valid_lft forever preferred_lft forever
   
[[email protected] ~]# elinks --dump http://192.168.200.254
   server 192.168.200.202
[[email protected] ~]# elinks --dump http://192.168.200.253
   server 192.168.200.203
  

  
Nginx-1/2 二台机器都执行监控Nginx进程的脚本
[[email protected] ~]# cat nginx_pidcheck
#!/bin/bash
while :
do
 nginxpid=`ps -C nginx --no-header | wc -l`
 if [ $nginxpid -eq 0 ]
 then
  /usr/local/nginx/sbin/nginx
  sleep 5
  nginxpid=`ps -C nginx --no-header | wc -l`
  echo $nginxpid
  if [ $nginxpid -eq 0 ]
  then
   /etc/init.d/keepalived stop
  fi
 fi
 sleep 5
done

[[email protected] ~]# nohup sh nginx_pidcheck &

这是执行无限循环的脚本,两台Nginx机器上都有执行此脚本,每隔5秒执行一次,用ps -C是命令来收集nginx的PID值到底是否为0,如果是0的话,即Nginx已经进程死掉,尝试启动nginx进程;如果继续为0,即Nginx启动失败,则关闭本机的Keeplaived服务,VIP地址则会由备机接管,当然了,整个网站就会全部由备机的Nginx来提供服务了,这样保证Nginx服务的高可用。


脚本测试:
[[email protected] ~]# netstat -anpt |grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4321/nginx         
[[email protected] ~]# killall -s QUIT nginx
[[email protected] ~]# netstat -anpt |grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      59418/nginx

VIP转移测试:
[[email protected] ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2d:3d:97 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.202/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.254/32 scope global eth0
    inet6 fe80::20c:29ff:fe2d:3d97/64 scope link
       valid_lft forever preferred_lft forever
   
[[email protected] ~]# service keepalived stop
停止 keepalived:                                          [确定]


[[email protected] ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2d:3d:97 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.202/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.254/32 scope global eth0
    inet 192.168.200.253/32 scope global eth0
    inet6 fe80::20c:29ff:fe2d:3d97/64 scope link
       valid_lft forever preferred_lft forever

[[email protected] ~]# elinks --dump http://192.168.200.254
   server 192.168.200.202
[[email protected] ~]# elinks --dump http://192.168.200.253
   server 192.168.200.202

 







































































































































































以上是关于Nginx + keepalived 双主解决方案的主要内容,如果未能解决你的问题,请参考以下文章

Keepalived+Nginx实现双主高可用负载均衡

nginx+keepalived高可用及双主模式

nginx+keepalived高可用及双主模式

Vmware部署Nginx+KeepAlived集群双主架构

keepalived主从及双主配置

keepalived高可用nginx/htttpd,双主模型