标签:
两台主机192.168.100.128/24 192.168.100.129/24
两个单独的ip 192.168.100.133/24 192.168.100.134/24
可以将你域名解析到这两个单独的ip,keepalived会将这两个ip通过虚拟ip技术漂移到两台主机上。如果一台机器down,keepalived会将原本漂移在它身上的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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
global_defs {notification_email {1491318829@qq.com}notification_email_from keepalived@localhost.comsmtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVEL}vrrp_script chk_http_port {script "/opt/nginx_pid.sh"interval 2weight -20 #每检查一次,权重减去20}vrrp_instance VI_1 {state MASTERinterface eth1 #注意看到你本机的网络接口virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {chk_http_port}virtual_ipaddress {192.168.100.133}}vrrp_instance VI_2 {state BACKUPinterface eth1 #两处都为本机的网络接口virtual_router_id 52priority 99 #权重advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.100.134/24}} |
|
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
33
34
35
36
37
38
39
40
41
42
43
44
45
|
global_defs {notification_email {1491318829@qq.com #接收警报的email地址,可以添加多个}notification_email_from admin@caicongyang.com ###发件人地址smtp_server 127.0.0.1 ###发送邮件的服务器smtp_connect_timeout 30 ###超时时间router_id LVS_DEVEL ####load balancer 的标识 ID,用于email警报}vrrp_script chk_http_port {script "/opt/nginx_pid.sh" ####检测nginx状态的脚本路径interval 2weight -20}vrrp_instance VI_1 {state BACKUPinterface eth2virtual_router_id 51priority 99advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.100.133/24}}vrrp_instance VI_2 {state MASTERinterface eth2virtual_router_id 52priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {chk_http_port}virtual_ipaddress {192.168.100.134/24}} |
启动nginx
|
1
|
#/usr/local/nginx/sbin/nginx |
启动keepalived
|
1
|
#service keepalived start |
测试
|
1
|
#ip a |
128机器结果
|
1
2
3
4
5
6
7
8
9
10
11
12
|
1: lo: <loopback,up,lower_up> 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 forever2: eth1: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:ff:ca:24 brd ff:ff:ff:ff:ff:ff inet 192.168.100.128/24 brd 192.168.100.255 scope global eth1 inet 192.168.100.133/32 scope global eth1 inet6 fe80::20c:29ff:feff:ca24/64 scope link valid_lft forever preferred_lft forever</broadcast,multicast,up,lower_up></loopback,up,lower_up> |
129机器结果
|
1
2
3
4
5
6
7
8
9
10
11
12
|
1: lo: <loopback,up,lower_up> 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 forever2: eth2: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c0:02:92 brd ff:ff:ff:ff:ff:ff inet 192.168.100.129/24 brd 192.168.100.255 scope global eth2 inet 192.168.100.134/24 scope global secondary eth2 inet6 fe80::20c:29ff:fec0:292/64 scope link valid_lft forever preferred_lft forever</broadcast,multicast,up,lower_up></loopback,up,lower_up> |
我们可以看到ip 133已经绑定到128的机器上了,134已经绑定到129的机器上了 接下来我们可以利用在128机器killall nginx查看上篇文章中的脚本能否起作用,结果是我们可以看到nginx会不断的重启。 那我们直接关掉128的机器,或者关闭的它的网络,然后看到ip133是否能自动绑定到129的机器上呢?结果当然是可以的
|
1
2
3
4
5
6
7
8
9
10
11
12
|
1: lo: <loopback,up,lower_up> 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 forever2: eth2: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c0:02:92 brd ff:ff:ff:ff:ff:ff inet 192.168.100.129/24 brd 192.168.100.255 scope global eth2 inet 192.168.100.134/24 scope global secondary eth2 inet 192.168.100.133/24 scope global secondary eth2 inet6 fe80::20c:29ff:fec0:292/64 scope link valid_lft forever preferred_lft forever</broadcast,multicast,up,lower_up></loopback,up,lower_up> |
如同上篇文章所说的,你可以用一下命令查看整个过程
|
1
|
#tail -f /var/log/messages |
部署nginx+keepalived实现双机双主高可用性环境
标签:
原文地址:http://www.cnblogs.com/kevingrace/p/5575727.html