Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71334
  • 博文数量: 6
  • 博客积分: 156
  • 博客等级: 入伍新兵
  • 技术积分: 110
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-07 17:20
个人简介

姓名:张伟 Blog:http://tutuye.blog.chinaunix.net

文章分类

全部博文(6)

分类: 架构设计与优化

2011-09-28 10:03:29

一.LVS与keepalived安装脚本
#!/bin/bash
#description: start LVS of DirectorServer
#Written by zhangmig@gmail.com

echo "================================="
echo "lvs + keepalived start installing"
echo "================================="

#编译安装ipvsadm前需要做个ln,笨方法,但是能适应不同版本
mkdir -p /home/install/lvs && cd /usr/src/kernels
ll | grep $(uname -r) > /home/install/lvs/linux-version
linuxversion=$(awk '{FS=" "} {print $9}' /home/install/lvs/linux-version)
ln -s /usr/src/kernels/$linuxversion/ /usr/src/linux

cd /home/install/lvs
url=""
wget $url/ipvsadm-1.24.tar.gz
wget $url/keepalived-1.1.15.tar.gz

tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
make && make install
cd ..

tar zxvf keepalived-1.1.15.tar.gz
cd keepalived-1.1.15
./configure
make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/bin/
cd ..

echo "================================="
echo "=====         Done        ======="
echo "================================="

二.keepalived.conf总结(自己理解,可能有误)
   关于vrrp:
虚拟路由器冗余协议,用于master与slave的通信以及状态检查,单台LVS应该可以不理会此项。
   关于persistence_timeout:Number 单位s
使用过程中发现无论设定任何数,会话保持时间都在一分钟。经检查发现在real-server中的web服 务resin.conf有这么一段60s,另外也发现nginx.conf 也有keepalived时间设定keepalive_timeout 30
   配置过程中出现只能访问一台机器(做不到轮询)以及日志疯长这种幽灵错误,将配置文件中的space换成tab后解决,格式问题?
   service keepalived start

三.LVS启动脚本(director)
   当启动keepalived时,ipvsadm -Ln后会发现lvs同样也会启动。
   在DR模式下,real-web的服务端口与lvs director的端口一致,设定值无效。
#!/bin/bash
#Linux Virtual Server Director configuretion && start
#zhangmig@gmail.com

VIP=
RIP_1=
RIP_2=
RIP_3=
...

case "$1" in

start)
ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255
route add -host $VIP dev eth0:0
echo 1 > /proc/sys/net/ipv4/ip_forward

ipvsadm -C

#Config virtual IP:Port && Load balance mode
ipvsadm -A -t $VIP:80 -s rr

#Config real IP:Port && Load forward mode
ipvsadm -a -T $VIP:80 -r $RIP_1:80 -g
ipvsadm -a -T $VIP:80 -r $RIP_2:80 -g
ipvsadm -a -T $VIP:80 -r $RIP_3:80 -g

ipvsadm -L
service ipvsadm save
service ipvsadm start
echo "ipvsadm started"
;;

stop)
ipvsadm -C
ipvsadm -Z
ifconfig eth0:0 down
route del $VIP
rm -rf /var/lock/subsys/ipvsadm > /dev/null 2>&1
echo "ipvsadm stopped"
;;

status)
if [ ! -e /var/lock/subsys/ipvsadm ] ; then
echo "ipvsadm is stopping"
exit 1
else
echo "ipvsadm is running"
fi
;;
*)
echo "/PATH/lvsdron.sh |start|stop|status|"
esac

四.lVS配置脚本(real_server)
#!/bin/bash
#The linux virtual server configuretion in real-web-server

VIP=

case "$1" in
start)
ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255
route add -host $VIP dev lo:0
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
sysctl -p
echo "============ Real-Server START ============="
;;
stop)
ifconfig lo:0 down
route del $VIP > /dev/null 2>&1
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 "============ Real-Server STOP ============="
;;
*)
echo "/PATH/realson.sh |start|stop|"
exit 1
esac

五.开机启动
chkconfig keepalived on
chkconfig --level num keepalived on#也可自定义runlevel启动
vi /etc/rc.d/rc.local
加入/PATH/*.sh
带有keepalived的lvs需要重启keepalived才能更新lvs配置

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