一、环境:
192.168.229.11 dr1 负载均衡器 master
192.168.229.12 dr2 负载均衡器 backup
192.168.229.13 rs1 web1
192.168.229.14 rs2 web2
1、在master上安装配置Keepalived:
# yum install keepalived ipvsadm -y
# ipvsadm安装并不启动
2、在master上修改配置文件
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id Director1 #两边不一样。
}
vrrp_instance VI_1 {
state MASTER #另外一台机器是BACKUP
interface ens33 #心跳网卡
virtual_router_id 51 #虚拟路由编号,主备要一致
priority 150 #优先级
advert_int 1 #检查间隔,单位秒
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.229.100/24 dev ens33 #VIP和工作接口
}
}
virtual_server 192.168.229.100 80 { #LVS 配置,VIP,就是keepalived配置的对外地址
delay_loop 3 #服务论询的时间间隔,#每隔3秒检查一次real_server状态
lb_algo rr #LVS 调度算法
lb_kind DR #LVS 集群模式
protocol TCP
real_server 192.168.229.13 80 {
weight 1 #权重
TCP_CHECK {
connect_timeout 3 #健康检查方式,连接超时时间
}
}
real_server 192.168.229.14 80 {
weight 1
TCP_CHECK {
connect_timeout 3 #设定连接超时时间为3秒 超过视为掉线
}
}
}
3、在backup(192.168.229.12)上安装keepalived:
# yum install keepalived ipvsadm -y
# ipvsadm安装并不启动
4、backup修改配置文件:
router_id Director2
state BACKUP
priority 100
配置示例:
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id Director2
}
vrrp_instance VI_1 {
state BACKUP #另外一台机器是BACKUP
interface ens33 #心跳网卡
virtual_router_id 51
priority 100 #优先级
advert_int 1 #检查间隔,单位秒
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.229.100/24 dev ens33 #VIP和工作端口
}
}
virtual_server 192.168.229.100 80 { #LVS 配置,VIP
delay_loop 3 #服务论询的时间间隔
lb_algo rr #LVS 调度算法
lb_kind DR #LVS 集群模式
protocol TCP
real_server 192.168.229.13 80 {
weight 1
TCP_CHECK {
connect_timeout 3
}
}
real_server 192.168.229.14 80 {
weight 1
TCP_CHECK {
connect_timeout 3
}
}
}
5、master和backup上启动服务:
# systemctl enable keepalived
# systemctl start keepalived
# ipvsadm -Ln
# reboot
6、web服务器配置
1) web1和web2同配置
安装web测试站点
# yum install -y httpd && systemctl start httpd && systemctl enable httpd
# netstat -antp | grep httpd
# elinks 127.0.0.1
# vim /var/www/html/index.html
# 自定义web主页,以便观察负载均衡结果
2)配置虚拟地址
#cp /etc/sysconfig/network-scripts/{ifcfg-lo,ifcfg-lo:0}
#vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.229.100 #对外提供服务的地址
NETMASK=255.255.255.255
ONBOOT=yes
其他行注释掉
# systemctl restart network
3)配置路由
# route add 192.168.229.100 dev lo
在两台机器(RS)上,添加一个路由:route add -host 192.168.229.100 dev lo 确保如果请求的目标IP是$VIP,那么让出去的数据包的源地址也显示为$VIP
4)配置ARP
# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
# reboot
7、测试
1)观察lvs路由条目
master上 查询
# ipvsadm -Ln
[root@dr1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.229.100:80 rr
-> 192.168.229.13:80 Route 1 0 0
-> 192.168.229.14:80 Route 1 0 0
2)观察vip地址在哪台机器上
master上 查询
# ip a
ens33:
inet 192.168.229.11/24 brd 192.168.229.255 scope global noprefixroute ens33
inet 192.168.229.100/24 scope global secondary ens33
3)客户端浏览器访问vip
4)关闭master上的keepalived服务,再次访问vip
master上 关闭
# systemctl stop keepalived.service
5)关闭web1站点服务,再次访问VIP
web1
# systemctl stop httpd