Nginx 高可用配置


实验环境

Nginx IP配置:
192.168.10.10(master)
192.168.10.20(slave)

1 Nginx 安装(略)

参考文档:https://editor.csdn.net/md/?articleId=106439943

2 启动Nginx

[root@Nginx_Master ~]# cd /usr/local/nginx/sbin/
[root@Nginx_Master sbin]# ./nginx
[root@Nginx_Slave ~]# cd /usr/local/nginx/sbin/
[root@Nginx_Slave sbin]# ./nginx

2 安装keepalived

在master和slave两台Nginx上安装keepalived

[root@Nginx_Master ~]# yum install -y keepalived
[root@Nginx_Slave ~]# yum install -y keepalived

3 master keepalived配置及启动

3.1 keepalived配置文件

注意:/etc/hosts文件需要添加映射关系192.168.10.10 master

[root@Nginx_Master ~]# vim /etc/keepalived/keepalived.conf

# 全局配置
global_defs {
         notification_email {
                 acassen@firewall.loc
                 failover@firewall.loc
                 sysadmin@firewall.loc
         }
         notification_email_from Alexandre.Cassen@firewall.loc
         smtp_server 192.168.10.10
         smtp_connect_timeout 30       
         router_id master # 名称不要重复即可,通过它,可以访问到主机
}

vrrp_script chk_http_port {
         script "/usr/local/src/nginx_check.sh"  # 执行脚本所在的位置
         interval 2     #检测脚本执行的间隔,单位秒,每个2秒执行一次脚本
         weight 2
}

vrrp_instance VI_1 {
         state MASTER    # 代表主服务器
         interface eno16777728   # 绑定的网卡
         virtual_router_id 51    # 主从服务器的virtual_router_id必须相同
         priority 90    # 主从服务器取不同的优先级,主机值较大,备份机值较小
         advert_int 1   #每隔1秒发送一次心跳,确保从服务器是否还活着
         authentication {               # 心跳检测需要的密码
                 auth_type PASS
                 auth_pass Passw0rd
        }
         virtual_ipaddress {
         192.168.10.50  # 虚拟ip
         }
}

3.2 启动keepalived

[root@Nginx_Master ~]# systemctl start keepalived.service

3.3 查看启动情况

[root@Nginx_Master ~]# ps -ef | grep keepalived
root       3540      1  0 22:56 ?        00:00:00 /usr/sbin/keepalived -D
root       3541   3540  0 22:56 ?        00:00:00 /usr/sbin/keepalived -D
root       3542   3540  0 22:56 ?        00:00:00 /usr/sbin/keepalived -D
root       3569   2991  0 22:59 pts/1    00:00:00 grep --color=auto keepalived

3.4 查看启动状态

[root@Nginx_Master ~]# systemctl status keepalived
keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled)
   Active: active (running) since 四 2020-06-04 22:56:24 CST; 3min 17s ago
  Process: 3539 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 3540 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─3540 /usr/sbin/keepalived -D
           ├─3541 /usr/sbin/keepalived -D
           └─3542 /usr/sbin/keepalived -D

6月 04 22:56:26 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
6月 04 22:56:26 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
6月 04 22:56:26 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
6月 04 22:56:26 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
6月 04 22:56:31 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
6月 04 22:56:31 Nginx_Master Keepalived_vrrp[3542]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eno16777728 for 192....10.50
6月 04 22:56:31 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
6月 04 22:56:31 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
6月 04 22:56:31 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
6月 04 22:56:31 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
Hint: Some lines were ellipsized, use -l to show in full.

3.5 查看keepalived启动日志

[root@Nginx_Master ~]# tail -f /var/log/messages

