Chinaunix首页 | 论坛 | 博客
  • 博客访问: 311858
  • 博文数量: 174
  • 博客积分: 130
  • 博客等级: 入伍新兵
  • 技术积分: 587
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-12 19:39
文章分类

全部博文(174)

文章存档

2018年(2)

2016年(10)

2015年(6)

2014年(31)

2013年(92)

2012年(33)

我的朋友

分类: 系统运维

2015-11-19 16:23:32

原文地址:LVS+KEEPALIVED+NAT详解 作者:那片依然海

       相信大家对LVS + KEEPALIVED已经很熟悉了。LVSLinux Virtual ServerKEEPALIVED是为LVS设计的,主要提供了VRRP功能,解决静态路由的单点故障的问题,并且还能够检测每个服务节点的健康状态,当出现故障节点,keepalived能够剔除该节点,当故障节点回复后,又能够重新加入集群。


       本实验是LVS+KEEPALIVED +NAT模式,在企业里这种模式用的比较少,适合小的访问量。正因如此,此方面的正式文档比较少,这也是我写此博客的原因。NAT模式所有的数据量都需要通过LD,所以LD得负载比较大,为减少LD的负载,可以选择使用TUN模式。

实验架构简图如下:

LD

station1 :外网IP eth0 172.16.1.11

内网IP eth1 192.168.1.11

Station2:外网 IP eth0 172.16.1.12

内网:IP eth1 192.168.1.12

浮动IP 172.16.1.100

网关浮动IP 192.168.1.254


Realserver: Apache1 192.168.1.13

                Apache2 192.168.1.14


下面开始进行配置:

station1
打开路由功能。

安装keepalived-1.2.7


  1. yum -y install gcc make openssl-devel openssl net-snmp net-snmp-devel popt popt-devel
  2. ./configure --prefix=/usr/local/keepalived –enable-snmp
  3. make && make install
  4. ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
  5. ln -s /usr/local/keepalived/etc/keepalived.conf /etc/keepalived.conf

修改主配置文件:


  1. ! Configuration File for keepalived
  2. global_defs {
  3.     router_id KL_HOST1
  4. }
  5. vrrp_instance VI_1 {
  6.     state BACKUP
  7.     interface eth0
  8.     virtual_router_id 51
  9.     priority 150
  10.     nopreempt
  11.     advert_int 1
  12.     authentication {
  13.         auth_type PASS
  14.         auth_pass 1111
  15.     }
  16. virtual_ipaddress {
  17.     172.16.1.100/24
  18.     }
  19. }
  20. vrrp_instance VI_2 {
  21.     state BACKUP
  22.     interface eth1
  23.     nopreempt
  24.     virtual_router_id 52
  25.     priority 150
  26.     advert_int 1
  27.     authentication {
  28.         auth_type PASS
  29.         auth_pass 2222
  30. }
  31. virtual_ipaddress {
  32.     192.168.1.254/24
  33. }
  34. }
  35. vrrp_sync_group VG_1 {
  36.     group {
  37.         VI_1
  38.         VI_2
  39.     }
  40. }
  41. virtual_server 172.16.1.100 80 {
  42.     delay_loop 6
  43.     lb_algo rr
  44.     lb_kind NAT
  45.     nat_mask 255.255.255.0
  46.     persistence_timeout 50
  47.        protocol TCP
  48. # sorry_server 192.168.200.200 1358
  49. real_server 192.168.1.13 80 {
  50.     weight 1
  51.     HTTP_GET {
  52.         url {
  53.             path /urltest/test.html
  54.                digest 37dba1d9a3c103df127b4e957c9de188
  55. }
  56.     connect_timeout 3
  57.     nb_get_retry 3
  58.     delay_before_retry 3
  59.     }
  60. }
  61. real_server 192.168.1.14 80 {
  62.     weight 2
  63.     HTTP_GET {
  64.         url {
  65.             path /urltest/test.html
  66.             digest 37dba1d9a3c103df127b4e957c9de188
  67.     }
  68.     connect_timeout 3
  69.     nb_get_retry 3
  70.     delay_before_retry 3
  71.         }
  72.     }
  73. }

这里面的md5值是使用如下命令生成的。


  1. /usr/local/keepalived/bin/genhash -s 192.168.1.14 -p 80 -u /urltest/test.html

