lvs其实是由2段代码组成,一段工作在内核空间叫ipvs,是真正调度的代码,另一段工作在用户空间叫ipvsadm,负责为lpvs内核框架来编写规则,定义谁是集群服务、有哪些real server,所以我们用ipvsadm编写规则定义集群服务告诉lvs用哪些规则去调度real server。
LVS的术语:
Director、Real server、VIP:Virtual IP、 DIP: Director IP 、 RIP: Real Server IP、CIP:Client IP
Director脚本: #!/bin/bash # LVS script for VS/DR . /etc/rc.d/init.d/functions VIP=192.168.0.210 RIP1=192.168.0.221 RIP2=192.168.0.222 PORT=80 case "$1" in start) /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev eth0:1 # Since this is the Director we must be able to forward packets echo 1 > /proc/sys/net/ipv4/ip_forward # Clear all iptables rules. /sbin/iptables -F # Reset iptables counters. /sbin/iptables -Z # Clear all ipvsadm rules/services. /sbin/ipvsadm -C # Add an IP virtual service for VIP 192.168.0.219 port 80 # In this recipe, we will use the round-robin scheduling method. # In production, however, you should use a weighted, dynamic scheduling method. /sbin/ipvsadm -A -t $VIP:80 -s wlc # Now direct packets for this VIP to # the real server IP (RIP) inside the cluster /sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -g -w 1 /sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g -w 2 /bin/touch /var/lock/subsys/ipvsadm &> /dev/null ;; stop) # Stop forwarding packets echo 0 > /proc/sys/net/ipv4/ip_forward # Reset ipvsadm /sbin/ipvsadm -C # Bring down the VIP interface /sbin/ifconfig eth0:1 down /sbin/route del $VIP /bin/rm -f /var/lock/subsys/ipvsadm echo "ipvs is stopped..." ;; status) if [ ! -e /var/lock/subsys/ipvsadm ]; then echo "ipvsadm is stopped ..." else echo "ipvs is running ..." ipvsadm -L -n fi ;; *) echo "Usage: $0 {start|stop|status}" ;; esac
RealServer脚本: #!/bin/bash # Script to start LVS DR real server. # description: LVS DR real server . /etc/rc.d/init.d/functions VIP=192.168.0.219 host=`/bin/hostname` case "$1" in start) # Start LVS-DR real server on this machine. /sbin/ifconfig lo down /sbin/ifconfig lo up 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
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev lo:0 ;; stop) # Stop LVS-DR real server loopback device(s). /sbin/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 ;; status) # Status of LVS-DR real server. islothere=`/sbin/ifconfig lo:0 | grep $VIP` isrothere=`netstat -rn | grep "lo:0" | grep $VIP` if [ ! "$islothere" -o ! "isrothere" ];then # Either the route or the lo:0 device # not found. echo "LVS-DR real server Stopped." else echo "LVS-DR real server Running." fi ;; *) # Invalid entry. echo "$0: Usage: $0 {start|status|stop}" exit 1 ;; esac
在CentOS 6.5上使用lxc虚拟机(为LVS提供轻量级实验环境):
原理:宿主机网卡变成交换机,并增加一个虚拟网卡和外部通讯
1 解决依赖关系
#service NetworkManager status #确保NetworkManager服务关闭
#service network status #保证network服务开启
# yum install libcgroup
# service cgconfig start
2 提供虚拟网桥接口
在/etc/sysconfig/network-scripts目录中新建名为ifcfg-br0的配置文件,其内容如下:
DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
IPADDR=172.16.100.7
NETMASK=255.255.0.0
GATEWAY=172.16.0.1
ONBOOT=yes
DELAY=0
NM_CONTROLLED=no
接下将桥接的物理网卡(假设为eth0)关联至前面定义的桥接设备,编辑/etc/sysconfig/network-script/ifcfg-eth0为类似如下内容:
DEVICE="eth0"
BOOTPROTO="static"
NM_CONTROLLED="no"
ONBOOT="yes"
TYPE="Ethernet"
BRIDGE=br0
上述步骤无误后重启network服务即可。另外,还有其它简单的方式来实现桥接设备的创建,例如使用brctl或virsh等,这里不再详述。
3 安装lxc
epel源中提供的lxc版本为0.9.0,其未带centos系统模板。因此,这里选择使用目前最新的lxc版本1.0.5。编译安装过程较为简单,按其源码目录中的INSTALL文档中的提示进行即可。我们事先经过测试后已经将lxc-1.0.5制作成了适用于centos 6 x86_64平台的rpm包(通过附件下载),因此,这里将直接使用rpm命令安装。
# yum install lxc-1.0.5-1.el6.x86_64.rpm lxc-libs-1.0.5-1.el6.x86_64.rpm
# rpm -ql lxc
#yum install libcgroup -y
#service cgconfig stat
#chkconfig cfconfig on
4 检查lxc运行环境
# lxc-checkconfig
Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-2.6.32-431.el6.x86_64
--- Namespaces ---
Namespaces: enabled
………………………………
--- Control groups ---
…………………………
--- Misc ---
Veth pair device: enabled
…………………………
Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig
5 创建centos虚拟机
lxc为创建虚拟机提供了模板文件,它们位于/usr/share/lxc/templates目录中。其中的lxc-centos即为创建lxc centos系统的模板。
另外,lxc为虚拟机提供的默认配置文件为/etc/lxc/default.conf,其中使用的桥接接口名称为virbr0,此与前面的创建的接口名称不一致,因此需要作出修改。当然,也可以将此文件复制之后进行修改,并以为作为接下来的要创建的centos虚拟机的专用配置文件。修改后的default.conf如下所示。
lxc.network.type = veth
lxc.network.link = br0
lxc.network.flags = up
创建虚拟机centos:
# lxc-create -n centos -t /usr/share/lxc/templates/lxc-centos
Host CPE ID from /etc/system-release-cpe: cpe:/o:centos:linux:6:GA
Checking cache download in /var/cache/lxc/centos/x86_64/6/rootfs ...
…………
Complete!
Download complete.
Expiring password for user root.
passwd: Success
Container rootfs and config have been created.
Edit the config file to check/enable networking setup.
The temporary root password is stored in:
'/var/lib/lxc/centos/tmp_root_pass'
The root password is set up as expired and will require it to be changed
at first login, which you should do as soon as possible. If you lose the
root password or wish to change it without starting the container, you
can change it from the host by running the following command (which will
also reset the expired flag):
chroot /var/lib/lxc/centos/rootfs passwd
上述输出内容表示系统安装已经成功,可由lxc-start命令启动了。另外,目标系统的root用户的默认密码在/var/lib/lxc/centos/tmp_root_pass文件中。
6 启动目标系统centos:
启动lxc虚拟机需要使用lxc-start命令。此命令的常用选项有
-n NAME:要启动的虚拟机的名称
-d: 在后台运行此虚拟机
-o /path/to/somefile: 日志文件的保存位置
-l: 日志级别
简单的启动命令如下所示:
# lxc-start -n centos
启动后可直接连接至目标系统的控制台,并在显示登录提示符后登录系统即可。需要注意的是,root用户的默认密码已经被设置为过期,因此第一次登录时需要修改密码后方可使用
也可以在lxc-start的命令后面附加-d选项,让系统运行于后台。如果要停止虚拟机,使用lxc-stop命令即可。