Chinaunix首页 | 论坛 | 博客
  • 博客访问: 957093
  • 博文数量: 99
  • 博客积分: 3306
  • 博客等级: 中校
  • 技术积分: 1238
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-21 10:14
文章分类

全部博文(99)

文章存档

2012年(37)

2011年(56)

2010年(6)

分类: LINUX

2011-09-15 09:33:56

在rhel 5.5 X86_64下测试的。
先要安装keepalived,在rhel5.5下只能安装keepalived-1.2.1的。不能安装更新的1.2.2版本,否则一堆报错搞起来很麻烦。
  1. 先configure指定内核路径和安装的路径
  2. ./configure --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5xen-x86_64/ --prefix=/opt/keepalived/

Keepalived configuration
------------------------
Keepalived version       : 1.2.1
Compiler                 : gcc
Compiler flags           : -g -O2 -DETHERTYPE_IPV6=0x86dd
Extra Lib                : -lpopt -lssl -lcrypto
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
Use VRRP Framework       : Yes

Use Debug flags          : No

  1. 然后make && make install即可。
主机和备机上都是相同的安装。安装好后先配置好主机上的keepalived.
  1. ! Configuration File for keepalived

  2. global_defs {
  3. notification_email {
  4. email@163.com
  5. }
  6. notification_email_from lvs@163.com
  7. smtp_server 127.0.0.1
  8. smtp_connect_timeout 30
  9. router_id LVS_DEVEL
  10. }
  11. vrrp_sync_group VGM {
  12. group {
  13. test
  14. }
  15. }
  16. vrrp_instance test {
  17. state MASTER
  18. interface eth1# 这里直接使用联网的那个网卡
  19. virtual_router_id 51
  20. priority 100
  21. advert_int 1
  22. authentication {
  23. auth_type PASS
  24. auth_pass 1111
  25. }
  26. virtual_ipaddress {
  27. 192.168.3.21 dev eth0 # 如果有多余空闲的网卡可以写上,否则建议不用写。会自动绑IP在已有的网卡上
  28. }
  29. }

  30. virtual_server 192.168.3.21 80 {
  31. delay_loop 6
  32. lb_algo wlc #使用wlc调度算法
  33. lb_kind DR  #使用DR模式
  34. nat_mask 255.255.255.128
  35. # persistence_timeout 50 #同一个IP 50S内被分到一个RS上,暂时不使用
  36. protocol TCP
  37. real_server 192.168.3.14 80 {
  38. weight 100
  39. TCP_CHECK {
  40. connect_port 80
  41. connect_timeout 3
  42. nb_get_retry 3
  43. delay_before_retry 10
  44. }
  45. #如果是静态的网页,可以使用GET HTTP来检测健康情况,这时需要获取连接的hash值。
  46. #genhash -s 192.168.3.14 -p 80 -u /hb.html
  47. }
  48. real_server 192.168.3.15 80 {
  49. weight 100
  50. TCP_CHECK {
  51. connect_port 80
  52. connect_timeout 3
  53. nb_get_retry 3
  54. delay_before_retry 10
  55. }

  56. }
  57. real_server 192.168.3.16 80 {
  58. weight 100
  59. TCP_CHECK {
  60. connect_port 80
  61. connect_timeout 3
  62. nb_get_retry 3
  63. delay_before_retry 10
  64. }
  65. }
  66. }


对于备机:只需要把
  1. state MASTER改成 state BACKUP
  2. priority 100 改成 priority 99

RealServer上和之前使用相同配置,lo:0绑定VIP。
然后依次到主机和备机上启动
  1. /opt/keepalived/sbin/keepalived -f /opt/keepalived/etc/keepalived/keepalived.conf -D -d
启动好后可以同时观察主机和备机的日志
  1. tail -f /var/log/messages
Sep 15 09:59:50 free71-171-stable Keepalived_vrrp: VRRP_Instance(test) Transition to MASTER STATE
Sep 15 09:59:50 free71-171-stable Keepalived_vrrp: VRRP_Group(VGM) Syncing instances to MASTER state
Sep 15 09:59:51 free71-171-stable Keepalived_vrrp: VRRP_Instance(test) Entering MASTER STATE
Sep 15 09:59:51 free71-171-stable Keepalived_vrrp: VRRP_Instance(test) setting protocol VIPs.
Sep 15 09:59:51 free71-171-stable Keepalived_vrrp: VRRP_Instance(test) Sending gratuitous ARPs on eth0 for 192.168.3.21
Sep 15 09:59:51 free71-171-stable Keepalived_vrrp: Netlink reflector reports IP 192.168.3.21 added
Sep 15 09:59:51 free71-171-stable Keepalived_healthcheckers: Netlink reflector reports IP 192.168.3.21 added
Sep 15 09:59:56 free71-171-stable Keepalived_vrrp: VRRP_Instance(test) Sending gratuitous ARPs on eth0 for 192.168.3.21
Sep 15 10:00:36 free71-171-stable Keepalived_vrrp: VRRP_Instance(test) Received higher prio advert
Sep 15 10:00:36 free71-171-stable Keepalived_vrrp: VRRP_Instance(test) Entering BACKUP STATE
Sep 15 10:00:36 free71-171-stable Keepalived_vrrp: VRRP_Instance(test) removing protocol VIPs.
Sep 15 10:00:36 free71-171-stable Keepalived_vrrp: VRRP_Group(VGM) Syncing instances to BACKUP state
Sep 15 10:00:36 free71-171-stable Keepalived_vrrp: Netlink reflector reports IP 192.168.3.21 removed
Sep 15 10:00:36 free71-171-stable Keepalived_healthcheckers: Netlink reflector reports IP 192.168.3.21 removed
   当把主机上的keepalived 进程杀掉后,可以看到备机上日志显示其状态变为MASTER模式,然后给eth0绑 定了VIP。
    如果要测试tun模式,需要把lb_kind由“DR”修改为“TUN”。
在使用 tun模式时,可以把echo 0 >/proc/sys/net/ipv4/ip_forward,防止之前配置过DR模式时的干扰。
   最后再说一下vrrp_instance段 interface和 virtual_ipaddress后指导网卡的问题。
   假如主备机器都有eth0,eth1两个网卡。那么可以选择使用eth0来绑定VIP。
  这时配置文件中
   vrrp_instance test {
   state MASTE
   interface eth1
   这里依然为 eth1,和使用
piranha中的lvs.cf里面直接使用新增虚拟接口如eth0:0这个概念是不同的piranha中   是为某个虚拟服务器在某个虚拟网卡上绑VIP,而keepalived则应该填上负载均衡服务器自己的固有IP所在的网卡,非VIP将要绑定的网卡。
 .................
 
}
virtual_ipaddress {

192.168.3.21 dev eth0
如果有多余空闲的物理网卡可以写上,比如现在还剩下一个空闲物理网卡eth0,则直接绑定到eth0上面。 当有多个虚拟VIP时,比如还有一个192.168.3.22,直接加上。
192.168.3.22 dev eth0 !这些VIP通通都会绑定到eth0上。
                                      !如果省略掉后面的dev eth0,那么这些虚拟网卡都会直接绑定到eth1上,此时使用ifconfig无法查看是否成功绑定了,可以使用ip addr show eth0 查看。


}

如果不希望在切到备机后当主机恢复时直接切会主机(某些应用的需求),可以把2个服务器都配置为BACKUP模式,
,设置不同的优先级,然后添加nopreempt选项。
阅读(2858) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~