操作目的

基于heartbeat v2和heartbeat-ldirectord实现LVS(DR)中Director的高可用,基于httpd提供web服务,并通过hb_gui的图形界面进行;

规划 

 

准备工作:三台主机, 分别配置如图所示的IP 和主机名

注意:1、rs1和rs2提供的不同的页面,目的是让效果明显
2 、VIP,即虚拟地址,不能被其他主机占用
3、director中的ipvsadm和vip都要确保是关闭的,让CRM来管理这些资源

一、配置LVS(DR)模型

 
   
  1. rs1   
  2. #setenforce 0   //关闭selinux
  3. #yum -y install httpd  
  4. # echo "<h1>rs1</h1>>> /var/www/html/index.html   //提供页面文件
  5. #service httpd start  
  6. # echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore   
  7. # echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore   
  8. # echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce   
  9. # echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce   
  10. # ifconfig lo:0 172.16.220.100 broadcast 172.16.220.100 netmask 255.255.255.255 up  
  11. # route add -host 172.16.220.100 dev lo:0  
  12. # elinks -dump http://172.16.220.21   //测试
  13.  rs1  
  14. # elinks -dump http://172.16.220.10  
  15.  rs1  
  16.  
  17. rs2:  
  18. #setenforce 0  
  19. # yum -y install httpd  
  20. # echo "<h1>rs2</h1>>> /var/www/html/index.html   
  21. #service httpd start  
  22. # echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore   
  23. # echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore   
  24. # echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce   
  25. # echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce   
  26. # ifconfig lo:0 172.16.220.100 broadcast 172.16.220.100 netmask 255.255.255.255 up  
  27. # route add -host 172.16.220.100 dev lo:0  
  28. # elinks -dump http://172.16.220.22  
  29.          rs2  
  30. # elinks -dump http://172.16.220.100  
  31.          rs2  
  32.  
  33. Directory :node1  
  34. #setenforce 0  
  35. # yum -y install ipvsadm   //安装ipvsam
  36. # ifconfig eth0:0 172.16.220.100 broadcast 172.16.220.100 netmask 255.255.255.255 up  
  37. # route add -host 172.16.220.100 dev eth0:0  
  38. # echo 1 > /proc/sys/net/ipv4/ip_forward  
  39.  
  40. # ipvsadm -A -t 172.16.220.100:80 -s r  
  41. # ipvsadm -a -t 172.16.220.100:80 -r 172.16.220.21 -g  
  42. # ipvsadm -a -t 172.16.220.100:80 -r 172.16.220.22 -g  
  43. # ipvsadm -ln  
  44. IP Virtual Server version 1.2.1 (size=4096)  
  45. Prot LocalAddress:Port Scheduler Flags  
  46.   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn  
  47. TCP  172.16.220.100:80 rr  
  48.   -> 172.16.220.22:80             Route   1      0          0           
  49.   -> 172.16.220.21:80             Route   1      0          0   
  50.  
  51.  测试;172.16.220.100 效果是rs1 rs2的轮询
  52. 到此,一个LVS的DR模型创建成功了。

 


二、把Director做成高可用

node1 node2 做成高可用集群中,如规划图所示

1 停止node1的相关资源

 
   
  1. node1:  
  2. #ipvsadm -S > /etc/sysconfig/ipvsadm  
  3. #service ipvsadm restart  
  4. #ipvsadm -ln  
  5. #service ipvsadm stop  
  6. #chkconfig ipvsadm off  
  7. #chkconfig --list ipvsadm  
  8. # ifconfig eth0:0 down  

 

2  把node2做成director

 
   
  1. #setenforce 0  
  2. #yum -y install ipvsadm  
  3. # ifconfig eth0:0 172.16.220.100 broadcast 172.16.220.100 netmask 255.255.255.255 up  
  4. # route add -host 172.16.200.100 dev eth0:0  
  5. # echo 1 > /proc/sys/net/ipv4/ip_forward   
  6. # ipvsadm -A -t 172.16.220.100:80 -s rr  
  7. # ipvsadm -a -t 172.16.220.100:80 -r 172.16.220.21 -g  
  8. # ipvsadm -a -t 172.16.220.100:80 -r 172.16.220.22 -g  
  9. # ipvsadm -ln   //查看定义的结果
  10. IP Virtual Server version 1.2.1 (size=4096)  
  11. Prot LocalAddress:Port Scheduler Flags  
  12.   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn  
  13. TCP  172.16.220.100:80 rr  
  14.   -> 172.16.220.22:80             Route   1      0          0           
  15.   -> 172.16.220.21:80             Route   1      0          0   
  16.  
  17. 测试:浏览器输入172.16.220.100 结果rs1 rs2轮询,工作正常
  18.  

 

