lvs+keepalived配置
1>环境描述
LVS server1 (Master):10.0.0.5虚拟IP为:10.0.0.3(装lvs+keepalived+配置)
LVS server2 (Slave) :10.0.0.6虚拟IP为:10.0.0.3(装lvs+keepalived+配置)
WEB server1: 10.0.0.7 (安装arptables,虚拟ip10.0.0.3)
WEB server2; 10.0.0. 8 (安装arptables,虚拟ip10.0.0.3)
注意:所有集群服务器时间要一致
Lvs配置使用DR模式。
调度器DR配置:
1.绑定vip ip addr add 10.0.0.3/24 dev eth0 label eth0:0
2.下载软件 yum install -y ipvsadm
3.清空所有配置IPvsadm -C
4.添加策略指定vip ipvsadm -A -t 10.0.0.3:80 -s rr (-t指定tcp协议 -s指定rr轮询)
5.将虚拟ip指定到真实ip ipvsadm -a -t 10.0.0.3:80 -r 10.0.0.7:80 -g (-g指定dr模式 -r 指定真实服务器地址)
6.将虚拟ip指定到真实ip ipvsadm -a -t 10.0.0.3:80 -r 10.0.0.8:80 -g (-g指定dr模式 -r 指定真实服务器地址)
7./etc/init.d/ipvsadm save 保存
真实Server1的配置:
1.添加vip ip addr add 10.0.0.3/24 dev eth0 label eth0:0
在server2的配置
1.添加vip ip addr add 10.0.0.3/24 dev eth0 label eth0:0
在server1和server2上安装arptables防止客户端直接访问到真实服务器(因为ip一样)
1.下载arptables yum install -y arptables.x86_64
2.编写策略 arptables -A INPUT -d 10.0.0.3 -j DROP
Arptables -A OUTPUT -s 10.0.0.3 -j mangle --mangle-ip-s 10.0.0.7
Arptables-save >/etc/sysconfing/arptables 导入策略
至此lvs安装完毕,使用curl 10.0.0.3可以发现已经实现轮询。
下面配置高可用
在lvs server1和lvs server2上装keepalived
yum install -y keepalived
主(master)配置Keepalived配置文件
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { #全局定义
notification_email { #定义邮件
alvin@126.com #定义邮件地址
}
notification_email_from alvin@126.com #定义邮件地址
smtp_server mail.126.com #邮件服务器
smtp_connect_timeout 30 #邮件超时时间
router_id LVS_DEVEL #route_id标识
}
vrrp_instance VI_1 { #定义VRR组,
state MASTER #定义为MASTER主
interface eth0 #对外访问的网络接口
virtual_router_id 100 #虚拟路由标识。注意主从要一致
priority 100 #主从优先级,主的优先级要高于从。注意
advert_int 1 #:广播周期秒数
authentication {
auth_type PASS
auth_pass 2209
}
virtual_ipaddress {
10.0.0.3 #:虚拟VIP地址,真实环境这里应该是公网IP
}
}
virtual_server 10.0.0.3 80 { #:虚拟VIP地址 与 端口,DR架构WEB端口要和虚拟端口监听一致。否则将无法访问
delay_loop 6 #健康检查时间间隔,单位是秒
lb_algo rr #调用算法为RR
lb_kind DR #调用架构模式为DR
persistence_timeout 0 #:同一IP 50秒内的请求都发到同个real server
protocol TCP #使用TCP协议
real_server 10.0.0.7 80 {#:真实WEB服务器地址与端口
weight 1 #:转发伐值,越高调用的越多
TCP_CHECK {
connect_timeout 10 #:连接超时为10秒
nb_get_retry 3
delay_before_retry 3
connect_port 80 #连接端口为80,要和上面的保持一致
}
}
real_server 10.0.0.8 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
从(Slave)配置Keepalived配置文件
主从配置区别:
state BACKUP #状态改成BACKUP
priority 90 #优先级要低于主
interface eth1 #网络接口注意和本机对应,
其它的配置文件内容和主的一样
重启service keepalived restart
配置WEBserver真实服务器,所有的WEB脚本都一样
ip addr add 10.0.0.3/32 dev lo
cat >>/etc/sysctl.conf<<EOF
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
EOF
sysctl -p