全部博文(362)
分类: LINUX
2011-05-26 21:00:46
Server A: 192.168.1.10 (主服务器) Server B: 192.168.1.20 Virtual IP: 192.168.1.100
$ sudo apt-get install keepalived
$ sudo vim /etc/keepalived/keepalived.conf global_defs { router_id LVS_DEVEL } 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.1.100 # 虚拟IP地址,可以多个。 } }
$ sudo vim /etc/keepalived/keepalived.conf global_defs { router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } }
$ sudo service keepalived start
$ ip a 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 pfifo_fast state UNKNOWN qlen 1000 link/ether 00:0c:29:4c:e7:e7 brd ff:ff:ff:ff:ff:ff inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.100/24 scope global secondary eth0 inet6 fe80::20c:29ff:fe4c:e7e7/64 scope link valid_lft forever preferred_lft forever
$ ip a 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 pfifo_fast state UNKNOWN qlen 1000 link/ether 00:0c:29:01:d8:16 brd ff:ff:ff:ff:ff:ff inet 192.168.1.20/24 brd 192.168.1.255 scope global eth0 inet6 fe80::20c:29ff:fe01:d816/64 scope link valid_lft forever preferred_lft forever
$ curlWelcome to nginx! $ curl Welcome to nginx! 192.168.1.10
Welcome to nginx! $ curl Welcome to nginx! 192.168.1.20
Welcome to nginx! Welcome to nginx! 192.168.1.10
$ curlWelcome to nginx! Welcome to nginx! 192.168.1.20
$ ip a 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 pfifo_fast state UNKNOWN qlen 1000 link/ether 00:0c:29:01:d8:16 brd ff:ff:ff:ff:ff:ff inet 192.168.1.20/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.100/24 scope global secondary eth0 inet6 fe80::20c:29ff:fe01:d816/64 scope link valid_lft forever preferred_lft forever
Server A: 192.168.1.10, Virtual IP: 192.168.1.100 Server B: 192.168.1.20, Virtual IP: 192.168.1.200
global_defs { router_id LVS_DEVEL } 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.1.100 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 52 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.200 } }
global_defs { router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.200 } }
$ ip a 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 pfifo_fast state UNKNOWN qlen 1000 link/ether 00:0c:29:4c:e7:e7 brd ff:ff:ff:ff:ff:ff inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.100/24 scope global secondary eth0 inet6 fe80::20c:29ff:fe4c:e7e7/64 scope link valid_lft forever preferred_lft forever
$ ip a 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 pfifo_fast state UNKNOWN qlen 1000 link/ether 00:0c:29:01:d8:16 brd ff:ff:ff:ff:ff:ff inet 192.168.1.20/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.200/24 scope global secondary eth0 inet6 fe80::20c:29ff:fe01:d816/64 scope link valid_lft forever preferred_lft forever
$ curlWelcome to nginx! $ curl Welcome to nginx! 192.168.1.10
Welcome to nginx! Welcome to nginx! 192.168.1.20
$ curlWelcome to nginx! Welcome to nginx! 192.168.1.10
$ ip a 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 pfifo_fast state UNKNOWN qlen 1000 link/ether 00:0c:29:4c:e7:e7 brd ff:ff:ff:ff:ff:ff inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.100/24 scope global secondary eth0 inet 192.168.1.200/24 scope global secondary eth0 inet6 fe80::20c:29ff:fe4c:e7e7/64 scope link valid_lft forever preferred_lft forever
zhengsenlin8882011-05-27 10:37:36
自己再弄个自动重启的脚本: #!/bin/bash Thread=`ps -ef | grep "/usr/local/apache/bin/httpd" | grep -v grep` if [ -z "$Thread" ] then /usr/local/apache/bin/apachectl restart sleep 3 if [ `ps -aux | grep "/usr/local/apache/bin/httpd" | grep -v grep | wc -l` -eq 0 ] then killall keepalived fi fi