Jun  4 22:56:23 Nginx_Master systemd: Stopping LVS and VRRP High Availability Monitor...
Jun  4 22:56:23 Nginx_Master Keepalived[3237]: Stopping
Jun  4 22:56:23 Nginx_Master Keepalived_vrrp[3239]: VRRP_Instance(VI_1) sent 0 priority
Jun  4 22:56:23 Nginx_Master Keepalived_vrrp[3239]: VRRP_Instance(VI_1) removing protocol VIPs.
Jun  4 22:56:23 Nginx_Master Keepalived_healthcheckers[3238]: Stopped
Jun  4 22:56:23 Nginx_Master avahi-daemon[912]: Withdrawing address record for 192.168.10.50 on eno16777728.
Jun  4 22:56:24 Nginx_Master Keepalived_vrrp[3239]: Stopped
Jun  4 22:56:24 Nginx_Master Keepalived[3237]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Jun  4 22:56:24 Nginx_Master systemd: Starting LVS and VRRP High Availability Monitor...
Jun  4 22:56:24 Nginx_Master Keepalived[3539]: Starting Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Jun  4 22:56:24 Nginx_Master Keepalived[3539]: Opening file '/etc/keepalived/keepalived.conf'.
Jun  4 22:56:24 Nginx_Master systemd: PID file /var/run/keepalived.pid not readable (yet?) after start.
Jun  4 22:56:24 Nginx_Master Keepalived[3540]: Starting Healthcheck child process, pid=3541
Jun  4 22:56:24 Nginx_Master Keepalived[3540]: Starting VRRP child process, pid=3542
Jun  4 22:56:24 Nginx_Master systemd: Started LVS and VRRP High Availability Monitor.
Jun  4 22:56:24 Nginx_Master Keepalived_healthcheckers[3541]: Opening file '/etc/keepalived/keepalived.conf'.
Jun  4 22:56:24 Nginx_Master Keepalived_vrrp[3542]: Registering Kernel netlink reflector
Jun  4 22:56:24 Nginx_Master Keepalived_vrrp[3542]: Registering Kernel netlink command channel
Jun  4 22:56:24 Nginx_Master Keepalived_vrrp[3542]: Registering gratuitous ARP shared channel
Jun  4 22:56:24 Nginx_Master Keepalived_vrrp[3542]: Opening file '/etc/keepalived/keepalived.conf'.
Jun  4 22:56:24 Nginx_Master Keepalived_vrrp[3542]: Unable to resolve default script username 'keepalived_script' - ignoring
Jun  4 22:56:24 Nginx_Master Keepalived_vrrp[3542]: VRRP_Instance(VI_1) removing protocol VIPs.
Jun  4 22:56:24 Nginx_Master Keepalived_vrrp[3542]: Using LinkWatch kernel netlink reflector...
Jun  4 22:56:24 Nginx_Master Keepalived_vrrp[3542]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Jun  4 22:56:25 Nginx_Master Keepalived_vrrp[3542]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jun  4 22:56:26 Nginx_Master Keepalived_vrrp[3542]: VRRP_Instance(VI_1) Entering MASTER STATE
Jun  4 22:56:26 Nginx_Master Keepalived_vrrp[3542]: VRRP_Instance(VI_1) setting protocol VIPs.
Jun  4 22:56:26 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
Jun  4 22:56:26 Nginx_Master Keepalived_vrrp[3542]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eno16777728 for 192.168.10.50
Jun  4 22:56:26 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
Jun  4 22:56:26 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
Jun  4 22:56:26 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
Jun  4 22:56:26 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
Jun  4 22:56:26 Nginx_Master avahi-daemon[912]: Registering new address record for 192.168.10.50 on eno16777728.IPv4.
Jun  4 22:56:31 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
Jun  4 22:56:31 Nginx_Master Keepalived_vrrp[3542]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eno16777728 for 192.168.10.50
Jun  4 22:56:31 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
Jun  4 22:56:31 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
Jun  4 22:56:31 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50
Jun  4 22:56:31 Nginx_Master Keepalived_vrrp[3542]: Sending gratuitous ARP on eno16777728 for 192.168.10.50

3.6 查看vip

主服务器绑定了虚拟IP

