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

全部博文(8)

文章存档

2014年(1)

2012年(7)

我的朋友

分类: LINUX

2012-05-08 10:18:53

这个是按照目前网上的配置方法写的,但看过Redhat的配置说明后,发现这个不是很好,建议各位去看我的另一篇文章,这篇文章暂时保留在这里,毕竟是辛苦弄出来的。另一篇:http://blog.chinaunix.net/uid-26683919-id-3219775.html
 

点击(此处)折叠或打开

  1. #!/bin/sh
  2. #文 件 名: autogen_lvs.sh
  3. #功 能: 自动生成LVS,Keepalived配置文件,生成环境自动部署,自动运行脚本等
  4. #创建时间: 2012-02-05
  5. #自动化生成的文件列表:
  6. #(执行该脚本后,会生成autogen_lvs目录,里面会存放自动生成的相关文件)
  7. #1. ConfigAllServer.sh 配置相关服务器集群功能脚本,包括负载均衡主服务器,负载均衡备服务器,真实服务器
  8. #2. RunAllServer.sh 开启所有Server集群功能,包括负载均衡主备服务器,真实服务器
  9. #3. StopAllServer.sh 关闭所有Server集群功能,包括负载均衡主备服务器,真实服务器
  10. #4. LVSServer.sh 开启或关闭负载均衡器集群功能,该文件会被ConfigAllServer.sh部署到负载主备服务器的/etc/ha.d/下,供后续使用
  11. #5. RealServer.sh 开启或关闭真实服务器集群功能,该文件会被ConfigAllServer.sh部署到真实服务器的/etc/下,供后续使用
  12. #6. keepalvied_master.conf 文件会被ConfigAllServer.sh部署到负载主服务器的/etc/keepalived/下,并重命名为keepalived.conf,作为HA配置
  13. #7. keepalvied_backup.conf 文件会被ConfigAllServer.sh部署到负载备服务器的/etc/keepalived/下,并重命名为keepalived.conf,作为HA配置
  14. #注意事项
  15. #1. 执行ConfigAllServer.sh, RunAllServer.sh, StopAllServer.sh这三个脚本的主机需要修改/etc/ssh/ssh_config,将StrictHostKeyChecking设置为no,防止弹出提示框引起自动化无法执行完毕
  16. #2. 执行该脚本的机子上需要安装expect
  17. #3. lvs和keepalived软件需要预先安装好

  18. # -------------------------------------------------以下根据实际情况进行设定----------------------------------------------------
  19. # ROOT权限密码,该密码为所有负载均衡服务器,真实服务器的通用密码,所有服务器ROOT将使用统一密码,便于自动化控制,否则上面提到的ConfigAllServer.sh, RunAllServer.sh, StopAllServer.sh这三个文件无法正常运行
  20. ROOTPASSWORD=123qwe
  21. # 负载均衡主服务器IP地址
  22. LVS_SERVER_IP=192.168.254.3
  23. # 负载均衡备服务器IP地址
  24. LVS_BACKUP_SERVER_IP=192.168.254.4
  25. # 虚拟IP地址,对外提供服务的地址
  26. VIP=192.168.254.2
  27. # 广播地址
  28. BROADCAST=192.168.255.255
  29. # 网关,也就是路由器对内地址
  30. GATEWAY=192.168.0.2
  31. # 真实服务器数组,数据之间用空格隔开
  32. # 如'10.0.0.1 10.0.0.2' 表示10.0.0.1和10.0.0.2
  33. # 如'10 10.0.0.1' 表示10.0.0.1, 10.0.0.2, ..., 10.0.0.10
  34. RS_IPS='192.168.254.10 192.168.254.11'
  35. # 真实服务器业务对外提供服务的端口数组,数据之间用空格隔开,'80 81'
  36. RS_PORTS='3306'
  37. #--------------------------------------------------------------------------------------------------------------------------------

  38. rm -fr ./autogen_lvs
  39. mkdir ./autogen_lvs

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

  50. LVS_CMD=
  51. for rs_port in $RS_PORTS;do
  52.     LVS_CMD=$LVS_CMD$'\t'$'\t'"ipvsadm -A -t $VIP:$rs_port -s wlc;"$'\n'
  53.     for rs_ip in $RS_IPS;do
  54.         LVS_CMD=$LVS_CMD$'\t'$'\t'"ipvsadm -a -t $VIP:$rs_port -r $rs_ip:$rs_port -g -w 1;"$'\n'
  55.     done
  56. done

  57. #Create lvs-dr.sh
  58. cat > ./autogen_lvs/lvs-dr.sh << end
  59. #!/bin/sh
  60. service iptables stop
  61. chkconfig iptables off
  62. chmod 777 /etc/rc.d/init.d/functions
  63. /etc/rc.d/init.d/functions
  64. case "\$1" in
  65.     start)
  66.         echo "start LVS of DirectorServer"
  67.         #Set the virtual IP Address
  68.         ifconfig eth0:1 $VIP broadcast $BROADCAST netmask 255.255.255.255 up
  69.         route add host $VIP dev eth0:1
  70.         #Clear IPVS Table
  71.         ipvsadm -C
  72.         #set LVS
  73.         
  74. $LVS_CMD
  75.         #Run LVS
  76.         ipvsadm
  77.         ;;
  78.     stop)
  79.         echo "close LVS Directorserver"
  80.         ipvsadm -C
  81.         ifconfig eth0:1 down
  82.         ;;
  83.     *)
  84.         echo "Usage: \$0 {start|stop}"
  85.         exit 1
  86. esac
  87. route del default gw $GATEWAY
  88. route add default gw $GATEWAY

  89. end

  90. #Create LVSServer.sh
  91. cat > ./autogen_lvs/LVSServer.sh << end
  92. #!/bin/sh
  93. service iptables stop
  94. chkconfig iptables off
  95. case "\$1" in
  96.     start)
  97.         /etc/init.d/keepalived start
  98.         ;;
  99.     stop)
  100.         /etc/init.d/keepalived stop
  101.         ;;
  102.     restart)
  103.         /etc/init.d/keepalived restart
  104.         ;;
  105.     *)
  106.         echo "Usage: \$0 {start|stop|restart}"
  107.         exit 1
  108. esac
  109. route del default gw $GATEWAY
  110. route add default gw $GATEWAY

  111. if ! grep LVSServer /etc/rc.local > /dev/null
  112. then
  113.         echo "sh /etc/LVSServer.sh start" >> /etc/rc.local
  114. fi

  115. end

  116. #Create RealServer.sh
  117. cat > ./autogen_lvs/RealServer.sh << end
  118. #!/bin/sh
  119. chmod 777 /etc/rc.d/init.d/functions
  120. /etc/rc.d/init.d/functions
  121. case "\$1" in
  122.     start)
  123.         echo "reparing for Real Server"
  124.         echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore
  125.         echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce
  126.         echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
  127.         echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
  128.         ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $BROADCAST up
  129.         route add -host $VIP dev lo:0
  130.         ;;
  131.     stop)
  132.         ifconfig lo:0 down
  133.         echo "0">/proc/sys/net/ipv4/conf/lo/arp_ignore
  134.         echo "0">/proc/sys/net/ipv4/conf/lo/arp_announce
  135.         echo "0">/proc/sys/net/ipv4/conf/all/arp_ignore
  136.         echo "0">/proc/sys/net/ipv4/conf/all/arp_announce
  137.         ;;
  138.     *)
  139.         echo "Usage: \$0 {start|stop}"
  140.         exit 1
  141. esac
  142. route del default gw $GATEWAY
  143. route add default gw $GATEWAY

  144. if ! grep RealServer /etc/rc.local > /dev/null
  145. then
  146.         echo "sh /etc/RealServer.sh start" >> /etc/rc.local
  147. fi

  148. end


  149. VIRTUAL_SERVER_LIST=
  150. for rs_port in $RS_PORTS;do
  151.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST"virtual_server $VIP $rs_port {"$'\n'
  152.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"delay_loop 6"$'\n'
  153.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"lb_algo wlc"$'\n'
  154.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"lb_kind DR"$'\n'
  155.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"#persistence_timeout 60"$'\n'
  156.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"protocol TCP"$'\n'
  157.     for rs_ip in $RS_IPS;do
  158.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"real_server $rs_ip $rs_port {"$'\n'
  159.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'"weight 1"$'\n'
  160.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'"TCP_CHECK {"$'\n'
  161.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'$'\t'"connect_timeout 10"$'\n'
  162.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'$'\t'"nb_get_retry 3"$'\n'
  163.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'$'\t'"delay_before_retry 3"$'\n'
  164.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'$'\t'"connect_port $rs_port"$'\n'
  165.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'"}"$'\n'
  166.         VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"}"$'\n'
  167.     done
  168.     VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST"}"$'\n'$'\n'
  169. done

  170. #Create keepalived_master.conf
  171. cat > ./autogen_lvs/keepalived_master.conf << end
  172. ! Configuration File for keepalived

  173. global_defs {
  174.    router_id LVS_DEVEL
  175. }

  176. vrrp_instance VI_1 {
  177.     state MASTER
  178.     interface eth0
  179.     virtual_router_id 51
  180.     priority 100
  181.     advert_int 1
  182.     authentication {
  183.         auth_type PASS
  184.         auth_pass 1111
  185.     }
  186.     virtual_ipaddress {
  187.         $VIP
  188.     }
  189. }

  190. $VIRTUAL_SERVER_LIST
  191. end

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

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

  206. #Create ConfigAllServer.sh
  207. cat > ./autogen_lvs/ConfigAllServer.sh << end
  208. #!/bin/sh

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

  216. if test_host_online $LVS_SERVER_IP;then
  217. /usr/bin/expect << endexpect
  218.     set timeout 10
  219.     spawn scp LVSServer.sh root@$LVS_SERVER_IP:/etc/
  220.     expect "*password*"
  221.     send "$ROOTPASSWORD\r"
  222.     expect eof

  223.     spawn scp keepalived_master.conf root@$LVS_SERVER_IP:/etc/keepalived/keepalived.conf
  224.     expect "*password*"
  225.     send "$ROOTPASSWORD\r"
  226.     expect eof
  227. endexpect
  228. fi

  229. if test_host_online $LVS_BACKUP_SERVER_IP;then
  230. /usr/bin/expect << endexpect
  231.     set timeout 10
  232.     spawn scp LVSServer.sh root@$LVS_BACKUP_SERVER_IP:/etc/
  233.     expect "*password*"
  234.     send "$ROOTPASSWORD\r"
  235.     expect eof

  236.     spawn scp keepalived_backup.conf root@$LVS_BACKUP_SERVER_IP:/etc/keepalived/keepalived.conf
  237.     expect "*password*"
  238.     send "$ROOTPASSWORD\r"
  239.     expect eof
  240. endexpect
  241. fi
  242. $SCP_LIST
  243. end

  244. RUN_LIST=
  245. for rs_ip in $RS_IPS;do
  246.     RUN_LIST=$RUN_LIST"if test_host_online $rs_ip;then"$'\n'
  247.     RUN_LIST=$RUN_LIST"/usr/bin/expect << endexpect"$'\n'
  248.     RUN_LIST=$RUN_LIST$'\t'"set timeout 10"$'\n'
  249.     RUN_LIST=$RUN_LIST$'\t'"spawn ssh root@$rs_ip"$'\n'
  250.     RUN_LIST=$RUN_LIST$'\t'"expect \"*password*\""$'\n'
  251.     RUN_LIST=$RUN_LIST$'\t'"send \"$ROOTPASSWORD\r\""$'\n'
  252.     RUN_LIST=$RUN_LIST$'\t'"expect \"#\""$'\n'
  253.     RUN_LIST=$RUN_LIST$'\t'"send \"sh /etc/RealServer.sh start\r\""$'\n'
  254.     RUN_LIST=$RUN_LIST$'\t'"expect eof"$'\n'
  255.     RUN_LIST=$RUN_LIST$'\t'"send \"exit\r\""$'\n'
  256.     RUN_LIST=$RUN_LIST"endexpect"$'\n'
  257.     RUN_LIST=$RUN_LIST"fi"$'\n'$'\n'
  258. done
  259. #Create RunAllServer.sh
  260. cat > ./autogen_lvs/RunAllServer.sh << end
  261. #!/bin/sh

  262. function test_host_online() {
  263.     if [[ \` ping \$1 -c 1 -w 1 | grep "1 received" | wc -l \` = "1" ]];then
  264.         return 0
  265.     else
  266.         return 1
  267.     fi
  268. }

  269. $RUN_LIST
  270. if test_host_online $LVS_SERVER_IP;then
  271. /usr/bin/expect << endexpect
  272.     set timeout 10
  273.     spawn ssh root@$LVS_SERVER_IP
  274.     expect "*password*"
  275.     send "$ROOTPASSWORD\r"
  276.     expect "#"
  277.     send "sh /etc/LVSServer.sh start\r"
  278.     expect eof
  279.     send "exit\r"
  280. endexpect
  281. fi

  282. if test_host_online $LVS_BACKUP_SERVER_IP;then
  283. /usr/bin/expect << endexpect
  284.     set timeout 10
  285.     spawn ssh root@$LVS_BACKUP_SERVER_IP
  286.     expect "*password*"
  287.     send "$ROOTPASSWORD\r"
  288.     expect "#"
  289.     send "sh /etc/LVSServer.sh start\r"
  290.     expect eof
  291.     send "exit\r"
  292. endexpect
  293. fi

  294. end

  295. #Create StopAllServer.sh
  296. sed 's/start/stop/' ./autogen_lvs/RunAllServer.sh > ./autogen_lvs/StopAllServer.sh


 

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