Nginx+Keepalived负载集群高可用

Posted woaiyunwei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx+Keepalived负载集群高可用相关的知识,希望对你有一定的参考价值。

nginx+Keepalived负载集群高可用

一、keepalived简介

1.1.1 介绍

1.Keepalived是基于vrrp协议的一款高可用软件。Keepailived有一台主服务器和多台备份服务器,在主服务器和备份服务器上面部署相同的服务配置,使用一个虚拟IP地址对外提供服务,当主服务器出现故障时,虚拟IP地址会自动漂移到备份服务器。
VRRP(Virtual Router Redundancy Protocol,虚拟路由器冗余协议),VRRP是为了解决静态路由的高可用。VRRP的基本架构
虚拟路由器由多个路由器组成,每个路由器都有各自的IP和共同的VRID(0-255),其中一个VRRP路由器通过竞选成为MASTER,占有VIP,对外提供路由服务,其他成为BACKUP,MASTER以IP组播(组播地址:224.0.0.18)形式发送VRRP协议包,与BACKUP保持心跳连接,若MASTER不可用(或BACKUP接收不到VRRP协议包),则BACKUP通过竞选产生新的MASTER并继续对外提供路由服务,从而实现高可用。

2.安装环境装备

主机ip 作用
10.0.0.7 nginx+keepalived(主)
10.0.0.8 nginx+keepalived(备)
10.0.0.3 飘移VIP

1.1.2 下载安装 keepalived

1.官网地址:http://www.keepalived.com/download.html

[root@lb02 /server/tools]# wget http://www.keepalived.com/software/keepalived-2.0.18.tar.gz
--2020-05-19 13:56:26--  http://www.keepalived.com/software/keepalived-2.0.18.tar.gz
Resolving www.keepalived.com (www.keepalived.com)... 37.59.63.157, 2001:41d0:8:7a9d::1
Connecting to www.keepalived.com (www.keepalived.com)|37.59.63.157|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1015958 (992K) [application/x-gzip]
Saving to: ‘keepalived-2.0.18.tar.gz’

100%[===========================================================================================>] 1,015,958   6.64KB/s   in 1m 58s 

2020-05-19 13:58:26 (8.41 KB/s) - ‘keepalived-2.0.18.tar.gz’ saved [1015958/1015958]

[root@lb02 /server/tools]# ll
total 996
-rw-r--r-- 1 root root 1015958 Jul 26  2019 keepalived-2.0.18.tar.gz
  1. 环境依赖安装
yum install e2fsprogs-devel keyutils-libs-devel libsepol-devel libselinux-devel krb5-devel  zlib-devel openssl openssl-devel libnl libnl-devel -y
  1. 解压安装keepalived
[root@lb02 /server/tools]# tar zxf keepalived-2.0.18.tar.gz 
[root@lb02 /server/tools]# cd keepalived-2.0.18/
[root@lb02 /server/tools/keepalived-2.0.18]# ./configure --prefix=/appliction/keepalived-2.0.18
[root@lb02 /server/tools/keepalived-2.0.18]# make && make install

4.做软连接

