拓扑
keealive_master 192.168.12.145
keepalive_slave 192.168.12.146
lvs_vip 192.168.12.147
realserver1 192.168.12.148
realserver2 192.168.12.149
下载 keepalived-1.2.15.tar.gz
yum install -y ipvsadm kernel-headers kerbel-devel
需要加载此模块
modprobe bridge
否则在执行sysctl -p时会报如下错误
error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
error: "net.bridge.bridge-nf-call-iptables" is an unknown key
error: "net.bridge.bridge-nf-call-arptables" is an unknown key
编译安装keepalive时需要指定内核否则无法启用lvs功能
------------------------
Keepalived version : 1.2.15
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lssl -lcrypto -lcrypt
Use IPVS Framework : No ##此处应为yes
IPVS sync daemon support : No ##此处应该为yes
IPVS use libnl : No
fwmark socket support : Yes
Use VRRP Framework : Yes
Use VRRP VMAC : Yes
SNMP support : No
SHA1 support : No
Use Debug flags : No
tar -xf keepalived-1.2.15.tar.gz -C /usr/local/src
cd /usr/local/src/keepalived-1.2.15
./configure
--prefix=/usr/local/keepalived
--with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/
##若没有此目录,则需要安装kernel-devel
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/keepalive/etc/keepalived/keepalived.conf /etc/keepalived/
编辑配置文件
MASTER端的配置
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected] ##此处设置虚ip切换时的通知邮箱
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL_1 ##设置lvs的id全网唯一
}
vrrp_instance VI_1 {
state MASTER ##设置lvs的状态,MASTER和SLVAE BACKUP需要修改此处
interface eth1 ##设置服务的接口
virtual_router_id 51 ##设置虚拟路由id(master和backup需一致,不然主备无法通信,切换)
priority 100 ##设置主从的优先级 BACKUP需要修改此处优先级(值越大优先级越高)
advert_int 1 ##设置同步时间
authentication { ##设置验证类型和密码
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { ##设置VIP
192.168.12.147
}
}
virtual_server 192.168.12.147 80 {
delay_loop 6 ##健康检查间隔
lb_algo rr ##lvs调度算法
lb_kind DR ##lvs调度模式
nat_mask 255.255.255.0
persistence_timeout 50 ##设置会话保持时间
protocol TCP ##协议
real_server 192.168.12.148 80 { ##真实服务器
weight 1 ##权重
TCP_CHECK {
connect_port 80 ##realserver服务端口
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.12.149 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
BACKUP机器根据上面修改
###########################################################################################
客户端配置(安装apache并复制此脚本执行)
#!/bin/bash
VIP=192.168.12.147
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
echo "1">/proc/sys/net/ipv4/conf/default/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/default/arp_announce
echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p