Ubuntu lvs+keepalived 负载均衡配置实验环境:
Vip: 192.168.1.128
Master:192.168.1.125
Slave: 192.168.1.126
Realserve1: 192.168.1.127
Realserve2: 192.168.1.129
1、分别在master和slave上安装ipvsadm、keepalived;Su do apt-get install ipvsadm keepalived安装完以后分别修改ipvsadm配置文件,要不然启动不成功;Sudo vim /etc/default/ipvsadm
在1.126也就是slave上,上边的DAEMON=”backup”修改完成后,启动ipvsadm;sudo /etc/init.d/ipvsadm start
2、在master跟slave上边还要开启路由转发功能,具体如下; sudo sysctl net.ipv4.ip_forward=1 sysctl -psudo sysctl -a | grep ip_forward sudo /etc/init.d/ipvsadm restart
3、接下来设置master跟slave的keepalivedkeepalived.conf默认是不存在的
sudo vim /etc/keepalived/keepalived.confvrrp_instance
VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.128
}
}
virtual_server 192.168.1.128 80 {
delay_loop 6
lb_algo wlc
lb_kind DR
protocol TCP
real_server 192.168.1.127 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.129 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
virtual_server 192.168.1.128 8080 {
delay_loop 6
lb_algo wlc
lb_kind DR
protocol TCP
real_server 192.168.1.127 8080 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.129 8080 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
以上是slave的配置内容,master跟它差不多,需要修改的内容有state状态 和priority优先级 其它的不用动;修改完成以后,sudo /etc/init.d/keepalived restart
4、还有就是在master跟slave上边需要添加虚拟服务器跟节点服务器
sudo ipvsadm -A -t 192.168.1.128:8080 -s wlc
sudo ipvsadm -a -t 192.168.1.128:8080 -r 192.168.1.129:8080 -g -w 1
sudo ipvsadm -a -t 192.168.1.128:8080 -r 192.168.1.127:8080 -g -w 1
sudo ipvsadm -A -t 192.168.1.128:80 -s wlc
sudo ipvsadm -a -t 192.168.1.128:80 -r 192.168.1.129:80 -g -w 1
sudo ipvsadm -a -t 192.168.1.128:80 -r 192.168.1.123:80 -g -w 1
Sudo ipvsadm-save5、接下来就是在两台realserver上的配置,提供应用的 RealServer1 和 RealServer2 不需要安装任何IPVS软件,只需要设置在开机后自动运行以下脚本
Sudovim dgf.sh
#!/bin/bash
SNS_VIP=192.168.1.128. /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 0Sudo bash dgf.sh start至此,lvs+keepalived 负载均衡就搭建好了^_^