1、lvs软件ipvsadm下载安装(主机和备机上面都必须安装)
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
tar zxvf ipvsadm-1.24.tar.gz
ln -s /usr/src/kernels/2.6.9-89.EL-smp-x86_64 /usr/src/linux //注释:这一步针对自己的系统做,否则一会安装ipvsadm会报错。
cd ipvsadm-1.24
make
make install
2、keepalived下载安装(主机和备机上面都必须安装)
wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
tar zxvf keepalived-1.1.15.tar.gz
cd keepalived-1.1.15
//注释:如果系统rhel版本低于5.0以下安装报错,这里要修改系统配置
//vi /usr/src/linux/include/linux/types.h 将下面2行红色字体用/* */注释掉,如下图
/*
typedef __u16 __bitwise __sum16;
typedef __u32 __bitwise __wsum;
*/
./configure
make
make install
3、将keepalived做成服务启动(主机和备机上面都必须安装)
cp /usr/local/etc/rc.d/init.d/keepalived /etc/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 注释:测试服务启动与关闭
service keepalived stop
4、主机上配置keeplived(配置前备份好keepalived.conf文件然后再清空即可)
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
echo “” > /etc/keepalived/keepalived.conf
增加如下内容:
! Configuration File for keepalived
global_defs {
notification_email {
zengzhunzhun@ninetowns.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER 状态是MASTER
interface eth0 监听网卡切换
virtual_router_id 51
priority 100 优先级100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.15.120 虚拟IP地址,即VIP
}
}
virtual_server 192.168.15.120 80{
delay_loop 6
lb_algo wlc
lb_kind DR DR模式
persistence_timeout 1
protocol TCP
real_server 192.168.15.94 80{
weight 1 权重,权重越高处理的请求越多
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.15.93 80{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
virtual_server 192.168.15.120 1099{
delay_loop 6
lb_algo wlc
lb_kind DR
persistence_timeout 1
protocol TCP
real_server 192.168.15.94 1099{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 1099
}
}
real_server 192.168.15.93 1099{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 1099
}
}
}
5、备机上配置keeplived(配置前备份好keepalived.conf文件然后再清空即可)
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
echo “” > /etc/keepalived/keepalived.conf
增加如下内容:(只有两处和主机上的不一样,下面已用红色字体标出)
! Configuration File for keepalived
global_defs {
notification_email {
zengzhunzhun@ninetowns.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP 注意状态是BACKUP
interface eth0
virtual_router_id 51
priority 99 优先级是99,比主机低
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.15.120
}
}
virtual_server 192.168.15.120 80{
delay_loop 6
lb_algo wlc
lb_kind DR
persistence_timeout 1
protocol TCP
real_server 192.168.15.94 80{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.15.93 80{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
virtual_server 192.168.15.120 1099{
delay_loop 6
lb_algo wlc
lb_kind DR
persistence_timeout 1
protocol TCP
real_server 192.168.15.94 1099{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 1099
}
}
real_server 192.168.15.93 1099{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 1099
}
}
}
6、两台realserver上启动脚本,脚本内容如下:
#!/bin/bash
VIP=192.168.15.120
. /etc/rc.d/init.d/functions
case "$1" in
start)
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
ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
sysctl -p > /dev/null 2>&1
echo "realserver start OK"
;;
stop)
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
ifconfig lo:0 down
/sbin/route del $VIP > /dev/null 2>&1
echo "realserver stoped"
;;
*)
echo "Usage:$0 {start|stop}"
exit 1
esac
exit 0
7、测试工作:
1)启动主机和备机的keepalived服务,ip a确认是否有vip地址?
2)输入ipvsadm -Ln查看是否有如下内容输出?
ipvsadm -Ln
IP Virtual Server version 1.2.0 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.15.120:80 wlc persistent 1
-> 192.168.15.94:80 Route 1 122 4
-> 192.168.15.93:80 Route 1 124 6
TCP 192.168.15.120:1099 wlc persistent 1
-> 192.168.15.94:1099 Route 1 0 0
-> 192.168.15.93:1099 Route 1 0 0
3)停止主机的keepalived服务,备机上确认是否接管vip地址?
4)备机上输入ipvsadm -Ln查看ActiveConn和InActConn对应的值不为0?主机上反而为0了。
5)启动主机的keepalived服务,确认主机再次接管vip地址继续服务?
我这里就不做测试了!!!有问题请留言!