防火墙配置:


  1. [root@station1 tmp]# iptables -L -n
  2. Chain INPUT (policy ACCEPT)
  3. target prot opt source destination
  4. ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
  5. ACCEPT 112 -- 0.0.0.0/0 0.0.0.0/0
  6. ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
  7. ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
  8. ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
  9. ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
  10. REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

  11. Chain FORWARD (policy ACCEPT)
  12. target prot opt source destination

  13. Chain OUTPUT (policy ACCEPT)
  14. target prot opt source destination
  15. [root@station1 tmp]# iptables -L -n -t nat
  16. Chain PREROUTING (policy ACCEPT)
  17. target prot opt source destination

  18. Chain POSTROUTING (policy ACCEPT)
  19. target prot opt source destination
  20. MASQUERADE all -- 192.168.1.0/24 0.0.0.0/0

  21. Chain OUTPUT (policy ACCEPT)
  22. target prot opt source destination
  23. [root@station1 tmp]#

station2操作
打开路由功能

安装keepalived-1.2.7


  1. yum -y install gcc make openssl-devel openssl net-snmp net-snmp-devel popt popt-devel
  2. ./configure --prefix=/usr/local/keepalived –enable-snmp
  3. make && make install
  4. ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
  5. ln -s /usr/local/keepalived/etc/keepalived.conf /etc/keepalived.conf

修改主配置文件:


  1. ! Configuration File for keepalived

  2. global_defs {
  3.    router_id KL_HOST2
  4. }

  5. vrrp_instance VI_1 {
  6.     state BACKUP
  7.     interface eth0
  8.     virtual_router_id 51
  9.     priority 100
  10.     advert_int 1
  11.     authentication {
  12.         auth_type PASS
  13.         auth_pass 1111
  14.     }
  15.     virtual_ipaddress {
  16.         172.16.1.100/24
  17.     }
  18. }

  19. vrrp_instance VI_2 {
  20.     state BACKUP
  21.     interface eth1
  22.     virtual_router_id 52
  23.     priority 100
  24.     advert_int 1
  25.     authentication {
  26.     auth_type PASS
  27.     auth_pass 2222
  28.     }
  29.     virtual_ipaddress {
  30.     192.168.1.254/24
  31.     }
  32. }
  33. vrrp_sync_group VG_1 {
  34.     group {
  35.     VI_1
  36.     VI_2
  37.    }
  38. }

  39. virtual_server 172.16.1.100 80 {
  40.     delay_loop 6
  41.     lb_algo rr
  42.     lb_kind NAT
  43.     nat_mask 255.255.255.0
  44.     persistence_timeout 50
  45.     protocol TCP

  46. # sorry_server 192.168.200.200 1358

  47.     real_server 192.168.1.13 80 {
  48.         weight 1
  49.         HTTP_GET {
  50.             url {
  51.               path /urltest/test.html
  52.               digest 37dba1d9a3c103df127b4e957c9de188
  53.             }
  54.             connect_timeout 3
  55.             nb_get_retry 3
  56.             delay_before_retry 3
  57.         }
  58.     }

  59.     real_server 192.168.1.14 80 {
  60.         weight 2
  61.         HTTP_GET {
  62.             url {
  63.               path /urltest/test.html
  64.               digest 37dba1d9a3c103df127b4e957c9de188
  65.             }
  66.             connect_timeout 3
  67.             nb_get_retry 3
  68.             delay_before_retry 3
  69.         }
  70.     }
  71. }

station2的防火墙和station1的完全一样


apache2apache1上做如下相同配置




  1. yum -y install httpd
  2. mkdir /var/www/html/urltest/
  3. echo “this is a test page” > /var/www/html/urltest/test.html
  4. iptables -I INPUT -p tcp –dport 80 -j ACCEPT
  5. service iptables save
  6. service httpd start
  7. chkconfig httpd on

所有配置已经完成

staiton1station2上开启keepalived服务




  1. keepalived -f /etc/keepalived.conf
  2. echo “keepalived -f /etc/keepalived.conf” >> /etc/rc.local


keepalivedvrrp功能和集群功能都没有任何问题!






阅读(524) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册