3.7 检测脚本路径及配置

[root@Nginx_Master ~]# vim /etc/keepalived/nginx_check.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
                killall keepalived
        fi
fi

[root@Nginx_Master ~]# chmod +x /etc/keepalived/nginx_check.sh

4 slave keepalived配置及启动

4.1 keepalived配置文件

[root@Nginx_Slave ~]# vim /etc/keepalived/keepalived.conf

# 全局配置
global_defs {
         notification_email {
                 acassen@firewall.loc
                 failover@firewall.loc
                 sysadmin@firewall.loc
         }
         notification_email_from Alexandre.Cassen@firewall.loc
         smtp_server 192.168.10.20
         smtp_connect_timeout 30
         router_id slave	# 名称不要重复即可,通过它,可以访问到主机
}

vrrp_script chk_http_port {
         script "/usr/local/src/nginx_check.sh"  # 执行脚本所在的位置
         interval 2     #检测脚本执行的间隔,单位秒,每个2秒执行一次脚本
         weight 2
}

vrrp_instance VI_1 {
         state BACKUP    # 代表从服务器
         interface eno16777728   # 绑定的网卡
         virtual_router_id 51    # 主从服务器的virtual_router_id必须相同
         priority 80    # 主从服务器取不同的优先级,主机值较大,备份机值较小
         advert_int 1   #每隔1秒发送一次心跳,确保从服务器是否还活着
         authentication {               # 心跳检测需要的密码
                 auth_type PASS
                 auth_pass passw0rd
        }
         virtual_ipaddress {
         192.168.10.50  # 虚拟ip
         }
}

4.2 启动keepalived

[root@Nginx_Slave ~]# systemctl start keepalived

4.3 查看启动情况

[root@Nginx_Slave ~]# ps -ef | grep keepalived
root       3810      1  0 23:17 ?        00:00:00 /usr/sbin/keepalived -D
root       3811   3810  0 23:17 ?        00:00:00 /usr/sbin/keepalived -D
root       3812   3810  0 23:17 ?        00:00:00 /usr/sbin/keepalived -D
root       3836   3097  0 23:19 pts/1    00:00:00 grep --color=auto keepalived

4.4 查看启动状态

[root@Nginx_Slave ~]# systemctl status keepalived
keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled)
   Active: active (running) since 四 2020-06-04 23:17:19 CST; 1min 13s ago
  Process: 3809 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 3810 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─3810 /usr/sbin/keepalived -D
           ├─3811 /usr/sbin/keepalived -D
           └─3812 /usr/sbin/keepalived -D

6月 04 23:17:19 Nginx_Slave Keepalived_healthcheckers[3811]: Opening file '/etc/keepalived/keepalived.conf'.
6月 04 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: Registering Kernel netlink reflector
6月 04 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: Registering Kernel netlink command channel
6月 04 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: Registering gratuitous ARP shared channel
6月 04 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: Opening file '/etc/keepalived/keepalived.conf'.
6月 04 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: Unable to resolve default script username 'keepalived_script' - ignoring
6月 04 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: VRRP_Instance(VI_1) removing protocol VIPs.
6月 04 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: Using LinkWatch kernel netlink reflector...
6月 04 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: VRRP_Instance(VI_1) Entering BACKUP STATE
6月 04 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]

4.5 查看keepalived启动日志

[root@Nginx_Slave ~]# tail -f /var/log/messages

