码迷,mamicode.com
首页 > 其他好文 > 详细

keepalived双主模式高可用工作于NAT模型下的ipvs

时间:2014-09-23 10:47:05      阅读:444      评论:0      收藏:0      [点我收藏+]

标签:blank   应用程序   target   模型   拓扑图   

一、Keepalived简介

ipvs提供高可用性,能够生成ipvs规则;也可以使用脚本来帮助高可用其他应用程序,没有heartbeart重量级;主要用在高可用ipvs,及一些反向代理应用程序

 

1.拓扑图

bubuko.com,布布扣

   说明:1.两个代理服务器通过VIP向外提供数据

              2.两个代理服务器都可以代理后端的服务器

              3.为测试方便,后端服务器至提供静态页面

2.ip规划

bubuko.com,布布扣 

3.安装配置haproxy

   关于haproxy的详细配置,    

  1.  
  2. [root@node1 ~]# cat /etc/haproxy/haproxy.cfg
  3. #---------------------------------------------------------------------
  4. # Example configuration for a possible web application.  See the
  5. # full configuration options online.
  6. #
  7. #   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
  8. #
  9. #---------------------------------------------------------------------
  10. #---------------------------------------------------------------------
  11. # Global settings
  12. #---------------------------------------------------------------------
  13. global
  14. # to have these messages end up in /var/log/haproxy.log you will
  15. # need to:
  16. #
  17. # 1) configure syslog to accept network log events.  This is done
  18. #    by adding the ‘-r‘ option to the SYSLOGD_OPTIONS in
  19. #    /etc/sysconfig/syslog
  20. #
  21. # 2) configure local2 events to go to the /var/log/haproxy.log
  22. #   file. A line like the following can be added to
  23. #   /etc/sysconfig/syslog
  24. #
  25. #    local2.*                       /var/log/haproxy.log
  26. #
  27. log         127.0.0.1 local2
  28. chroot      /var/lib/haproxy
  29. pidfile     /var/run/haproxy.pid
  30. maxconn     4000
  31. user        haproxy
  32. group       haproxy
  33. daemon
  34. # turn on stats unix socket
  35. stats socket /var/lib/haproxy/stats
  36. #---------------------------------------------------------------------
  37. # common defaults that all the ‘listen‘ and ‘backend‘ sections will
  38. # use if not designated in their block
  39. #---------------------------------------------------------------------
  40. defaults
  41. mode                    http
  42. log                     global
  43. option                  httplog
  44. option                  dontlognull
  45. option http-server-close
  46. option forwardfor       except 127.0.0.0/8
  47. option                  redispatch
  48. retries                 3
  49. timeout http-request    10s
  50. timeout queue           1m
  51. timeout connect         10s
  52. timeout client          1m
  53. timeout server          1m
  54. timeout http-keep-alive 10s
  55. timeout check           10s
  56. maxconn                 3000
  57. #---------------------------------------------------------------------
  58. # main frontend which proxys to the backends
  59. #---------------------------------------------------------------------
  60. frontend  main *:80
  61. default_backend static
  62. #---------------------------------------------------------------------
  63. # static backend for serving up images, stylesheets and such
  64. #---------------------------------------------------------------------
  65. backend static
  66. balance     roundrobin
  67. server      node2 192.168.1.202:80 check maxconn 2000
  68. server      node3 192.168.1.203:80 check maxconn 2000
  69. #---------------------------------------------------------------------
  70. # round robin balancing between the various backends
  71. #---------------------------------------------------------------------

   说明:两个节点的HAproxy的配置文件应该保持一样

4.测试haproxy的配置

bubuko.com,布布扣

bubuko.com,布布扣

