这个比我前面写的那个要好些(前面那个是按照网上的配置写的,不太专业)
需要安装的软件:
LVS主从节点上:
keepalived
ipvsadm
RealServer上需要安装的软件:
arptables_jf
另外,执行自动化配置脚本的机子,需要expect软件支持,参见脚本中的注意事项。
这个脚本是在我手动配置成功后编写的,还没测试过,不过应该问题不大,有问题麻烦帮忙指正。
自动化脚本:
- #!/bin/sh
- #文 件 名: autogen_lvs.sh
- #功 能: 自动生成LVS,Keepalived配置文件,生成环境自动部署,自动运行脚本等
- #创建时间: 2012-02-05
- #修改时间: 2012-06-01
- #自动化生成的文件列表:
- #(执行autogen_lvs.sh后,会生成autogen目录,里面会存放自动生成的相关文件)
- #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配置
- # -------------------------------------------------以下根据实际情况进行设定----------------------------------------------------
- # ROOT权限密码,该密码为所有负载均衡服务器,真实服务器的通用密码,所有服务器ROOT将使用统一密码,便于自动化控制,否则上面提到的ConfigAllServer.sh, RunAllServer.sh, StopAllServer.sh这三个文件无法正常运行
- ROOTPASSWORD=123456
- # 负载均衡主服务器IP地址
- LVS_SERVER_IP=172.16.0.40
- # 负载均衡备服务器IP地址
- LVS_BACKUP_SERVER_IP=1.1.1.1
- # 虚拟IP地址,对外提供服务的地址
- VIP=172.16.0.252
- # 广播地址
- BROADCAST=172.16.0.255
- # 真实服务器数组,数据之间用空格隔开
- # 如'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='172.16.0.43 172.16.0.44'
- # 真实服务器业务对外提供服务的端口数组,数据之间用空格隔开,如'80 81'
- RS_PORTS='8447 8888'
- # 网卡设备号,如eth0,eth1,单网卡一般都为eth0
- DEVICE=eth0
- #--------------------------------------------------------------------------------------------------------------------------------
- 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
- #Create LVSServer.sh
- cat > ./autogen_lvs/LVSServer.sh << end
- #!/bin/sh
- RS_PORTS="$RS_PORTS"
- for port in \$RS_PORTS;do
- iptables -I INPUT 1 -p tcp --dport \$port -j ACCEPT
- done
- 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
- 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
- VIP="$VIP"
- RS_PORTS="$RS_PORTS"
- RIP=\`ifconfig | sed -n "/$DEVICE/{
- N
- s/^$DEVICE.*inet addr:\(.*\) Bcast.*$/\1/p
- q
- }
- "\`
- if [ -z "\`rpm -qa | grep arptables_jf\`" ];then
- echo "You need install the software of arptables_jf first"
- exit
- fi
- if [ ! -e "/etc/sysconfig/arptables" ];then
- service arptables_jf save
- fi
- #add vip on $DEVICE
- ip addr add \$VIP dev $DEVICE
- #drop arp package to vip
- service arptables_jf start
- arptables -A IN -d \$VIP -j DROP
- arptables -A OUT -s \$VIP -j mangle --mangle-ip-s \$RIP
- service iptables start
- for port in \$RS_PORTS;do
- iptables -I INPUT 1 -p tcp --dport \$port -j ACCEPT
- iptables -t nat -A PREROUTING -p tcp -d \$VIP --dport \$port -j REDIRECT
- done
- 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 $DEVICE
- 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
- #Create ConfigAllServer.sh
- cat > ./autogen_lvs/ConfigAllServer.sh << end
- #!/bin/sh
- RS_IPS="$RS_IPS"
- ROOTPASSWORD="$ROOTPASSWORD"
- LVS_SERVER_IP="$LVS_SERVER_IP"
- LVS_BACKUP_SERVER_IP="$LVS_BACKUP_SERVER_IP"
- function test_host_online() {
- if [[ \` ping \$1 -c 1 -w 1 | grep "1 received" | wc -l \` = "1" ]];then
- return 0
- else
- return 1
- fi
- }
- sed -i 's/^# StrictHostKeyChecking ask$/StrictHostKeyChecking no/' /etc/ssh/ssh_config
- if test_host_online \$LVS_SERVER_IP;then
- /usr/bin/expect << endexpect
- set timeout 100
- 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 100
- 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
- for rs_ip in \$RS_IPS;do
- if test_host_online \$rs_ip;then
- /usr/bin/expect << endexpect
- set timeout 100
- spawn scp RealServer.sh root@\$rs_ip:/etc/
- expect "*password*"
- send "\$ROOTPASSWORD\r"
- expect eof
- endexpect
- fi
- done
- end
- #Create RunAllServer.sh
- cat > ./autogen_lvs/RunAllServer.sh << end
- #!/bin/sh
- LVS_SERVER_IP="$LVS_SERVER_IP"
- ROOTPASSWORD="$ROOTPASSWORD"
- LVS_BACKUP_SERVER_IP="$LVS_BACKUP_SERVER_IP"
- RS_IPS="$RS_IPS"
- function test_host_online() {
- if [[ \` ping \$1 -c 1 -w 1 | grep "1 received" | wc -l \` = "1" ]];then
- return 0
- else
- return 1
- fi
- }
- sed -i 's/^# StrictHostKeyChecking ask$/StrictHostKeyChecking no/' /etc/ssh/ssh_config
- if test_host_online \$LVS_SERVER_IP;then
- /usr/bin/expect << endexpect
- set timeout 100
- spawn ssh root@\$LVS_SERVER_IP
- expect "*password*"
- send "\$ROOTPASSWORD\r"
- expect "#"
- send "sh /etc/LVSServer.sh start\r"
- expect "#"
- send "exit\r"
- endexpect
- fi
- if test_host_online \$LVS_BACKUP_SERVER_IP;then
- /usr/bin/expect << endexpect
- set timeout 100
- spawn ssh root@\$LVS_BACKUP_SERVER_IP
- expect "*password*"
- send "\$ROOTPASSWORD\r"
- expect "#"
- send "sh /etc/LVSServer.sh start\r"
- expect "#"
- send "exit\r"
- endexpect
- fi
- for rs_ip in \$RS_IPS;do
- if test_host_online \$rs_ip;then
- /usr/bin/expect << endexpect
- set timeout 100
- spawn ssh root@\$rs_ip
- expect "*password*"
- send "\$ROOTPASSWORD\r"
- expect "#"
- send "sh /etc/RealServer.sh start\r"
- expect "#"
- send "exit\r"
- endexpect
- fi
- done
- end
- #Create StopAllServer.sh
- sed 's/start/stop/' ./autogen_lvs/RunAllServer.sh > ./autogen_lvs/StopAllServer.sh
阅读(2498) | 评论(1) | 转发(0) |