DEVICE=eth2:0 TYPE=Ethernet ONBOOT=yes BOOTPROTO=static DNS1=192.168.232.2 IPADDR=192.168.232.16 NETMASK=255.255.255.0 GETWAY=192.168.232.2
eth2 Link encap:Ethernet HWaddr 00:0C:29:49:90:5B inet addr:192.168.232.135 Bcast:192.168.232.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe49:905b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:66322 errors:0 dropped:0 overruns:0 frame:0 TX packets:31860 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:67624991 (64.4 MiB) TX bytes:2723877 (2.5 MiB) Interrupt:19 Base address:0x2000 eth2:0 Link encap:Ethernet HWaddr 00:0C:29:49:90:5B inet addr:192.168.232.16 Bcast:192.168.232.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:19 Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:22622 errors:0 dropped:0 overruns:0 frame:0 TX packets:22622 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1236328 (1.1 MiB) TX bytes:1236328 (1.1 MiB)
server { listen 8088; server_name localhost; location / { root /var/www/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
<html> <head> <title>Welcome to nginx!</title> </head> <body bgcolor="white" text="black"> <center><h1>Welcome to nginx! 192.168.232.135</h1></center> </body> </html>
<html> <head> <title>Welcome to nginx!</title> </head> <body bgcolor="white" text="black"> <center><h1>Welcome to nginx! 192.168.232.134</h1></center> </body> </html>
! Configuration File for keepalived global_defs { notification_email { #[email protected] #[email protected] #[email protected] } #notification_email_from [email protected] #smtp_server 192.168.200.1 #smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_http_port { script "</dev/tcp/127.0.0.1/8088" interval 1 weight -2 } vrrp_instance VI_1 { state MASTER interface eth2 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.232.16 } track_script { chk_http_port } }
! Configuration File for keepalived global_defs { notification_email { #[email protected] #[email protected] #[email protected] } #notification_email_from [email protected] #smtp_server 192.168.200.1 #smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_http_port { script "</dev/tcp/127.0.0.1/8088" interval 1 weight -2 } vrrp_instance VI_1 { state BACKUP interface eth2 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.232.16 } track_script { chk_http_port } }
Jun 11 18:03:10 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.232.16 Jun 11 18:03:15 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.232.16 Jun 11 19:30:44 localhost Keepalived: Terminating on signal Jun 11 19:30:44 localhost Keepalived: Stopping Keepalived v1.2.2 (06/10,2014) Jun 11 19:30:44 localhost Keepalived_vrrp: Terminating VRRP child process on signal Jun 11 19:30:44 localhost Keepalived_healthcheckers: Terminating Healthchecker child process on signal
Jun 11 19:30:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs. Jun 11 19:30:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.232.16 Jun 11 19:30:50 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.232.16 added Jun 11 19:30:55 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.232.16
Jun 11 19:38:49 localhost Keepalived_vrrp: VRRP_Script(chk_http_port) failed Jun 11 19:38:51 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert Jun 11 19:38:51 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE Jun 11 19:38:51 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs. Jun 11 19:38:51 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.232.16 removed
Jun 11 19:38:52 localhost Keepalived_vrrp: VRRP_Instance(VI_1) forcing a new MASTER election Jun 11 19:38:53 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE Jun 11 19:38:54 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE Jun 11 19:38:54 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs. Jun 11 19:38:54 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.232.16 Jun 11 19:38:54 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.232.16 added Jun 11 19:38:59 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.232.16
为什么主备的参数state都是MASTER,对的你没有看错确实要都设置成一样的,不然并不能实现我们想要的VIP漂浮的效果,我测试很久才发现的.state都设置成MASTER后,会根据priority的值大小竞争来决定谁是真正的MASTER,脚本检测也是在失败的时候会把权重减去相应的值,比如原来master(181)的priority=100,如果脚本检测到端口8088无法连接,就会priority-2=98,< S-B(150)的priority(99),此时 S-B(150) 将竞争成为master,这样就实现了web应用的热备。
如果以上实验都没有问题了,那么就该nginx负载均衡的配置了,配置修改参见如下:http://blog.csdn.net/e421083458/article/details/30086413