Chinaunix首页 | 论坛 | 博客
  • 博客访问: 55504
  • 博文数量: 8
  • 博客积分: 218
  • 博客等级: 入伍新兵
  • 技术积分: 95
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-09 11:20
文章分类

全部博文(8)

文章存档

2014年(1)

2012年(7)

我的朋友

分类: LINUX

2012-05-23 16:30:04

这个比我前面写的那个要好些(前面那个是按照网上的配置写的,不太专业)
需要安装的软件:
LVS主从节点上:
keepalived
ipvsadm
RealServer上需要安装的软件:
arptables_jf
另外,执行自动化配置脚本的机子,需要expect软件支持,参见脚本中的注意事项。
 
这个脚本是在我手动配置成功后编写的,还没测试过,不过应该问题不大,有问题麻烦帮忙指正。
 
自动化脚本:

点击(此处)折叠或打开

  1. #!/bin/sh
  2. #文 件 名: autogen_lvs.sh
  3. #功 能: 自动生成LVS,Keepalived配置文件,生成环境自动部署,自动运行脚本等
  4. #创建时间: 2012-02-05
  5. #修改时间: 2012-06-01
  6. #自动化生成的文件列表:
  7. #(执行autogen_lvs.sh后,会生成autogen目录,里面会存放自动生成的相关文件)
  8. #1. ConfigAllServer.sh 配置相关服务器集群功能脚本,包括负载均衡主服务器,负载均衡备服务器,真实服务器
  9. #2. RunAllServer.sh 开启所有Server集群功能,包括负载均衡主备服务器,真实服务器
  10. #3. StopAllServer.sh 关闭所有Server集群功能,包括负载均衡主备服务器,真实服务器
  11. #4. LVSServer.sh 开启或关闭负载均衡器集群功能,该文件会被ConfigAllServer.sh部署到负载主备服务器的/etc/ha.d/下,供后续使用
  12. #5. RealServer.sh 开启或关闭真实服务器集群功能,该文件会被ConfigAllServer.sh部署到真实服务器的/etc/下,供后续使用
  13. #6. keepalvied_master.conf 文件会被ConfigAllServer.sh部署到负载主服务器的/etc/keepalived/下,并重命名为keepalived.conf,作为HA配置
  14. #7. keepalvied_backup.conf 文件会被ConfigAllServer.sh部署到负载备服务器的/etc/keepalived/下,并重命名为keepalived.conf,作为HA配置

  15. # -------------------------------------------------以下根据实际情况进行设定----------------------------------------------------
  16. # ROOT权限密码,该密码为所有负载均衡服务器,真实服务器的通用密码,所有服务器ROOT将使用统一密码,便于自动化控制,否则上面提到的ConfigAllServer.sh, RunAllServer.sh, StopAllServer.sh这三个文件无法正常运行
  17. ROOTPASSWORD=123456
  18. # 负载均衡主服务器IP地址
  19. LVS_SERVER_IP=172.16.0.40
  20. # 负载均衡备服务器IP地址
  21. LVS_BACKUP_SERVER_IP=1.1.1.1
  22. # 虚拟IP地址,对外提供服务的地址
  23. VIP=172.16.0.252
  24. # 广播地址
  25. BROADCAST=172.16.0.255
  26. # 真实服务器数组,数据之间用空格隔开
  27. # 如'10.0.0.1 10.0.0.2' 表示10.0.0.1和10.0.0.2
  28. # 如'10 10.0.0.1' 表示10.0.0.1, 10.0.0.2, ..., 10.0.0.10
  29. RS_IPS='172.16.0.43 172.16.0.44'
  30. # 真实服务器业务对外提供服务的端口数组,数据之间用空格隔开,'80 81'
  31. RS_PORTS='8447 8888'
  32. # 网卡设备号,如eth0,eth1,单网卡一般都为eth0
  33. DEVICE=eth0
  34. #--------------------------------------------------------------------------------------------------------------------------------

  35. rm -fr ./autogen_lvs
  36. mkdir ./autogen_lvs

  37. first_string=`echo $RS_IPS | cut -d " " -f1`
  38. if [[ `echo $first_string | grep "\." | wc -l` = "0" ]];then
  39.     IP_NUM=$first_string
  40.     IP_START=`echo $RS_IPS | cut -d " " -f2 | awk 'BEGIN{ FS="." } { printf "%s.%s.%s.", $1, $2, $3}' `
  41.     OFFSET=`echo $RS_IPS | cut -d " " -f2 | cut -d "." -f4`
  42.     RS_IPS=
  43.     for((i=0;i<$IP_NUM;i++));do
  44.         RS_IPS=$RS_IPS" $IP_START`expr $OFFSET + $i`"
  45.     done
  46. fi

  47. #Create LVSServer.sh
  48. cat > ./autogen_lvs/LVSServer.sh << end
  49. #!/bin/sh

  50. RS_PORTS="$RS_PORTS"

  51. for port in \$RS_PORTS;do
  52.     iptables -I INPUT 1 -p tcp --dport \$port -j ACCEPT
  53. done

  54. case "\$1" in
  55.     start)
  56.         /etc/init.d/keepalived start
  57.         ;;
  58.     stop)
  59.         /etc/init.d/keepalived stop
  60.         ;;
  61.     restart)
  62.         /etc/init.d/keepalived restart
  63.         ;;
  64.     *)
  65.         echo "Usage: \$0 {start|stop|restart}"
  66.         exit 1
  67. esac

  68. if ! grep LVSServer /etc/rc.local > /dev/null
  69. then
  70.         echo "sh /etc/LVSServer.sh start" >> /etc/rc.local
  71. fi

  72. end

  73. #Create RealServer.sh
  74. cat > ./autogen_lvs/RealServer.sh << end
  75. #!/bin/sh

  76. VIP="$VIP"
  77. RS_PORTS="$RS_PORTS"

  78. RIP=\`ifconfig | sed -n "/$DEVICE/{
  79. N
  80. s/^$DEVICE.*inet addr:\(.*\) Bcast.*$/\1/p
  81. q
  82. }
  83. "\`

  84. if [ -z "\`rpm -qa | grep arptables_jf\`" ];then
  85.         echo "You need install the software of arptables_jf first"
  86.         exit
  87. fi

  88. if [ ! -e "/etc/sysconfig/arptables" ];then
  89.         service arptables_jf save
  90. fi

  91. #add vip on $DEVICE
  92. ip addr add \$VIP dev $DEVICE

  93. #drop arp package to vip
  94. service arptables_jf start
  95. arptables -A IN -d \$VIP -j DROP
  96. arptables -A OUT -s \$VIP -j mangle --mangle-ip-s \$RIP

  97. service iptables start
  98. for port in \$RS_PORTS;do
  99.     iptables -I INPUT 1 -p tcp --dport \$port -j ACCEPT
  100.     iptables -t nat -A PREROUTING -p tcp -d \$VIP --dport \$port -j REDIRECT
  101. done

  102. if ! grep RealServer /etc/rc.local > /dev/null
  103. then
  104.         echo "sh /etc/RealServer.sh start" >> /etc/rc.local
  105. fi
  106. end

  107. VIRTUAL_SERVER_LIST=
  108. for rs_port in $RS_PORTS;do
  109.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST"virtual_server $VIP $rs_port {"$'\n'
  110.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"delay_loop 6"$'\n'
  111.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"lb_algo wlc"$'\n'
  112.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"lb_kind DR"$'\n'
  113.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"#persistence_timeout 60"$'\n'
  114.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"protocol TCP"$'\n'
  115.     for rs_ip in $RS_IPS;do
  116.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"real_server $rs_ip $rs_port {"$'\n'
  117.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'"weight 1"$'\n'
  118.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'"TCP_CHECK {"$'\n'
  119.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'$'\t'"connect_timeout 10"$'\n'
  120.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'$'\t'"nb_get_retry 3"$'\n'
  121.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'$'\t'"delay_before_retry 3"$'\n'
  122.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'$'\t'"connect_port $rs_port"$'\n'
  123.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'"}"$'\n'
  124.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"}"$'\n'
  125.     done
  126.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST"}"$'\n'$'\n'
  127. done

  128. #Create keepalived_master.conf
  129. cat > ./autogen_lvs/keepalived_master.conf << end
  130. ! Configuration File for keepalived

  131. global_defs {
  132.    router_id LVS_DEVEL
  133. }

  134. vrrp_instance VI_1 {
  135.     state MASTER
  136.     interface $DEVICE
  137.     virtual_router_id 51
  138.     priority 100
  139.     advert_int 1
  140.     authentication {
  141.         auth_type PASS
  142.         auth_pass 1111
  143.     }
  144.     virtual_ipaddress {
  145.         $VIP
  146.     }
  147. }

  148. $VIRTUAL_SERVER_LIST
  149. end

  150. #Create keepalived_backup.conf
  151. sed -e 's/state MASTER/state BACKUP/' -e 's/priority 100/priority 99/' ./autogen_lvs/keepalived_master.conf > ./autogen_lvs/keepalived_backup.conf

  152. #Create ConfigAllServer.sh
  153. cat > ./autogen_lvs/ConfigAllServer.sh << end
  154. #!/bin/sh

  155. RS_IPS="$RS_IPS"
  156. ROOTPASSWORD="$ROOTPASSWORD"
  157. LVS_SERVER_IP="$LVS_SERVER_IP"
  158. LVS_BACKUP_SERVER_IP="$LVS_BACKUP_SERVER_IP"

  159. function test_host_online() {
  160.     if [[ \` ping \$1 -c 1 -w 1 | grep "1 received" | wc -l \` = "1" ]];then
  161.         return 0
  162.     else
  163.         return 1
  164.     fi
  165. }

  166. sed -i 's/^# StrictHostKeyChecking ask$/StrictHostKeyChecking no/' /etc/ssh/ssh_config

  167. if test_host_online \$LVS_SERVER_IP;then
  168. /usr/bin/expect << endexpect
  169.     set timeout 100
  170.     spawn scp LVSServer.sh root@\$LVS_SERVER_IP:/etc/
  171.     expect "*password*"
  172.     send "\$ROOTPASSWORD\r"
  173.     expect eof

  174.     spawn scp keepalived_master.conf root@\$LVS_SERVER_IP:/etc/keepalived/keepalived.conf
  175.     expect "*password*"
  176.     send "\$ROOTPASSWORD\r"
  177.     expect eof
  178. endexpect
  179. fi

  180. if test_host_online \$LVS_BACKUP_SERVER_IP;then
  181. /usr/bin/expect << endexpect
  182.     set timeout 100
  183.     spawn scp LVSServer.sh root@\$LVS_BACKUP_SERVER_IP:/etc/
  184.     expect "*password*"
  185.     send "\$ROOTPASSWORD\r"
  186.     expect eof

  187.     spawn scp keepalived_backup.conf root@\$LVS_BACKUP_SERVER_IP:/etc/keepalived/keepalived.conf
  188.     expect "*password*"
  189.     send "\$ROOTPASSWORD\r"
  190.     expect eof
  191. endexpect
  192. fi

  193. for rs_ip in \$RS_IPS;do
  194.     if test_host_online \$rs_ip;then
  195. /usr/bin/expect << endexpect
  196.         set timeout 100
  197.         spawn scp RealServer.sh root@\$rs_ip:/etc/
  198.         expect "*password*"
  199.         send "\$ROOTPASSWORD\r"
  200.         expect eof
  201. endexpect
  202.     fi
  203. done
  204. end

  205. #Create RunAllServer.sh
  206. cat > ./autogen_lvs/RunAllServer.sh << end
  207. #!/bin/sh

  208. LVS_SERVER_IP="$LVS_SERVER_IP"
  209. ROOTPASSWORD="$ROOTPASSWORD"
  210. LVS_BACKUP_SERVER_IP="$LVS_BACKUP_SERVER_IP"
  211. RS_IPS="$RS_IPS"

  212. function test_host_online() {
  213.     if [[ \` ping \$1 -c 1 -w 1 | grep "1 received" | wc -l \` = "1" ]];then
  214.         return 0
  215.     else
  216.         return 1
  217.     fi
  218. }

  219. sed -i 's/^# StrictHostKeyChecking ask$/StrictHostKeyChecking no/' /etc/ssh/ssh_config

  220. if test_host_online \$LVS_SERVER_IP;then
  221. /usr/bin/expect << endexpect
  222.     set timeout 100
  223.     spawn ssh root@\$LVS_SERVER_IP
  224.     expect "*password*"
  225.     send "\$ROOTPASSWORD\r"
  226.     expect "#"
  227.     send "sh /etc/LVSServer.sh start\r"
  228.     expect "#"
  229.     send "exit\r"
  230. endexpect
  231. fi

  232. if test_host_online \$LVS_BACKUP_SERVER_IP;then
  233. /usr/bin/expect << endexpect
  234.     set timeout 100
  235.     spawn ssh root@\$LVS_BACKUP_SERVER_IP
  236.     expect "*password*"
  237.     send "\$ROOTPASSWORD\r"
  238.     expect "#"
  239.     send "sh /etc/LVSServer.sh start\r"
  240.     expect "#"
  241.     send "exit\r"
  242. endexpect
  243. fi

  244. for rs_ip in \$RS_IPS;do
  245.     if test_host_online \$rs_ip;then
  246. /usr/bin/expect << endexpect
  247.         set timeout 100
  248.         spawn ssh root@\$rs_ip
  249.         expect "*password*"
  250.         send "\$ROOTPASSWORD\r"
  251.         expect "#"
  252.         send "sh /etc/RealServer.sh start\r"
  253.         expect "#"
  254.         send "exit\r"
  255. endexpect
  256.     fi
  257. done

  258. end

  259. #Create StopAllServer.sh
  260. sed 's/start/stop/' ./autogen_lvs/RunAllServer.sh > ./autogen_lvs/StopAllServer.sh


阅读(2454) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

十七岁的回忆2012-05-24 10:21:08

恩,先分享下来,可能用得着呢~~