5.安装配置keepalived

       ①、安装keepalived

           keepalived的安装可以通过yum源来安装,也可以通过编译源码来安装,本处通过yum源赖安装

       ②、配置keepalived主节点

  1. [root@node1 ~]# cat /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {  //全局参数
  4. notification_email { //邮件
  5. sysadmin@firewall.loc //收件人地址
  6. }
  7. notification_email_from Alexandre.Cassen@firewall.loc //发件人地址
  8. smtp_server 127.0.0.1 //邮件服务器的地址
  9. smtp_connect_timeout 30 //间隔时间
  10. router_id LVS_DEVEL  //邮件服务器的组的id
  11. }
  12. vrrp_script chk_haproxy {
  13. script "killall -0 haproxy" //检查haproxy是否在线
  14. interval 1 //检查间隔时间
  15. weight -5 //如果检查失败,则权重-5
  16. }
  17. vrrp_instance VI_1 {   //定义第一个集群
  18. state MASTER      //初始状态为主节点,从节点应该为BACKUP
  19. interface eth0    //配置ip的端口
  20. virtual_router_id 51 //本组集群的id号,主从节点必须一样
  21. priority 100 //主节点的优先级,备用节点的优先级必须低于主节点
  22. advert_int 1 //心跳检查间隔时间
  23. authentication {
  24. auth_type PASS   //通信为明文密码通信
  25. auth_pass 1111 //通信的密码,主从节点必须一样
  26. }
  27. virtual_ipaddress {
  28. 192.168.1.99 //定义一个VIP
  29. }
  30. track_script {   //调用上面的命令
  31. chk_haproxy
  32. }
  33. }

   ③、配置keepalived从节点

  1. [root@node4 ~]# cat /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. notification_email {
  5. sysadmin@firewall.loc
  6. }
  7. notification_email_from Alexandre.Cassen@firewall.loc
  8. smtp_server 127.0.0.1
  9. smtp_connect_timeout 30
  10. router_id LVS_DEVEL
  11. }
  12. vrrp_script chk_haproxy {
  13. script "killall -0 haproxy"
  14. interval 1
  15. weight -5
  16. }
  17. vrrp_instance VI_1 {
  18. state BACKUP
  19. interface eth0
  20. virtual_router_id 51
  21. priority 99
  22. advert_int 1
  23. authentication {
  24. auth_type PASS
  25. auth_pass 1111
  26. }
  27. virtual_ipaddress {
  28. 192.168.1.99
  29. }
  30. track_script {
  31. chk_haproxy
  32. }
  33. }
  34. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  35. link/ether 00:0c:29:56:78:cd brd ff:ff:ff:ff:ff:ff
  36. inet 192.168.1.204/24 brd 192.168.1.255 scope global eth0
  37. inet 192.168.1.99/32 scope global eth0
  38. inet6 fe80::20c:29ff:fe56:78cd/64 scope link tentative dadfailed
  39. valid_lft forever preferred_lft forever

      可以看到ip地址已经配置上去了

bubuko.com,布布扣

   我们可以看到网页访问正常

   下面,我们模拟服务器损坏,将node1上的haproxy关掉,看看ip地址是否会转移到node4上

  1. [root@node4 ~]# ip add
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. inet 127.0.0.1/8 scope host lo
  5. inet6 ::1/128 scope host
  6. valid_lft forever preferred_lft forever
  7. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  8. link/ether 00:0c:29:56:78:cd brd ff:ff:ff:ff:ff:ff
  9. inet 192.168.1.204/24 brd 192.168.1.255 scope global eth0
  10. inet 192.168.1.99/32 scope global eth0
  11. inet6 fe80::20c:29ff:fe56:78cd/64 scope link tentative dadfailed
  12. valid_lft forever preferred_lft forever

bubuko.com,布布扣

   查看网页是否能够访问正常   

    OK!!可以看到我们的页面访问正常,这就可以实现当前段的一个反向代理服务器宕机或者后端的一个web服务宕机,服务都可以正常对外提供

     7.扩展

         我们还可以自定义通知机制

 

  1. #!/bin/bash
  2. # Author: zero<zero1@163.com>
  3. # description: An example of notify script
  4. #
  5. vip=192.168.1.99
  6. contact=‘root@localhost‘
  7. notify() {
  8. mailsubject="`hostname` to be $1: $vip floating"
  9. mailbody="`date ‘+%F %H:%M:%S‘`: vrrp transition, `hostname` changed to be $1"
  10. echo $mailbody | mail -s "$mailsubject" $contact
  11. }
  12. case "$1" in
  13. master)
  14. notify master
  15. exit 0
  16. ;;
  17. backup)
  18. notify backup
  19. exit 0
  20. ;;
  21. fault)
  22. notify fault
  23. exit 0
  24. ;;
  25. *)
  26. echo ‘Usage: `basename $0` {master|backup|fault}‘
  27. exit 1
  28. ;;
  29. esac

       在keepalived的配置文件中通过notify来调用,如下所示

  1. notify_master "/etc/keepalived/notify.sh master"
  2. notify_backup "/etc/keepalived/notify.sh backup"
  3. notify_fault "/etc/keepalived/notify.sh fault"

keepalived双主模式高可用工作于NAT模型下的ipvs

标签:blank   应用程序   target   模型   拓扑图   

原文地址:http://zerofly.blog.51cto.com/4476065/1557163

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!