Xenserver 虚拟机下配置LVS+keepalived
测试环境:
思杰xenserver 5.5
与vmware 和真实机不一样需要安装kernel-xen并切换至Xen内核启动
之前是物理机配置。一路正常。今天却遇到了问题。所以做个记录。
测试机相关信息:
LVS-DR-Master 192.1.1.36
LVS-DR-BACKUP 192.1.1.37
LVS-DR-VIP 192.1.1.38
WEB1-Realserver 192.1.1.31
WEB2-Realserver 192.1.1.32
GateWay 192.1.1.1
一、系统配置
安装相应的包:yum -y install kernel-xen kernel-devel openssl openssl-devel
修改menu.lst文件:
- # more /boot/grub/menu.lst
- # grub.conf generated by anaconda
- #
- # Note that you do not have to rerun grub after making changes to this file
- # NOTICE: You have a /boot partition. This means that
- # all kernel and initrd paths are relative to /boot/, eg.
- # root (hd0,0)
- # kernel /vmlinuz-version ro root=/dev/xvda2
- # initrd /initrd-version.img
- #boot=/dev/xvda
- default=0
- timeout=5
- splashimage=(hd0,0)/grub/splash.xpm.gz
- hiddenmenu
- #title CentOS (2.6.18-128.1.10.el5.xs5.5.0.51xen)
- # root (hd0,0)
- # kernel /vmlinuz-2.6.18-128.1.10.el5.xs5.5.0.51xen ro root=LABEL=/ console=xvc0 rhgb quiet
- # initrd /initrd-2.6.18-128.1.10.el5.xs5.5.0.51xen.img
- title CentOS (2.6.18-92.el5xen)
- root (hd0,0)
- kernel /vmlinuz-2.6.18-92.el5xen ro root=LABEL=/ console=xvc0 rhgb quiet
- initrd /initrd-2.6.18-92.el5xen.img
#uname -r
2.6.18-128.el5xen
#ln -s /usr/src/kernels/2.6.18-128.el5xen-x86_64/ /usr/src/linux
二、安装相应的包
#mkdir /usr/local/src/lvs
#cd /usr/local/src/lvs
#wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
#wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
- #lsmod |grep ip_vs
- #tar zxvf ipvsadm-1.24.tar.gz
- #cd ipvsadm-1.24
- #make && make install
- #find / -name ipvsadm # 查看ipvsadm的位置
- #tar zxvf keepalived-1.1.15.tar.gz
- #cd keepalived-1.1.15
- #./configure && make && make install
- #find / -name keepalived # 查看keepalived位置
- #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/keepalived.conf /etc/keepalived/
- #cp /usr/local/sbin/keepalived /usr/sbin/
- #service keepalived start|stop # 做成系统启动服务方便管理.
三、配置keepalived
- ! Configuration File for keepalived
- global_defs {
- router_id LVS_DEVEL
- }
- vrrp_sync_group VGM {
- group {
- VI_OUT1
- }
- }
- #vip1
- vrrp_instance VI_OUT1 {
- state MASTER
- interface eth0
- virtual_router_id 51
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.1.1.38
- }
- }
- virtual_server 192.1.1.38 80 {
- delay_loop 6
- lb_algo rr
- lb_kind DR
- persistence_timeout 50
- protocol TCP
- real_server 192.1.1.31 80 {
- weight 100
- TCP_CHECK {
- connect_timeout 3
- nb_get_retry 3
- delay_before_retry 3
- connect_port 80
- }
- }
- real_server 192.1.1.32 80 {
- weight 100
- TCP_CHECK {
- connect_timeout 3
- nb_get_retry 3
- delay_before_retry 3
- connect_port 80
- }
- }
- }
Backup 机器的keepalived需要修改:
State BACKUP
weight 10 数值比master小即可
四、客户端绑定VIP
脚本:
- #vi /usr/local/sbin/realserver.sh
- #!/bin/bash
- SNS_VIP=192.1.1.38 (只需要修改此处)
- . /etc/rc.d/init.d/functions
- case "$1" in
- start)
- ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
- /sbin/route add -host $SNS_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 >/dev/null 2>&1
- echo "RealServer Start OK"
- ;;
- stop)
- ifconfig lo:0 down
- route del $SNS_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 "RealServer Stoped"
- ;;
- *)
- echo "Usage: $0 {start|stop}"
- exit 1
- esac
- exit 0
五、测试客户端绑定VIP
启动脚本。使用ip a 查看是否在lo绑定了VIP 停止后消失。
六、启动master backup 的keepalived查看日志。是否正常。
启动客户端apache 定义一个index.html 即可测试
本文不写测试过程。
就是停WEB1 再停WEB2 再停master看backup是否接管
七、错误
之前是在真实机上搞、一步没有出错。今天在Xenserver上需要注意一定要切到Xen内核启动。
由于配置客户端apache时。习惯性指定了listen IP:80 这是个错误。
原因:客户端绑定VIP后。监听127.0.0.1 80 用这个与LVS端进行通讯。
如果指定IP需要加上127 要不然就会出现拒绝请求的错误。
本文转自守住每一天51CTO博客,原文链接:http://blog.51cto.com/liuyu/291947,如需转载请自行联系原作者