这个是按照目前网上的配置方法写的,但看过Redhat的配置说明后,发现这个不是很好,建议各位去看我的另一篇文章,这篇文章暂时保留在这里,毕竟是辛苦弄出来的。另一篇:http://blog.chinaunix.net/uid-26683919-id-3219775.html
- #!/bin/sh
- #文 件 名: autogen_lvs.sh
- #功 能: 自动生成LVS,Keepalived配置文件,生成环境自动部署,自动运行脚本等
- #创建时间: 2012-02-05
- #自动化生成的文件列表:
- #(执行该脚本后,会生成autogen_lvs目录,里面会存放自动生成的相关文件)
- #1. ConfigAllServer.sh 配置相关服务器集群功能脚本,包括负载均衡主服务器,负载均衡备服务器,真实服务器
- #2. RunAllServer.sh 开启所有Server集群功能,包括负载均衡主备服务器,真实服务器
- #3. StopAllServer.sh 关闭所有Server集群功能,包括负载均衡主备服务器,真实服务器
- #4. LVSServer.sh 开启或关闭负载均衡器集群功能,该文件会被ConfigAllServer.sh部署到负载主备服务器的/etc/ha.d/下,供后续使用
- #5. RealServer.sh 开启或关闭真实服务器集群功能,该文件会被ConfigAllServer.sh部署到真实服务器的/etc/下,供后续使用
- #6. keepalvied_master.conf 文件会被ConfigAllServer.sh部署到负载主服务器的/etc/keepalived/下,并重命名为keepalived.conf,作为HA配置
- #7. keepalvied_backup.conf 文件会被ConfigAllServer.sh部署到负载备服务器的/etc/keepalived/下,并重命名为keepalived.conf,作为HA配置
- #注意事项
- #1. 执行ConfigAllServer.sh, RunAllServer.sh, StopAllServer.sh这三个脚本的主机需要修改/etc/ssh/ssh_config,将StrictHostKeyChecking设置为no,防止弹出提示框引起自动化无法执行完毕
- #2. 执行该脚本的机子上需要安装expect
- #3. lvs和keepalived软件需要预先安装好
- # -------------------------------------------------以下根据实际情况进行设定----------------------------------------------------
- # ROOT权限密码,该密码为所有负载均衡服务器,真实服务器的通用密码,所有服务器ROOT将使用统一密码,便于自动化控制,否则上面提到的ConfigAllServer.sh, RunAllServer.sh, StopAllServer.sh这三个文件无法正常运行
- ROOTPASSWORD=123qwe
- # 负载均衡主服务器IP地址
- LVS_SERVER_IP=192.168.254.3
- # 负载均衡备服务器IP地址
- LVS_BACKUP_SERVER_IP=192.168.254.4
- # 虚拟IP地址,对外提供服务的地址
- VIP=192.168.254.2
- # 广播地址
- BROADCAST=192.168.255.255
- # 网关,也就是路由器对内地址
- GATEWAY=192.168.0.2
- # 真实服务器数组,数据之间用空格隔开
- # 如'10.0.0.1 10.0.0.2' 表示10.0.0.1和10.0.0.2
- # 如'10 10.0.0.1' 表示10.0.0.1, 10.0.0.2, ..., 10.0.0.10
- RS_IPS='192.168.254.10 192.168.254.11'
- # 真实服务器业务对外提供服务的端口数组,数据之间用空格隔开,如'80 81'
- RS_PORTS='3306'
- #--------------------------------------------------------------------------------------------------------------------------------
- rm -fr ./autogen_lvs
- mkdir ./autogen_lvs
- first_string=`echo $RS_IPS | cut -d " " -f1`
- if [[ `echo $first_string | grep "\." | wc -l` = "0" ]];then
- IP_NUM=$first_string
- IP_START=`echo $RS_IPS | cut -d " " -f2 | awk 'BEGIN{ FS="." } { printf "%s.%s.%s.", $1, $2, $3}' `
- OFFSET=`echo $RS_IPS | cut -d " " -f2 | cut -d "." -f4`
- RS_IPS=
- for((i=0;i<$IP_NUM;i++));do
- RS_IPS=$RS_IPS" $IP_START`expr $OFFSET + $i`"
- done
- fi
- LVS_CMD=
- for rs_port in $RS_PORTS;do
- LVS_CMD=$LVS_CMD$'\t'$'\t'"ipvsadm -A -t $VIP:$rs_port -s wlc;"$'\n'
- for rs_ip in $RS_IPS;do
- LVS_CMD=$LVS_CMD$'\t'$'\t'"ipvsadm -a -t $VIP:$rs_port -r $rs_ip:$rs_port -g -w 1;"$'\n'
- done
- done
- #Create lvs-dr.sh
- cat > ./autogen_lvs/lvs-dr.sh << end
- #!/bin/sh
- service iptables stop
- chkconfig iptables off
- chmod 777 /etc/rc.d/init.d/functions
- /etc/rc.d/init.d/functions
- case "\$1" in
- start)
- echo "start LVS of DirectorServer"
- #Set the virtual IP Address
- ifconfig eth0:1 $VIP broadcast $BROADCAST netmask 255.255.255.255 up
- route add host $VIP dev eth0:1
- #Clear IPVS Table
- ipvsadm -C
- #set LVS
-
- $LVS_CMD
- #Run LVS
- ipvsadm
- ;;
- stop)
- echo "close LVS Directorserver"
- ipvsadm -C
- ifconfig eth0:1 down
- ;;
- *)
- echo "Usage: \$0 {start|stop}"
- exit 1
- esac
- route del default gw $GATEWAY
- route add default gw $GATEWAY
- end
- #Create LVSServer.sh
- cat > ./autogen_lvs/LVSServer.sh << end
- #!/bin/sh
- service iptables stop
- chkconfig iptables off
- case "\$1" in
- start)
- /etc/init.d/keepalived start
- ;;
- stop)
- /etc/init.d/keepalived stop
- ;;
- restart)
- /etc/init.d/keepalived restart
- ;;
- *)
- echo "Usage: \$0 {start|stop|restart}"
- exit 1
- esac
- route del default gw $GATEWAY
- route add default gw $GATEWAY
- if ! grep LVSServer /etc/rc.local > /dev/null
- then
- echo "sh /etc/LVSServer.sh start" >> /etc/rc.local
- fi
- end
- #Create RealServer.sh
- cat > ./autogen_lvs/RealServer.sh << end
- #!/bin/sh
- chmod 777 /etc/rc.d/init.d/functions
- /etc/rc.d/init.d/functions
- case "\$1" in
- start)
- echo "reparing for Real Server"
- echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore
- echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce
- echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
- echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
- ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $BROADCAST up
- route add -host $VIP dev lo:0
- ;;
- stop)
- ifconfig lo:0 down
- echo "0">/proc/sys/net/ipv4/conf/lo/arp_ignore
- echo "0">/proc/sys/net/ipv4/conf/lo/arp_announce
- echo "0">/proc/sys/net/ipv4/conf/all/arp_ignore
- echo "0">/proc/sys/net/ipv4/conf/all/arp_announce
- ;;
- *)
- echo "Usage: \$0 {start|stop}"
- exit 1
- esac
- route del default gw $GATEWAY
- route add default gw $GATEWAY
- if ! grep RealServer /etc/rc.local > /dev/null
- then
- echo "sh /etc/RealServer.sh start" >> /etc/rc.local
- fi
- end
- VIRTUAL_SERVER_LIST=
- for rs_port in $RS_PORTS;do
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST"virtual_server $VIP $rs_port {"$'\n'
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"delay_loop 6"$'\n'
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"lb_algo wlc"$'\n'
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"lb_kind DR"$'\n'
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"#persistence_timeout 60"$'\n'
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"protocol TCP"$'\n'
- for rs_ip in $RS_IPS;do
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"real_server $rs_ip $rs_port {"$'\n'
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'"weight 1"$'\n'
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'"TCP_CHECK {"$'\n'
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'$'\t'"connect_timeout 10"$'\n'
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'$'\t'"nb_get_retry 3"$'\n'
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'$'\t'"delay_before_retry 3"$'\n'
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'$'\t'"connect_port $rs_port"$'\n'
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'$'\t'"}"$'\n'
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST$'\t'"}"$'\n'
- done
- VIRTUAL_SERVER_LIST=$VIRTUAL_SERVER_LIST"}"$'\n'$'\n'
- done
- #Create keepalived_master.conf
- cat > ./autogen_lvs/keepalived_master.conf << end
- ! Configuration File for keepalived
- global_defs {
- router_id LVS_DEVEL
- }
- vrrp_instance VI_1 {
- state MASTER
- interface eth0
- virtual_router_id 51
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- $VIP
- }
- }
- $VIRTUAL_SERVER_LIST
- end
- #Create keepalived_backup.conf
- sed -e 's/state MASTER/state BACKUP/' -e 's/priority 100/priority 99/' ./autogen_lvs/keepalived_master.conf > ./autogen_lvs/keepalived_backup.conf
- SCP_LIST=
- for rs_ip in $RS_IPS;do
- SCP_LIST=$SCP_LIST"if test_host_online $rs_ip;then"$'\n'
- SCP_LIST=$SCP_LIST"/usr/bin/expect << endexpect"$'\n'
- SCP_LIST=$SCP_LIST$'\t'"set timeout 10"$'\n'
- SCP_LIST=$SCP_LIST$'\t'"spawn scp RealServer.sh root@$rs_ip:/etc/"$'\n'
- SCP_LIST=$SCP_LIST$'\t'"expect \"*password*\""$'\n'
- SCP_LIST=$SCP_LIST$'\t'"send \"$ROOTPASSWORD\r\""$'\n'
- SCP_LIST=$SCP_LIST$'\t'"expect eof"$'\n'
- SCP_LIST=$SCP_LIST"endexpect"$'\n'
- SCP_LIST=$SCP_LIST"fi"$'\n'$'\n'
- done
- #Create ConfigAllServer.sh
- cat > ./autogen_lvs/ConfigAllServer.sh << end
- #!/bin/sh
- function test_host_online() {
- if [[ \` ping \$1 -c 1 -w 1 | grep "1 received" | wc -l \` = "1" ]];then
- return 0
- else
- return 1
- fi
- }
- if test_host_online $LVS_SERVER_IP;then
- /usr/bin/expect << endexpect
- set timeout 10
- spawn scp LVSServer.sh root@$LVS_SERVER_IP:/etc/
- expect "*password*"
- send "$ROOTPASSWORD\r"
- expect eof
- spawn scp keepalived_master.conf root@$LVS_SERVER_IP:/etc/keepalived/keepalived.conf
- expect "*password*"
- send "$ROOTPASSWORD\r"
- expect eof
- endexpect
- fi
- if test_host_online $LVS_BACKUP_SERVER_IP;then
- /usr/bin/expect << endexpect
- set timeout 10
- spawn scp LVSServer.sh root@$LVS_BACKUP_SERVER_IP:/etc/
- expect "*password*"
- send "$ROOTPASSWORD\r"
- expect eof
- spawn scp keepalived_backup.conf root@$LVS_BACKUP_SERVER_IP:/etc/keepalived/keepalived.conf
- expect "*password*"
- send "$ROOTPASSWORD\r"
- expect eof
- endexpect
- fi
- $SCP_LIST
- end
- RUN_LIST=
- for rs_ip in $RS_IPS;do
- RUN_LIST=$RUN_LIST"if test_host_online $rs_ip;then"$'\n'
- RUN_LIST=$RUN_LIST"/usr/bin/expect << endexpect"$'\n'
- RUN_LIST=$RUN_LIST$'\t'"set timeout 10"$'\n'
- RUN_LIST=$RUN_LIST$'\t'"spawn ssh root@$rs_ip"$'\n'
- RUN_LIST=$RUN_LIST$'\t'"expect \"*password*\""$'\n'
- RUN_LIST=$RUN_LIST$'\t'"send \"$ROOTPASSWORD\r\""$'\n'
- RUN_LIST=$RUN_LIST$'\t'"expect \"#\""$'\n'
- RUN_LIST=$RUN_LIST$'\t'"send \"sh /etc/RealServer.sh start\r\""$'\n'
- RUN_LIST=$RUN_LIST$'\t'"expect eof"$'\n'
- RUN_LIST=$RUN_LIST$'\t'"send \"exit\r\""$'\n'
- RUN_LIST=$RUN_LIST"endexpect"$'\n'
- RUN_LIST=$RUN_LIST"fi"$'\n'$'\n'
- done
- #Create RunAllServer.sh
- cat > ./autogen_lvs/RunAllServer.sh << end
- #!/bin/sh
- function test_host_online() {
- if [[ \` ping \$1 -c 1 -w 1 | grep "1 received" | wc -l \` = "1" ]];then
- return 0
- else
- return 1
- fi
- }
- $RUN_LIST
- if test_host_online $LVS_SERVER_IP;then
- /usr/bin/expect << endexpect
- set timeout 10
- spawn ssh root@$LVS_SERVER_IP
- expect "*password*"
- send "$ROOTPASSWORD\r"
- expect "#"
- send "sh /etc/LVSServer.sh start\r"
- expect eof
- send "exit\r"
- endexpect
- fi
- if test_host_online $LVS_BACKUP_SERVER_IP;then
- /usr/bin/expect << endexpect
- set timeout 10
- spawn ssh root@$LVS_BACKUP_SERVER_IP
- expect "*password*"
- send "$ROOTPASSWORD\r"
- expect "#"
- send "sh /etc/LVSServer.sh start\r"
- expect eof
- send "exit\r"
- endexpect
- fi
- end
- #Create StopAllServer.sh
- sed 's/start/stop/' ./autogen_lvs/RunAllServer.sh > ./autogen_lvs/StopAllServer.sh
阅读(2583) | 评论(0) | 转发(0) |