Jun  4 23:17:18 Nginx_Slave systemd: Stopping LVS and VRRP High Availability Monitor...
Jun  4 23:17:18 Nginx_Slave Keepalived[3186]: Stopping
Jun  4 23:17:18 Nginx_Slave Keepalived_healthcheckers[3187]: Stopped
Jun  4 23:17:19 Nginx_Slave Keepalived_vrrp[3188]: Stopped
Jun  4 23:17:19 Nginx_Slave Keepalived[3186]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Jun  4 23:17:19 Nginx_Slave systemd: Starting LVS and VRRP High Availability Monitor...
Jun  4 23:17:19 Nginx_Slave Keepalived[3809]: Starting Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Jun  4 23:17:19 Nginx_Slave Keepalived[3809]: Opening file '/etc/keepalived/keepalived.conf'.
Jun  4 23:17:19 Nginx_Slave systemd: PID file /var/run/keepalived.pid not readable (yet?) after start.
Jun  4 23:17:19 Nginx_Slave Keepalived[3810]: Starting Healthcheck child process, pid=3811
Jun  4 23:17:19 Nginx_Slave Keepalived[3810]: Starting VRRP child process, pid=3812
Jun  4 23:17:19 Nginx_Slave systemd: Started LVS and VRRP High Availability Monitor.
Jun  4 23:17:19 Nginx_Slave Keepalived_healthcheckers[3811]: Opening file '/etc/keepalived/keepalived.conf'.
Jun  4 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: Registering Kernel netlink reflector
Jun  4 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: Registering Kernel netlink command channel
Jun  4 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: Registering gratuitous ARP shared channel
Jun  4 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: Opening file '/etc/keepalived/keepalived.conf'.
Jun  4 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: Unable to resolve default script username 'keepalived_script' - ignoring
Jun  4 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: VRRP_Instance(VI_1) removing protocol VIPs.
Jun  4 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: Using LinkWatch kernel netlink reflector...
Jun  4 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jun  4 23:17:19 Nginx_Slave Keepalived_vrrp[3812]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]

4.6 查看vip

此时从服务器还没有绑定虚拟IP

4.7 检测脚本路径及配置

[root@Nginx_Master ~]# vim /etc/keepalived/nginx_check.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
                killall keepalived
        fi
fi

[root@Nginx_Master ~]# chmod +x /etc/keepalived/nginx_check.sh

5 关闭防火墙

[root@Nginx_Master ~]# systemctl stop firewalld
[root@Nginx_Master ~]# systemctl disable firewalld
[root@Nginx_Master ~]# systemctl stop iptables
[root@Nginx_Master ~]# systemctl disable iptables

6 测试

6.1 使用虚拟IP访问

6.2 查看master虚拟IP

[root@Nginx_Master ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    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: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:9c:7d:22 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.10/24 brd 192.168.10.255 scope global eno16777728
       valid_lft forever preferred_lft forever
    inet 192.168.10.50/32 scope global eno16777728
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe9c:7d22/64 scope link
       valid_lft forever preferred_lft forever

6.3 停止master中的nginx和keepalived

[root@Nginx_Master ~]# systemctl stop keepalived.service
# 查看关闭keepalived日志
Jun  4 23:31:33 Nginx_Master systemd: Stopping LVS and VRRP High Availability Monitor...
Jun  4 23:31:33 Nginx_Master Keepalived[3540]: Stopping
Jun  4 23:31:33 Nginx_Master Keepalived_vrrp[3542]: VRRP_Instance(VI_1) sent 0 priority
Jun  4 23:31:33 Nginx_Master Keepalived_vrrp[3542]: VRRP_Instance(VI_1) removing protocol VIPs.
Jun  4 23:31:33 Nginx_Master Keepalived_healthcheckers[3541]: Stopped
Jun  4 23:31:33 Nginx_Master avahi-daemon[912]: Withdrawing address record for 192.168.10.50 on eno16777728.
Jun  4 23:31:34 Nginx_Master Keepalived_vrrp[3542]: Stopped
Jun  4 23:31:34 Nginx_Master Keepalived[3540]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Jun  4 23:31:34 Nginx_Master systemd: Stopped LVS and VRRP High Availability Monitor.

6.4 再次使用虚拟IP访问

访问正常,此时再查看slave虚拟IP绑定情况

6.5 查看slave虚拟IP绑定情况

参考:超详细的Nginx负载均衡+高可用配置(亲测附图)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值