3 停止node2的相关资源

 
   
  1. # ipvsadm -S > /etc/sysconfig/ipvsadm 
  2.  #service ipvsadm restart 
  3.  #ipvsadm -ln 
  4.  #service ipvsadm stop 
  5.  #chkconfig ipvsadm off 
  6.  #chkconfig --list ipvsadm  
  7. # ifconfig eth0:0 down   

4 把node1 node2做成集群

4.1 配置时间同步、ssh 互连

 
   
  1. node1:  
  2. #hwclock -s   //与系统时间一致
  3. #vim /etc/hosts   
  4. 添加  
  5. 172.16.220.11   node1  
  6. 172.16.220.12   node2  
  7.  
  8. # ssh-keygen -t rsa  //交互的方式,Enter即可
  9. Generating public/private rsa key pair.  
  10. Enter file in which to save the key (/root/.ssh/id_rsa):   
  11. Created directory '/root/.ssh'.  
  12. Enter passphrase (empty for no passphrase):   
  13. Enter same passphrase again:   
  14. Your identification has been saved in /root/.ssh/id_rsa.  
  15. Your public key has been saved in /root/.ssh/id_rsa.pub.  
  16. The key fingerprint is:  
  17. 9e:2f:7d:c7:c3:ab:cb:11:da:04:6c:4a:d6:31:29:78 [email protected]  
  18.  
  19. # ssh-copy-id -i .ssh/id_rsa.pub [email protected]  //与node2建立通信
  20. 15  
  21. The authenticity of host 'node2 (172.16.220.12)' can't be established.  
  22. RSA key fingerprint is 16:15:c4:65:45:d7:ea:c2:a7:29:4b:25:d1:ff:72:c8.  
  23. Are you sure you want to continue connecting (yes/no)? yes  
  24. Warning: Permanently added 'node2,172.16.220.12' (RSA) to the list of known hosts.  
  25. [email protected]'s password:   
  26. Now try logging into the machine, with "ssh '[email protected]'", and check in:  
  27.  
  28.   .ssh/authorized_keys  
  29.  
  30. to make sure we haven't added extra keys that you weren't expecting.  
  31.  
  32. #ssh node2 'ifconfig'              //测试 ,显示node2的"ifconfig"的相关内容
  33.  
  34. node2:  
  35. #hwclock -s
  36. #vim /etc/hosts   
  37. 添加  
  38. 172.16.220.11   node1  
  39. 172.16.220.12   node2  
  40.  
  41. # ssh-keygen -t rsa (交互中的Enter即可)  
  42. # ssh-copy-id -i .ssh/id_rsa.pub [email protected]  
  43. #ssh node1 'ifconfig'        //测试 即可
  44.  

 

4.2 在集群节点node1 node2上安装heartbeat 

