负载均衡技术对于一个网站尤其是大型网站的web服务器集群来说是至关重要的!做好负载均衡架构,可以实现故障转移和高可用环境,避免单点故障,保证网站健康持续运行。
由于业务扩展,网站的访问量不断加大,负载越来越高。现需要在web前端放置nginx负载均衡,同时结合keepalived对前端nginx实现HA高可用。
介绍下Nginx和keepalive
1.Nginx

Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性:
Nginx作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务器对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。
2.keepalive

Keepalived是Linux下面实现VRRP备份路由的高可靠性运行件。基于Keepalived设计的服务模式能够真正做到主服务器和备份服务器故障时IP瞬间无缝交接。二者结合,可以构架出比较稳定的软件LB方案。
Nginx+keepalive高可用方式有两种:
1.Nginx+keepalived 主从配置

这种方案,使用一个vip地址,前端使用2台机器,一台做主,一台做备,但同时只有一台机器工作,另一台备份机器在主机器不出现故障的时候,永远处于浪费状态,对于服务器不多的网站,该方案不经济实惠。
2.Nginx+keepalived 双主配置

这种方案,使用两个vip地址,前端使用2台机器,互为主备,同时有两台机器工作,当其中一台机器出现故障,两台机器的请求转移到一台机器负担,非常适合于当前架构环境。所以在这里就详细介绍下双主模型配置

一、拓扑结构

Nginx+keepalived高可用(双主模式)_第1张图片

二、测试环境介绍

系统centos7.4 64位
centos6.9 64位
前端node1服务器:DIP:192.168.92.136
VIP1:192.168.92.23
VIP2:192.168.92.24
前端node2服务器:DIP:192.168.92.133
VIP1:192.168.92.24
VIP2:192.168.92.23
后端服务器:web node3:192.168.92.123
web node4:192.168.92.124
web node5:192.168.92.125
我们开始之前先把防火墙和selinux关掉,很多时候我们服务器之间不通都是这些原因造成的。

三、软件安装

Nginx和keepalive的安装非常简单,我们可以直接使用yun来安装。
yum install keepalived nginx -y
后端服务器我们同样用yum来装上Nginx
后端node3

[root@node3 ~]# yum -y install nginx
[root@node3 ~]# echo "this is 192.168.92.123" > /usr/share/nginx/html/index.html 
[root@node3 ~]# service nginx start
[root@node3 ~]# curl 192.168.92.123
this is 192.168.92.123

后端node4

[root@node4 ~]# yum -y install nginx
[root@node4 ~]# echo "this is 192.168.92.124" > /usr/share/nginx/html/index.html 
[root@node4 ~]# service nginx start
[root@node4 ~]# curl 192.168.92.124
this is 192.168.92.124

后端node5

[root@node5 ~]# yum -y install nginx
[root@node5 ~]# echo "this is 192.168.92.125" > /usr/share/nginx/html/index.html 
[root@node5 ~]# service nginx start
[root@node5 ~]# curl 192.168.92.125
this is 192.168.92.125

四、在node1、node2上配置Nginx

[root@node2 ~]# vim /etc/nginx/conf.d/node2.conf     #在扩展配置目录中配置需要注释掉主配置文件中的server部分
upstream web1 {
        #ip_hash;     #hash绑定ip
        server 192.168.92.123:80;
        server 192.168.92.124:80;
        server 192.168.92.125:80;
    }
 server {
        listen       80;
        server_name  www.node.com;
        index index.html index.htm;
        location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://web1;
        }
      }

五、在node1上配置keepalive

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

global_defs {
   notification_email {
    root@localhost
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
   vrrp_mcast_gruop4 224.0.100.23
}
vrrp_script chk_haproxy {
    script "/etc/keepalived/chk_nginx.sh"
    interval 2
    weight 2
}
vrrp_instance VI_1 {
    state MASTER
    interface ens37
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 111123
    }
    track_script {
    chk_nginx
    }
    virtual_ipaddress {
        192.168.92.23
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface ens37
    virtual_router_id 151
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123123
    }
    track_script {
    chk_nginx
    }
    virtual_ipaddress {
        192.168.92.24
    }
}

六、在node2上配置keepalive

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

global_defs {
   notification_email {
    root@localhost
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
   vrrp_mcast_gruop4 224.0.100.23
}
vrrp_script chk_haproxy {
    script "/etc/keepalived/chk_nginx.sh"
    interval 2
    weight 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens34
    virtual_router_id 51
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 111123
    }
    track_script {
    chk_nginx
    }
    virtual_ipaddress {
        192.168.92.23
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface ens34
    virtual_router_id 151
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123123
    }
    track_script {
    chk_nginx
    }
    virtual_ipaddress {
        192.168.92.24
    }
}

七、在双主服务器上添加检测脚本

此脚本作用是检测Nginx是否运行,如果没有运行就启动Nginx
如果启动失败则停止keepalive,保证备用服务器正常运行。

[root@node2 ~]# cat /etc/keepalived/chk_nginx.sh 
#!/bin/bash
status=$(ps -C nginx --no-heading|wc -l)
if [ "${status}" = "0" ]; then
            systemctl start nginx
        status2=$(ps -C nginx --no-heading|wc -l)
        if [ "${status2}" = "0"  ]; then
                systemctl stop keepalived
        fi
fi

八、启动Nginx、keepalive服务

[root@node2 ~]# service nginx start
[root@node2 ~]# service keepalived start
[root@node3 ~]# service nginx start
[root@node3 ~]# service keepalived start

九、查看VIP并测试访问

[root@node2 ~]# ip a
..........

ens34:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ca:0b:2b brd ff:ff:ff:ff:ff:ff
    inet 192.168.92.133/24 brd 192.168.92.255 scope global dynamic ens34
       valid_lft 1293sec preferred_lft 1293sec
    inet 192.168.92.24/32 scope global ens34
       valid_lft forever preferred_lft forever
    inet6 fe80::9bff:2e2b:aebb:e35/64 scope link 
       valid_lft forever preferred_lft forever
.........
[root@node1 ~]# ip a
..........

ens37:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:04:b6:17 brd ff:ff:ff:ff:ff:ff
    inet 192.168.92.136/24 brd 192.168.92.255 scope global dynamic ens37
       valid_lft 1567sec preferred_lft 1567sec
    inet 192.168.92.23/32 scope global ens37
       valid_lft forever preferred_lft forever
    inet6 fe80::7ff4:9608:5903:1a4b/64 scope link 
       valid_lft forever preferred_lft forever
..........
[root@node1 ~]# curl http://192.168.92.23
this is 192.168.92.123
[root@node1 ~]# curl http://192.168.92.23
this is 192.168.92.124
[root@node1 ~]# curl http://192.168.92.23
this is 192.168.92.125
[root@node1 ~]# curl http://192.168.92.24
this is 192.168.92.124

十、测试脚本是否能正常运行

手动停止Nginx后自动恢复启动

[root@node1 ~]# systemctl stop nginx
[root@node1 ~]# ss -tnlp
State       Recv-Q Send-Q         Local Address:Port                        Peer Address:Port              
LISTEN      0      128                        *:80                                     *:*                   users:(("nginx",pid=20257,fd=6),("nginx",pid=20256,fd=6))
LISTEN      0      128                        *:22                                     *:*                   users:(("sshd",pid=913,fd=3))
LISTEN      0      100                127.0.0.1:25                                     *:*                   users:(("master",pid=991,fd=13))
LISTEN      0      128                       :::22                                    :::*                   users:(("sshd",pid=913,fd=4))
LISTEN      0      100                      ::1:25                                    :::*                   users:(("master",pid=991,fd=14))