nginx+keepalived实现双机双主高可用性

1.前言

上篇文章我们配置的双机热备,意思是说两台机器一台处于运行中,另一台永久处于备份。这样的话资源浪费严重,适用于国企,政府类系统。今天我们所讲的是双机双主热备,意味着两台机器同时处于工作状态,两台主机又同时为备份状态。

2.准备:

两台主机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,漂移到另一台机器上,这样就实现了双机双主互为主机和备份。

3.安装

安装可以查看我的上篇文章:nginx+keepalived实现双机热备高可用性 http://blog.csdn.net/caicongyang/article/details/46444007

4.配置

128机器 keepalived.conf配置文件

[plain]  view plain  copy
  1. global_defs {  
  2. notification_email {  
  3. [email protected]  
  4. }  
  5. notification_email_from [email protected]  
  6. smtp_server 127.0.0.1  
  7. smtp_connect_timeout 30  
  8. router_id LVS_DEVEL  
  9. }  
  10. vrrp_script chk_http_port {  
  11. script "/opt/nginx_pid.sh"  
  12. interval 2  
  13. weight -20 #每检查一次,权重减去20  
  14. }  
  15. vrrp_instance VI_1 {  
  16. state MASTER  
  17. interface eth1 #注意看到你本机的网络接口  
  18. virtual_router_id 51  
  19. priority 100  
  20. advert_int 1  
  21. authentication {  
  22. auth_type PASS  
  23. auth_pass 1111  
  24. }  
  25. track_script {  
  26. chk_http_port  
  27. }  
  28. virtual_ipaddress {  
  29. 192.168.100.133  
  30. }  
  31. }  
  32. vrrp_instance VI_2 {  
  33. state BACKUP  
  34. interface eth1 #两处都为本机的网络接口  
  35. virtual_router_id 52  
  36. priority 99 #权重  
  37. advert_int 1  
  38. authentication {  
  39. auth_type PASS  
  40. auth_pass 1111  
  41. }  
  42. virtual_ipaddress {  
  43. 192.168.100.134/24  
  44. }  
  45. }  

129机器 keepalived.conf配置文件

[plain]  view plain  copy
  1. global_defs {  
  2. notification_email {  
  3. [email protected]               #接收警报的email地址,可以添加多个  
  4. }  
  5. notification_email_from [email protected]   ###发件人地址  
  6. smtp_server 127.0.0.1          ###发送邮件的服务器  
  7. smtp_connect_timeout 30      ###超时时间  
  8. router_id LVS_DEVEL      ####load balancer 的标识 ID,用于email警报  
  9. }  
  10. vrrp_script chk_http_port {  
  11. script "/opt/nginx_pid.sh" ####检测nginx状态的脚本路径  
  12. interval 2  
  13. weight -20  
  14. }  
  15. vrrp_instance VI_1 {  
  16. state BACKUP  
  17. interface eth2  
  18. virtual_router_id 51  
  19. priority 99  
  20. advert_int 1  
  21. authentication {  
  22. auth_type PASS  
  23. auth_pass 1111  
  24. }  
  25. virtual_ipaddress {  
  26. 192.168.100.133/24  
  27. }  
  28. }  
  29. vrrp_instance VI_2 {  
  30. state MASTER  
  31. interface eth2  
  32. virtual_router_id 52  
  33. priority 100  
  34. advert_int 1  
  35. authentication {  
  36. auth_type PASS  
  37. auth_pass 1111  
  38. }  
  39. track_script {  
  40. chk_http_port  
  41. }  
  42. virtual_ipaddress {  
  43. 192.168.100.134/24  
  44. }  
  45. }  

5.启动和测试

启动nginx
[plain]  view plain  copy
  1. #/usr/local/nginx/sbin/nginx  
启动keepalived
[plain]  view plain  copy
  1. #service keepalived start  

测试
[plain]  view plain  copy
  1. #ip a  

128机器结果
[plain]  view plain  copy
  1. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN   
  2.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00  
  3.     inet 127.0.0.1/8 scope host lo  
  4.     inet6 ::1/128 scope host   
  5.        valid_lft forever preferred_lft forever  
  6. 2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000  
  7.     link/ether 00:0c:29:ff:ca:24 brd ff:ff:ff:ff:ff:ff  
  8.     inet 192.168.100.128/24 brd 192.168.100.255 scope global eth1  
  9.     inet 192.168.100.133/32 scope global eth1  
  10.     inet6 fe80::20c:29ff:feff:ca24/64 scope link   
  11.        valid_lft forever preferred_lft forever  

129机器结果
[plain]  view plain  copy
  1. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN   
  2.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00  
  3.     inet 127.0.0.1/8 scope host lo  
  4.     inet6 ::1/128 scope host   
  5.        valid_lft forever preferred_lft forever  
  6. 2: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000  
  7.     link/ether 00:0c:29:c0:02:92 brd ff:ff:ff:ff:ff:ff  
  8.     inet 192.168.100.129/24 brd 192.168.100.255 scope global eth2  
  9.     inet 192.168.100.134/24 scope global secondary eth2  
  10.     inet6 fe80::20c:29ff:fec0:292/64 scope link   
  11.        valid_lft forever preferred_lft forever  

我们可以看到ip 133已经绑定到128的机器上了,134已经绑定到129的机器上了
接下来我们可以利用在128机器killall nginx查看上篇文章中的脚本能否起作用,结果是我们可以看到nginx会不断的重启。
那我们直接关掉128的机器,或者关闭的它的网络,然后看到ip133是否能自动绑定到129的机器上呢?结果当然是可以的
[plain]  view plain  copy
  1. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN   
  2.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00  
  3.     inet 127.0.0.1/8 scope host lo  
  4.     inet6 ::1/128 scope host   
  5.        valid_lft forever preferred_lft forever  
  6. 2: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000  
  7.     link/ether 00:0c:29:c0:02:92 brd ff:ff:ff:ff:ff:ff  
  8.     inet 192.168.100.129/24 brd 192.168.100.255 scope global eth2  
  9.     inet 192.168.100.134/24 scope global secondary eth2  
  10.     inet 192.168.100.133/24 scope global secondary eth2  
  11.     inet6 fe80::20c:29ff:fec0:292/64 scope link   
  12.        valid_lft forever preferred_lft forever  

如同上篇文章所说的,你可以用一下命令查看整个过程
[plain]  view plain  copy
  1. #tail -f /var/log/messages    


后记:这两边nginx+keepalied高可用性的文章中,我们配置了notification_email但是都没用到,楼主将在下篇文章配置smtp服务,让其生效,这样就能在服务发生异常的情况下,发送邮件提醒运维人员。当然博主我也会在后期发布免费网络监视工具Nagios的相关文章,将其整合进来,具体利弊后期分析。


不懂运维的程序员,不是好的工程师!

猜你喜欢

转载自blog.csdn.net/dingyang0315/article/details/79107176
今日推荐