需要的包有

 
   
  1. heartbeat-2.1.4-9.el5.i386.rpm  
  2.  heartbeat-pils-2.1.4-10.el5.i386.rpm   
  3. heartbeat-stonith-2.1.4-10.el5.i386.rpm   
  4. libnet-1.1.4-3.el5.i386.rpm   
  5. perl-MailTools-1.77-1.el5.noarch.rpm  
  6.  heartbeat-ldirectord-2.1.4-9.el5.i386.rpm  
  7.  heartbeat-gui-2.1.4-9.el5.i386.rpm  
 
   
  1. node1 node1安装软件包:  
  2. # yum -y --nogpgcheck localinstall heartbeat-2.1.4-9.el5.i386.rpm heartbeat-pils-2.1.4-10.el5.i386.rpm heartbeat-stonith-2.1.4-10.el5.i386.rpm libnet-1.1.4-3.el5.i386.rpm perl-MailTools-1.77-1.el5.noarch.rpm heartbeat-ldirectord-2.1.4-9.el5.i386.rpm heartbeat-gui-2.1.4-9.el5.i386.rpm   
  3.  
  4. node1:(以下的配置都是在node1上完成的,node2不需要再操作)
  5. #cp /usr/share/doc/heartbeat-2.1.4/ha.cf authkeys haresources  /etc/ha.d/  
  6. #cp /usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf /etc/ha.d/
  7. #cd /etc/ha.d/  
  8. #chmod 600 authkeys   
  9. #vim authkeys   
  10. auth 1          (dd if=/dev/urandom count=512 bs=1 | md5sum生成的随机字符串,最后一行)  
  11. 1   md5 7b1b89ead5bcc0265a8d419ef91de7f7  
  12.  
  13. # vim ha.cf   
  14. 将#bcast  eth0            # Linux启用
    改为 bcast   eth0   
  15.  
  16. 将#node   ken3
    #node   kathy
    启用并修改为:
    node    node1
    node    node2
  17.  
  18. 将#ping 10.10.10.254
    启用,并改为:
    ping 172.16.0.1
  19. compression_threshold 2
    compression     bz2
    并添加;
    crm on
  20.  
  21. #vim haresources  
  22. 添加  
  23. node1   172.16.220.100/16/eth0/172.16.220.255 httpd  
  24. #vim ldirectord.cf  
  25. 内容如下:  
  26. checktimeout=3 
  27. checkinterval=1 
  28. utoreload=yes 
  29. logfile="/var/log/ldirectord.log" 
  30. quiescent=yes 
  31. virtual=172.16.220.100:80  
  32.         real=172.16.220.21:80 gate  
  33.         real=172.16.220.22:80 gate  
  34.         fallback=127.0.0.1:80 gate  
  35.         service=http 
  36.         request=".test.html"   
  37. receive="OK"       
  38.         scheduler=rr 
  39.         protocol=tcp 
  40.         checktype=negotiate 
  41.         checkport=80 
  42. #scp -p authkeys ha.cf haresources ldirectord.cf   node2:/etc/ha.d/
  43.  
  44. (注意此时在rs1 rs2的上分别配置;echo "<h1>OK</h1>>> /var/www/html/.test.html)  
  45. #chkconfig ldirectord off  
  46. #passwd hacluster   //在此节点上修改hacluster的密码
  47. redhat  
  48. #service heartbeat start   //启动本节点服务
  49. #ssh node2 '/etc/rc.d/init.d/heartbeat start'   //启动node2节点的服务
  50. #hb_gui &   //打开图像界面配置

1)出现图形界面 Connection --Login

 

输入密码:redhat,点击OK即可
进入之后观察node1 node2都处于running状态为正常

2)添加资源

第一个资源:
Resource IP:ldrictord
Type 选中ldirecotd  点击Add 即可 资源上右击--Start 发现运行在其中一个节点上

第二个资源:

Resource IP:vip
Type 选中IPaddr
Name 下面给ip一个地址为 172.16.220.100
Add Parameter 选中lvs_support  并为true 
点击Add即可  --资源上右击--Start 运行在其中一个节点上

3)定义资源约束
Colocations--Add New Item  默认点击OK即可,配置如下


Order---Add New Item  默认点击OK即可,配置如下


4)启动资源并测试
配置后的结果如下

 

 

在node2上做测试
 

 
   
  1. #ifconfig   
  2.  
  3. eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:5B:DC:50    
  4.           inet addr:172.16.220.100  Bcast:172.16.255.255  Mask:255.255.0.0  
  5.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
  6.           Interrupt:67 Base address:0x2000   
  7. #ipvsadm -ln   
  8.  IP Virtual Server version 1.2.1 (size=4096)  
  9. Prot LocalAddress:Port Scheduler Flags  
  10.   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn  
  11. TCP  172.16.220.100:80 rr  
  12.   -> 172.16.220.21:80             Route   1      0          0           
  13.   -> 172.16.220.22:80             Route   1      0          0     
  14.  

 

最后浏览172.16.220.100 效果是rs1 rs2的轮询