[root@lb02 /server/tools/keepalived-2.0.18]# ln -s /appliction/keepalived-2.0.18/ /appliction/keepalived
[root@lb02 /server/tools/keepalived-2.0.18]# ll -ld /appliction/keepalived
lrwxrwxrwx 1 root root 30 May 19 14:14 /appliction/keepalived -> /appliction/keepalived-2.0.18/
[root@lb02 /server/tools/keepalived-2.0.18]# tree /appliction/keepalived
/appliction/keepalived
├── bin
│?? └── genhash
├── etc
│?? ├── keepalived
│?? │?? ├── keepalived.conf
│?? │?? └── samples
│?? │??     ├── client.pem
│?? │??     ├── dh1024.pem
│?? │??     ├── keepalived.conf.conditional_conf
│?? │??     ├── keepalived.conf.fwmark
│?? │??     ├── keepalived.conf.HTTP_GET.port
│?? │??     ├── keepalived.conf.inhibit
│?? │??     ├── keepalived.conf.IPv6
│?? │??     ├── keepalived.conf.misc_check
│?? │??     ├── keepalived.conf.misc_check_arg
│?? │??     ├── keepalived.conf.quorum
│?? │??     ├── keepalived.conf.sample
│?? │??     ├── keepalived.conf.SMTP_CHECK
│?? │??     ├── keepalived.conf.SSL_GET
│?? │??     ├── keepalived.conf.status_code
│?? │??     ├── keepalived.conf.track_interface
│?? │??     ├── keepalived.conf.virtualhost
│?? │??     ├── keepalived.conf.virtual_server_group
│?? │??     ├── keepalived.conf.vrrp
│?? │??     ├── keepalived.conf.vrrp.localcheck
│?? │??     ├── keepalived.conf.vrrp.lvs_syncd
│?? │??     ├── keepalived.conf.vrrp.routes
│?? │??     ├── keepalived.conf.vrrp.rules
│?? │??     ├── keepalived.conf.vrrp.scripts
│?? │??     ├── keepalived.conf.vrrp.static_ipaddress
│?? │??     ├── keepalived.conf.vrrp.sync
│?? │??     ├── root.pem
│?? │??     ├── sample.misccheck.smbcheck.sh
│?? │??     └── sample_notify_fifo.sh
│?? └── sysconfig
│??     └── keepalived
├── sbin
│?? └── keepalived
└── share
    ├── doc
    │?? └── keepalived
    │??     └── README
    ├── man
    │?? ├── man1
    │?? │?? └── genhash.1
    │?? ├── man5
    │?? │?? └── keepalived.conf.5
    │?? └── man8
    │??     └── keepalived.8
    └── snmp
        └── mibs

15 directories, 36 files
  1. 其实也可以yum安装
[root@lb02 /server/tools/keepalived-2.0.18]# yum install keepalived -y

1.1.2 使用keepalived

  1. 在源码文件中拷贝启动命令文件
[root@lb02 ~]# cp /server/tools/keepalived-2.0.18/keepalived/etc/init.d/keepalived /etc/init.d/
[root@lb02 ~]# ll /etc/init.d/
total 44
-rw-r--r-- 1 root root 18281 Mar 29  2019 functions
-rwxr-xr-x 1 root root  1308 May 19 18:51 keepalived
-rwxr-xr-x 1 root root  4569 Mar 29  2019 netconsole
-rwxr-xr-x 1 root root  7923 Mar 29  2019 network
-rw-r--r-- 1 root root  1160 Mar 18 07:46 README

2.在源码目录文件中拷贝sysconfig/keepalivedw配置文件

[root@lb02 ~]# cp /server/tools/keepalived-2.0.18/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

3.在源码目录文件中拷贝keepalived

[root@lb02 ~]# mkdir /etc/keepalived
[root@lb02 ~]# cp /server/tools/keepalived-2.0.18/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@lb02 ~]# ll /etc/keepalived/
total 4
-rw-r--r-- 1 root root 3550 May 19 18:59 keepalived.conf
  1. 在安装目录拷贝keepalived命令
[root@lb02 ~]# cp /appliction/keepalived/sbin/keepalived /usr/sbin/
[root@lb02 ~]# ll /usr/sbin/keepalived
-rwxr-xr-x 1 root root 2383840 May 19 19:01 /usr/sbin/keepalived

5.启动keepalived

