一、 地址规划

        nginx1-10.1.1.2

        nginx2-10.1.1.3

        vip-10.1.1.4

    修改两台主机名:hostnamectl set-hostname nginx1

                             hostnamectl set-hostname nginx2

  二、在nginx1和nginx2上分别安装nginx,这里用的是最新的稳定版

   先安装依赖包

   yum -y install gcc gcc++ prce pcre-devel zlib zlib-devel openssl openssl-devel

    官网下载nginx-1.10.3源码包

    tar xzvf nginx-1.10.3.tar.gz 
   mkdir -p /usr/local/nginx
   mv nginx-1.10.3 /usr/local/
    cd /usr/local/nginx-1.10.3/
  ./configure --prefix=/usr/local/nginx
   make && make install

  安装后注册nginx服务,使其可以开机启动
   vi /lib/systemd/system/nginx.service
  [Unit]
  Description=nginx
  After=network.target
 
  [Service]
  Type=forking
  ExecStart=/usr/local/nginx/sbin/nginx
  ExecReload=/usr/local/nginx/sbin/nginx -s reload
  ExecStop=/usr/local/nginx/sbin/nginx -s quit
  PrivateTmp=true
 
  [Install]
  WantedBy=multi-user.target

  设置nginx开机启动命令: systemctl enable nginx

   启动nginx systemctl start nginx

 三、分别在nginx1-2上安装keepalived

 centos7可以直接yum安装keepalived,注意配置文件和脚本,否则不成功

keepalived -v
Keepalived v1.2.13 (11/05,2016) 版本信息

  yum -y install keepalived
  mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.orginal
  vi /etc/keepalived/keepalived.conf

  nginx1-master配置文件如下(为了方便你的粘贴就不上截图了):
 more /etc/keepalived/keepalived.conf (nginx1-master)

! Configuration file for keepalived
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
}
#监控nginx
vrrp_script chk_nginx {
    script "/etc/keepalived/script/monitor_nginx.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state MASTER  #主从设置 MASTER
    interface eth1  #网卡名
    virtual_router_id 51
    mcast_src_ip 10.1.1.2 #本机ip
    priority 51  #从机小于主机
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
    }
    virtual_ipaddress {
        10.1.1.4  #VIP 的IP
    }
    track_script {
        chk_nginx  #检测脚本
    }

}

virtual_server 10.1.1.4 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 10.1.1.2 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    } 

nginx2-backup配置文件如下:

more /etc/keepalived/keepalived.conf (nginx1-backup)

! Configuration file for keepalived
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
}
#监控NGINX
vrrp_script chk_nginx {
    script "/etc/keepalived/script/monitor_nginx.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP  #主从设置 MASTER
    interface eth1  #网卡名
    virtual_router_id 50
    mcast_src_ip 10.1.1.2 #本机ip
    priority 50  #从机小于主机
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
    }
    virtual_ipaddress {
        10.1.1.4  #VIP 的IP
    }
    track_script {
        chk_nginx  #检测脚本
    }

}

virtual_server 10.1.1.4 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 10.1.1.2 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    real_server 10.1.1.3 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}
监测脚本如下(nginx1-2相同):

  more /etc/keepalived/monitor_nginx.sh

 #!/bin/bash
  A=`ps -C nginx –no-header |wc -l`
 if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        systemctl stop keepalived
    fi
fi

 hostnamectl set-hostname nginx2
# 安装ipvsadm ()

#  yum install ipvsadm

在两台nginx分别执行脚本,绑定虚拟IP,抑制ARP广播

 

#!/bin/bash
#description: Config realserver

VIP=10.1.1.4
 
/etc/rc.d/init.d/functions
 
case "$1" in
start)
       /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
       /sbin/route add -host $VIP dev lo:0
       echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
       sysctl -p >/dev/null 2>&1
       echo "RealServer Start OK"
       ;;
stop)
       /sbin/ifconfig lo:0 down
       /sbin/route del $VIP >/dev/null 2>&1
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
       echo "RealServer Stoped"
       ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac
 
exit 0