基于VMware的虚拟Linux集群搭建-lvs+keepalived
准本工作
若本机没有yum库,最好配置安装yum库,方便软件的安装
yum库创建基本步骤
①安装createrepo
②yum -y installl createrepo
③用createrepo创建
createrepo命令用于生成yum仓库所须要的一些基本配置信息。
详细请參考“yum库的安装于配置”
集群配置
1. 设置虚拟机IP
在Vmare虚拟机中虚拟四个Linux主机
LvsMaster:192.168.10.101
LvsBak: 192.168.10.102
RealServer1: 192.168.10.121
RealServer2: 192.168.10.122
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3VveGlhb2ppZV80MTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
主机之间的拓扑关系图
2. 安装ipvsadm 和 keepalived
① 分别在LvsMaster和LvsBak上安装 ipvsadm
yum -y install *ipvsadm*
下载地址:wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar
②分别在LvsMaster和LvsBak上安装 keepalived
安装依赖库:yum install -y openssl openssl-devel
下载keepalived:wget http://www.keepalived.org/software/keepalived-1.2.6.tar.gz
tar zxvf keepalived-1.2.6.tar.gz
./configure --prefix=/usr/local/keepalived
make
make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived /etc/keepalived.conf
③ 配置 keepalived
LvsMaster配置
global_defs {
notification_email {
540621626@qq.com #emial通知
}
notification_email_from 540621626@qq.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_sync_group test {
#设置vrrp组
group {
loadbalance
}
}
vrrp_instance loadbalance {
state MASTER #设置主机类型,MASTER和BACKUP两种。必须大写 ,即太主机一台备用机
interface eth0 #设置对外服务网卡
virtual_router_id 51
priority 100 #设置优先级。备用主机的优先级要比主机低就可以
advert_int 1#设置同步时间间隔
authentication {#设置验证类型和password
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {#设置虚拟ip,即浮动ip
192.168.10.115
}
virtual_server 192.168.10.115 80 {
delay_loop 6 #健康检查时间间隔
lb_algo rr#负载调度算法,默觉得轮训算法
lb_kind DR#负载均衡转发规则,也就是IP转发规则,DR转发方式最快。可是全部机器必须在一个网段内。还有NAT和TUNEL两种转发方式
protocol TCP
real_server 192.168.10.121 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.10.122 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
LvsBakr配置
global_defs {
notification_email {
540621626@qqq.com
}
notification_email_from 540621626@qq.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_sync_group test {
group {
loadbalance
}
}
vrrp_instance loadbalance {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.115
}
}
virtual_server 192.168.10.115 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.10.121 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.10.122 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
④ 启动keepalived
LvsMastrer: /etc/init.d/keepalived start
LvsBak : /etc/init.d/keepalived start
查看启动状态
LvsMaster: tail -f /var/log/messages
LvsBak: tail -f /var/log/messages
用ip addr和ipvsadm查看路由情况
LvsMaster: ip addr
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3VveGlhb2ppZV80MTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
LvsMaster: ipvsadm
LvsBak:ip addr
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3VveGlhb2ppZV80MTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
LvsBak:ipvsadm
3. 设置RealServer1和RealServer2
① 编辑RealServe脚本
source /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up
/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
② 在RealServer1和RealServer2上安装apache
DocumentRoot /var/www/html
ServerName www.example.com
</VirtualHost>
4. 測试负载均衡和双机热备
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3VveGlhb2ppZV80MTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">