[root@lb02 ~]# systemctl start keepalived
[root@lb02 ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-05-19 19:03:12 CST; 6s ago
  Process: 1637 ExecStart=/appliction/keepalived-2.0.18/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1638 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─1638 /appliction/keepalived-2.0.18/sbin/keepalived -D
           ├─1639 /appliction/keepalived-2.0.18/sbin/keepalived -D
           └─1640 /appliction/keepalived-2.0.18/sbin/keepalived -D

May 19 19:03:15 lb02 Keepalived_vrrp[1640]: Sending gratuitous ARP on eth0 for 192.168.200.18
May 19 19:03:15 lb02 Keepalived_vrrp[1640]: Sending gratuitous ARP on eth0 for 192.168.200.16
May 19 19:03:15 lb02 Keepalived_vrrp[1640]: Sending gratuitous ARP on eth0 for 192.168.200.17
May 19 19:03:15 lb02 Keepalived_vrrp[1640]: Sending gratuitous ARP on eth0 for 192.168.200.18
May 19 19:03:15 lb02 Keepalived_vrrp[1640]: Sending gratuitous ARP on eth0 for 192.168.200.16
May 19 19:03:15 lb02 Keepalived_vrrp[1640]: Sending gratuitous ARP on eth0 for 192.168.200.17
May 19 19:03:15 lb02 Keepalived_vrrp[1640]: Sending gratuitous ARP on eth0 for 192.168.200.18
May 19 19:03:16 lb02 Keepalived_healthcheckers[1639]: HTTP_CHECK on service [192.168.200.5]:tcp:1358 failed after 3 retry.
May 19 19:03:16 lb02 Keepalived_healthcheckers[1639]: Removing service [192.168.200.5]:tcp:1358 to VS [10.10.10.3]:tcp:1358
May 19 19:03:16 lb02 Keepalived_healthcheckers[1639]: Remote SMTP server [192.168.200.1]:25 connected.
[root@lb02 ~]# ps -ef |grep keepalived
root       1638      1  0 19:03 ?        00:00:00 /appliction/keepalived-2.0.18/sbin/keepalived -D
root       1639   1638  0 19:03 ?        00:00:00 /appliction/keepalived-2.0.18/sbin/keepalived -D
root       1640   1638  0 19:03 ?        00:00:00 /appliction/keepalived-2.0.18/sbin/keepalived -D
root       1675   1395  0 19:03 pts/0    00:00:00 grep --color=auto keepalived
[root@lb02 ~]# systemctl enable keepalived
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.

6.修改配置文件

[root@lb02 /etc/keepalived]# cp keepalived.conf{,.back}
[root@lb02 /etc/keepalived]# ll
total 8
-rw-r--r-- 1 root root 3550 May 19 18:59 keepalived.conf
-rw-r--r-- 1 root root 3550 May 19 19:10 keepalived.conf.back
[root@lb02 /etc/keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {                             #全局定义邮件配置(可以删除不用)
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {      #高可用配置段
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.16
        192.168.200.17
        192.168.200.18
    }
}

virtual_server 192.168.200.100 443 {     #LVS控制段
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.201.100 443 {
        weight 1
        SSL_GET {
            url {
              path /
              digest ff20ad2481f97b1754ef3e12ecd3a9cc
            }
            url {
              path /mrtg/
              digest 9b3a0c85a887a256d6939da88aabd8cd
            }
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
}

virtual_server 10.10.10.2 1358 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    sorry_server 192.168.200.200 1358

    real_server 192.168.200.2 1358 {
        weight 1
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.200.3 1358 {
        weight 1
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
}

virtual_server 10.10.10.3 1358 {
    delay_loop 3
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.200.4 1358 {
        weight 1
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.200.5 1358 {
        weight 1
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
}
#删除LVS的项,结果配置如下
[root@lb02 /etc/keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL    #备节点改成LVS_BACKUP
   vrrp_skip_check_adv_addr
   vrrp_strict            #这行要注释掉 否者后面会报错 不需要严格助手vrrp协议,不然VIP无法访问
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER           #备节点为NACKUP
    interface eth0
    virtual_router_id 51
    priority 100        #优先级备节点为50
    advert_int 1
    nopreempt  #通过这个设置我们可实现当优先级高的调度节点故障恢复后,不再抢占为主调度服务器,从而也就避免了因调度节点的故障及故障恢复后来回切换的问题。
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    10.0.0.3/24 dev eth0 label eth0:1    #设置VIP
    }
}
  1. 重启keepalived
[root@lb02 ~]# systemctl start keepalived
[root@lb02 ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-05-19 21:59:27 CST; 22h ago
  Process: 2428 ExecStart=/appliction/keepalived-2.0.18/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 2429 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─2429 /appliction/keepalived-2.0.18/sbin/keepalived -D
           └─2430 /appliction/keepalived-2.0.18/sbin/keepalived -D

May 19 22:48:24 lb02 Keepalived_vrrp[2430]: Sending gratuitous ARP on eth0 for 10.0.0.3
May 19 22:48:24 lb02 Keepalived_vrrp[2430]: (VI_1) Sending/queueing gratuitous ARPs on eth0 for 10.0.0.3
May 19 22:48:24 lb02 Keepalived_vrrp[2430]: Sending gratuitous ARP on eth0 for 10.0.0.3
May 19 22:48:24 lb02 Keepalived_vrrp[2430]: Sending gratuitous ARP on eth0 for 10.0.0.3
May 19 22:48:24 lb02 Keepalived_vrrp[2430]: Sending gratuitous ARP on eth0 for 10.0.0.3
May 19 22:48:24 lb02 Keepalived_vrrp[2430]: Sending gratuitous ARP on eth0 for 10.0.0.3
May 19 22:48:25 lb02 Keepalived_vrrp[2430]: (VI_1) Master received advert from 10.0.0.5 with higher priority 100, ours 50
May 19 22:48:25 lb02 Keepalived_vrrp[2430]: (VI_1) Entering BACKUP STATE
May 19 22:48:25 lb02 Keepalived_vrrp[2430]: (VI_1) removing VIPs.
May 19 22:48:25 lb02 Keepalived_vrrp[2430]: (VI_1) removing firewall drop rule
[root@lb02 ~]# systemctl enable keepalived
  1. 在host文件中设置10.0.0.3为VIP
C:WindowsSystem32driversetc
10.0.0.3     www.fzjh.org  www.etiantian.org  bbs.etiantian.org blog.etiantian.org  www.linux.com status.etiantian.org www.kdyp.org  blog.kdyp.org

9.ping测试一下10.0.0.3

[root@lb01 ~]# ping 10.0.0.3
PING 10.0.0.3 (10.0.0.3) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
报错:解决方法如下
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict     #把这行注释掉就可以了
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
      10.0.0.3/24 dev eth0 label eth0:1
    }
}

  1. 浏览器访问
    技术图片

  2. 停止lb01

[root@lb01 ~]# systemctl stop keepalived.service 
[root@lb01 ~]# systemctl status keepalived.service 
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Wed 2020-05-20 22:53:44 CST; 9s ago
  Process: 73752 ExecStart=/application/keepalived-2.0.18/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 73753 (code=exited, status=0/SUCCESS)

May 20 22:31:59 lb01 Keepalived_vrrp[73754]: (VI_1) Sending/queueing gratuitous ARPs on eth0 for 10.0.0.3
May 20 22:31:59 lb01 Keepalived_vrrp[73754]: Sending gratuitous ARP on eth0 for 10.0.0.3
May 20 22:31:59 lb01 Keepalived_vrrp[73754]: Sending gratuitous ARP on eth0 for 10.0.0.3
May 20 22:31:59 lb01 Keepalived_vrrp[73754]: Sending gratuitous ARP on eth0 for 10.0.0.3
May 20 22:31:59 lb01 Keepalived_vrrp[73754]: Sending gratuitous ARP on eth0 for 10.0.0.3
May 20 22:53:43 lb01 Keepalived[73753]: Stopping
May 20 22:53:43 lb01 systemd[1]: Stopping LVS and VRRP High Availability Monitor...
May 20 22:53:43 lb01 Keepalived_vrrp[73754]: (VI_1) sent 0 priority
May 20 22:53:43 lb01 Keepalived_vrrp[73754]: (VI_1) removing VIPs.
May 20 22:53:44 lb01 systemd[1]: Stopped LVS and VRRP High Availability Monitor.
  1. 查看lb02
[root@lb02 ~]# ip ad
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:b4:a2:59 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.6/24 brd 10.0.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.3/24 scope global secondary eth0:1  #ip存在
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feb4:a259/64 scope link 
       valid_lft forever preferred_lft forever
  1. 浏览器访问
    技术图片
    也是OK的 代表keepalived+Nginx高可用成功
    14.编写脚本监控nginx负载均衡器,如果负载均衡器宕机的同时也停止keepalived
[root@lb01 ~]# cat /server/scripts/ng_check.sh
#!/bin/bash
#aotu by chenhj 2020.3.15
#email gzchenhj@163.com
#nginx server check shell
  if [ `netstat -luntp|grep nginx|wc -l` -ne 1 ]
  then
  systemctl stop keepalived
  fi
  1. 赋予脚本执行权限
[root@lb01 ~]# chmod +x /server/scripts/ng_check.sh 
[root@lb01 ~]# ll /server/scripts/ng_check.sh
-rwxr-xr-x 1 root root 208 May 20 23:07 /server/scripts/ng_check.sh
  1. 在keepalived配置文件里添加脚本行
vrrp_script check_nginx {    #监控的名称
        script "/server/scripts/ng_check.sh"  #脚本路径
        interval 2     #多少秒检查一次
        weight 2        #权重
}

track_script {       #触发这个监本
		  check_nginx   #监控脚本名称
}   

完整配置

[root@lb01 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script check_nginx {
	script "/server/scripts/ng_check.sh"
	interval 2
	weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    nopreempt  #通过这个设置我们可实现当优先级高的调度节点故障恢复后,不再抢占为主调度服务器,从而也就避免了因调度节点的故障及故障恢复后来回切换的问题。
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
      10.0.0.3/24 dev eth0 label eth0:1
    }
track_script {       #触发这个监本
		  check_nginx   #监控脚本名称
}   
}
#注意主备节点都要添加 备节点如下:(备节点的脚本放置目录尽量和主节点一致)
[root@lb02 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_BACKUP 
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_nginx {
   script "/server/scripts/ng_check.sh"
   interval 2 
   weitgh   2
}

vrrp_instance VI_1 {
    state MASTER          
    interface eth0
    virtual_router_id 51
    priority 50  
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    10.0.0.3/24 dev eth0 label eth0:1 
    }
track_script {       #触发这个监本
		  check_nginx   #监控脚本名称
}   
}

  1. 重启keepalived
[root@lb01 ~]# systemctl restart keepalived
  1. 关闭主节点nginx进行测试
[root@lb01 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:e0:39:11 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.5/24 brd 10.0.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.3/24 scope global secondary eth0:1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fee0:3911/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:e0:39:1b brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.5/16 brd 172.16.255.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fee0:391b/64 scope link 
       valid_lft forever preferred_lft forever
[root@lb01 ~]# nginx -s stop
[root@lb01 ~]# nginx -s stop
[root@lb01 ~]# ip add | grep 10.0.0.3
[root@lb01 ~]# ip add | grep 10.0.0.3
###在lb02出现了
[root@lb02 ~]# ip add | grep 10.0.0.3 
    inet 10.0.0.3/24 scope global secondary eth0:1
测试成功
##在lb01上启动nginx和keepalived
[root@lb01 ~]# nginx
[root@lb01 ~]# systemctl start keepalived.service 
[root@lb01 ~]# ip add | grep 10.0.0.3
[root@lb01 ~]# ip add | grep 10.0.0.3
[root@lb01 ~]# ip add | grep 10.0.0.3
    inet 10.0.0.3/24 scope global secondary eth0:1
[root@lb01 ~]# ip add | grep 10.0.0.3
    inet 10.0.0.3/24 scope global secondary eth0:1
···






以上是关于Nginx+Keepalived负载集群高可用的主要内容,如果未能解决你的问题,请参考以下文章

nginx结合keepalived做高可用负载集群服务

Nginx +keepalived+varnish+lamp实现高可用负载均衡集群

keepalived+nginx实现高可用and负载均衡集群

Keepalived+Nginx实现高可用负载均衡集群

keepalived + nginx组建高可用负载平衡Web server集群

Keepalived+Nginx实现高可用负载均衡集群