LVS结合keepalived配置
ipvsadm –C 清空ipvsadm规则
ipvsadm –ln 查看ipvsadm规则
ifconfig eth0:0 down 卸载网卡
ip addr 查看keepalived的隐藏ip
官网:http://www.keepalived.org
Keepalived、NAT、DR分开做。
准备五台机器
主Directo1r负载均衡调度器,网卡(公网IP,实验用nat)eth0:192.168.134.140
Vip:192.168.134.150
从Director2负载均衡调度器,网卡(公网IP,实验用nat)eth0:192.168.134.137
Vip:192.168.134.150
Real server1真实服务器,网卡(公网IP,实验用nat)eth0:192.168.134.137
Real server2真实服务器,网卡(公网IP,实验用nat)eth0:192.168.134.138
Client 客户端 网卡(公网IP,实验用nat)eth0:192.168.134.136--à用来测试curl
主Directo1r调度器和从Director2调度器同时安装keepalived和ipvsadm
yum install -y keepalived
yum install –y ipvsadm --à用于查看规则IP
安装后,编辑keepalived文件
vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER #备用服务器上为BACKUP
interface eth0
virtual_router_id 51
priority 100 #备用服务器上为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.134.150
}
}
virtual_server 192.168.134.150 80 {
delay_loop 6 #(每隔10秒查询realserver状态)
lb_algo rr #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 0 #(同一IP的连接60秒内被分配到同一台realserver)
protocolTCP #(用TCP协议检查realserver状态)
real_server 192.168.134.137 80 {
weight 100 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server192.168.134.138 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
改完把配置文件推送到从Director2负载均衡器
yum install–y openssh-clients -à注意不是与scp相关的,传输的两台机器都要装这个命令,然后输入yes,再输入对方密码。
scp命令是在同一网段内,直接拷贝在远程机器(还不确定)因为主机没有scp命令。所以要yum
scp /etc/keepalived/keepalived.conf192.168.134.136:/etc/keepalived/keepalived.conf
以上为主Directo1r负载均衡器的配置文件,从上Director2负载均衡器的配置文件只需要修
改
state MASTER -> state BACKUP
priority 100 -> priority 90
配置完keepalived/后,需要开启端口转发(主从都要做):
echo 1 >/proc/sys/net/ipv4/ip_forward
然后,两个rs上执行 /usr/local/sbin/lvs_dr_rs.sh 脚本
最后,两个director上启动keepalived/服务(先主后从):
/etc/init.d/keepalived start
另外,需要注意的是,启动keepalived/服务会自动生成vip和ipvsadm规则,不需要再去执行上面提到的/usr/local/sbin/lvs_dr.sh 脚本。
//要是做了DR,再测试可能会有缓存,但目的不是这个,宕掉服务器访问不异常。
编译安装:
cd/usr/local/src/
wgethttp://www.keepalived.org/software/keepalived-1.2.6.tar.gz
tar -zxvfkeepalived-1.2.6.tar.gz
cd keepalived-1.2.6
yum install -y openssl openssl-devel
yum install –y popt-devel
./configure--prefix=/usr/local/keepalived
cp/usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
chmod +x/etc/init.d/keepalived
vim/etc/init.d/keepalived
15行修改或添加
./usr/local/keepalived/etc/sysconfig/keepalived
//下面添加
PATH="$PATH:/usr/local/keepalived/sbin"
export PATH
vim/usr/local/keepalived/etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-D-f /usr/local/keepalived/etc/keepalived/keepalived.conf"
/etc/init.d/keepalivedstart
解释请看《keepalived编译安装》
错误:
configure: error:
!!!OpenSSL is not properly installed on your system. !!!
!!!Can not include OpenSSL headers files. !!!
解决办法:yum install -y openssl openssl-devel
configure: error: Popt libraries isrequired
解决办法:yum install –y popt-devel