Nginx_Master: 192.168.1.103 提供负载均衡 Nginx_BackUp: 192.168.1.104 负载均衡备机 Nginx_VIP_TP: 192.168.1.108 网站的 VIP 地址(虚拟 IP)
原理: VIP 是外网访问的IP地址,通过 keepalived 设置,以及 VRRP 将 VIP 绑定到主机和备机上,通过权重实现控制。当主机挂掉后,keepalived 释放对主机的控制,备机接管VIP。(VIP地址直接在keepalived.conf文件内填写即可,不需要使用ifconfig命令增加)
扩展: 主机和备机可进一步延伸,它们只通过 Nginx 提供负载均衡。再让其它的机器提供真实的 web 服务。
安装 Nginx (省略) Linux–nginx源码安装/配置
Keepalived 安装 下载地址: http://www.keepalived.org/download.html
1 2 3 4 5 6 7 8 wget http:// www.keepalived.org/software/ keepalived-1.2 .7 .tar.gz tar -zxf keepalived-1.2 .7 .tar.gz cd keepalived-1.2 .7 64 位系统:./configure --sysconf=/ etc --prefix=/usr/ local/keepalived --with-kernel-dir=/u sr/src/ kernels/2.6.32-358.2.1.el6.x86_64/ 或 32 位系统: ./configure --sysconf=/ etc --prefix=/usr/ local/keepalived --with-kernel-dir=/u sr/src/ kernels/2.6.32-358.6.2.el6.i686/
可通过 getconf LONG_BIT 得到系统位数。
参数解释:--sysconf
指定了配置文件的地址.即:/etc/keepalived/keepalived.conf--prefix
指定了安装目录--with-kernel-dir
指定使用内核源码中的头文件,即 include 目录。只有使用 LVS 时才需要这个参数,其它的时候不需要。
如果报错: configure: error: Popt libraries is required 解决:yum install popt-devel
再configue .
成功后提示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Keepalived configuration ------------------------ Keepalived version : 1.2 .7 Compiler : gcc Compiler flags : -g -O2 Extra Lib : -lpopt -lssl -lcrypto Use IPVS Framework : Yes IPVS sync daemon support : Yes IPVS use libnl : No Use VRRP Framework : Yes Use VRRP VMAC : Yes SNMP support : No Use Debug flags : No 安装: make make install
设置成为服务并开机启动: 1 2 3 4 5 cp /usr/ local/keepalived/ sbin/keepalived /u sr/sbin/ /etc/ rc.d/init.d/ keepalived statuschkconfig --add keepalived chkconfig keepalived on
(注:也可使用yum命令安装:yum -y install keepalived
)
设置主机上的配置文件内容: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { sunyu@easymobi.cn wuxuegang.123 @163 .com } notification_email_from pub@easymobi.cn 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 mcast_src_ip 192.168 .1.103 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111222 } virtual_ipaddress { 192.168 .1.108 } }
前面的结构那里已经规定好了 VIP 和 主备机的 IP, 所以这里按上面的填。
备机的配置文件: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ! Configuration File for keepalived global_defs { notification_email { sunyu@easymobi.cn wuxuegang.123 @163 .com } notification_email_from pub@easymobi.cn smtp_server 127.0 .0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state SLAVER interface eth0 virtual_router_id 51 mcast_src_ip 192.168 .1.104 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111222 } virtual_ipaddress { 192.168 .1.108 } }
这时候 ping 192.168.1.108 是不通的。(看到这句话我才知道是不需要ifconfig命令添加虚拟ip的,如果自己添加了VIP,那么即使没有启动keepalived服务,也能ping通)
然后在两台机器上分别启动 keepalived 服务
这时候再ping 192.168.1.108
通了。 实际上这时候 108 是被绑到主机上的。在主机上查看系统日志:
1 2 3 4 5 6 7 8 9 10 11 tailf /var/log/messagesMay 29 18 :32 :16 localhost Keepalived_vrrp[27731 ]: Opening file '/etc/keepalived/keepalived.conf'.May 29 18 :32 :16 localhost Keepalived_vrrp[27731 ]: Configuration is using : 62906 BytesMay 29 18 :32 :16 localhost Keepalived_vrrp[27731 ]: Using LinkWatch kernel netlink reflector...May 29 18 :32 :16 localhost Keepalived_healthcheckers[27729 ]: Using LinkWatch kernel netlink reflector...May 29 18 :32 :16 localhost Keepalived_vrrp[27731 ]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)] May 29 18 :32 :17 localhost Keepalived_vrrp[27731 ]: VRRP_Instance(VI_1) Transition to MASTER STATEMay 29 18 :32 :18 localhost Keepalived_vrrp[27731 ]: VRRP_Instance(VI_1) Entering MASTER STATEMay 29 18 :32 :18 localhost Keepalived_vrrp[27731 ]: VRRP_Instance(VI_1) setting protocol VIPs.May 29 18 :32 :18 localhost Keepalived_vrrp[27731 ]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.108 May 29 18 :32 :18 localhost Keepalived_healthcheckers[27729 ]: Netlink reflector reports IP 192.168.1.108 added
可以看到.VRRP(虚拟路由冗余协议)已经启动.我们可以通过命令 ip addr 来检查主 Nginx 上的 IP 分配情况.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@localhost ~] 1 : lo: mtu 16436 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 inet6 ::1 /128 scope host valid_lft forever preferred_lft forever 2 : eth0: mtu 1500 qdisc mq state UP qlen 1000 link/ether 00 :15 :c5:ef:53 :8 c brd ff:ff:ff:ff:ff:ff inet 192.168 .1.103 /25 brd 192.168 .1.255 scope global eth0 inet 192.168 .1.108 /32 scope global eth0 inet6 fe80::215 :c5ff:feef:538 c/64 scope link valid_lft forever preferred_lft forever 3 : eth1: mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 00 :15 :c5:ef:53 :8 e brd ff:ff:ff:ff:ff:ff
可以看到 VIP 地址已经绑定到主 Nginx 机器上: inet 192.168.1.108/32 scope global eth0
我们通过 tcpdump 抓包:
1 2 3 4 5 6 7 8 9 10 11 [root@localhost ~]# tcpdump vrrp tcpdump : verbose output suppressed, use -v or -vv for full protocol decodelistening on eth0, link-type EN10MB (Ethernet ), capture size 65535 bytes13 :38 :27.797982 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2 , Advertisement , vrid 51 , prio 100 , authtype simple, intvl 1s, length 2013 :38 :28.794693 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2 , Advertisement , vrid 51 , prio 100 , authtype simple, intvl 1s, length 2013 :38 :29.794518 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2 , Advertisement , vrid 51 , prio 100 , authtype simple, intvl 1s, length 2013 :38 :30.798581 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2 , Advertisement , vrid 51 , prio 100 , authtype simple, intvl 1s, length 2013 :38 :31.795902 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2 , Advertisement , vrid 51 , prio 100 , authtype simple, intvl 1s, length 2013 :38 :32.804050 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2 , Advertisement , vrid 51 , prio 100 , authtype simple, intvl 1s, length 2013 :38 :33.801191 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2 , Advertisement , vrid 51 , prio 100 , authtype simple, intvl 1s, length 2013 :38 :34.798793 IP htuidc.bgp.ip > vrrp.mcast.net: VRRPv2 , Advertisement , vrid 51 , prio 100 , authtype simple, intvl 1s, length 20
这样,一个 Nginx + Keepalived 的架构就完成了。
监控和主备切换 接下来可以完善一下,加上实时监控,如果发现负载均衡的 Nginx 出现问题,就将该机器上的 Keepalived 服务停掉。 nginx_check.sh:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #!/bin/bash while : do nginxpid=`ps -C nginx --no-header | wc -l` if [ $nginxpid -eq 0 ];then service nginx start sleep 3 nginxpid=`ps -C nginx --no-header | wc -l` echo $nginxpid if [ $nginxpid -eq 0 ];then service keepalived stop fi fi sleep 3 done
然后让该脚本一直在后台运行:nohup /etc/nginx_check.sh
或者将它添加成服务,让它开机自启动:CentOS 开机自启动脚本
测试: 在两台机器的 web 服务器上分别放一个 index.html, 里面内容分别是自己机器的IP. 通过VIP访问:http://192.168.1.108/index.html 发现显示的是主机的IP. 此时,关掉主机的 nginx, 这时候由于上面的监控脚本。主机的 keepalived 也会关闭。这时候再访问上面地址,发现显示的是备机的IP。可见,切换成功。
负载均衡 Linux – Nginx 负载均衡和反向代理 通过 ip_hash 实现会话保持
如果Keepalived故障转移会失效,尝试禁用iptables
执行service iptables stop
关闭iptables服务
执行chkconfig iptables off
取消iptables服务自动启动