DR模式负载均衡、keepalived、lvs高可用
1.配置均衡负载
机器 | IP |
---|---|
server1 | 172.25.14.1 |
server2 | 172.25.14.2 |
server3 | 172.25.14.3 |
server1
yum install -y ipvsadm
ip addr add 172.25.14.100/24 dev eth0
ipvsadm -A -t 172.25.14.100:80 -s rr
ipvsadm -a -t 172.25.14.100:80 -r 172.25.14.2:80 -g
ipvsadm -a -t 172.25.14.100:80 -r 172.25.14.3:80 -g
ipvsadm -l
ipvsadm -ln
server2
yum install -y httpd
echo server2 > /var/www/html/index.html
systemctl start httpd
server3
yum install -y httpd
echo server3 > /var/www/html/index.html
systemctl start httpd
物理机进行curl 172.25.14.100
因为server2,server3上没有ip172.25.14.100,所以无结果。
此时server1上显示server2,server3各有俩次访问
给server2,server3添加vip 172.25.14.100
此时物理机再次访问curl 172.25.14.100,server2、server3各访问5次
物理机切换到root用户
arp -d 172.25.14.100 #删除vip
for i in {1..10};do curl 172.25.14.100;done #再次访问,只访问server3
server2、server3安装arptables,设置策略,使得物理机访问时禁止掉其上的172.25.14.100
yum install -y arptables.x86_64
server2
arptables -A INPUT -d 172.25.14.100 -j DROP
arptables -A OUTPUT -s 172.25.14.100 -j mangle --mangle-ip-s 172.25.14.2
arptables-save
arptables-save > /etc/sysconfig/arptables
arptables -nL
systemctl restart arptables.service
scp /etc/sysconfig/arptables root@172.25.14.3://etc/sysconfig/arptables
server3
cd /etc/sysconfig/
vim arptables
systemctl restart arptables.service
arptables -nL
物理机再次删掉vip100并重新ping,mac地址显示如下
server1的mac地址如下
物理机再次访问到调度器
2.keepalived
server1
yum install -y keepalived
yum install -y mailx
vim /etc/keepalived/keepalived.conf #编辑配置文件
删除server1上的vip
清除策略,重启keepalived,vip再次出现在server1上
关闭server3的apache
物理机访问
server1上出现邮件,报道server3down掉
server3重启apache服务
物理机再次访问
server1上出现第二封邮件,显示server3 up
3.lvs高可用
增加server4:172.25.14.4
yum install -y keepalived
yum install -y ipvsadm.x86_64
修改keepalived配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 14
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.14.100
}
}
virtual_server 172.25.14.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 50
protocol TCP
real_server 172.25.14.2 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.25.14.3 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
开启keepalived服务,便可显示出ipvsadm策略
systemctl start keepalived.service
ipvsadm -ln
server1关闭keepalived
物理机访问curl 172.25.14.100
查看server4日志,显示由BACKUP变为MASTER
server1开启keepalived后查看server4日志,由MASTER 变为BACKUP
server1的日志显示变为MASTER