Keepalived学习笔记 (2)

3.3.2 LVS
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
qingean@163.com
}
notification_email_from admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_MASTER
}
vrrp_instance VI_1 {
state MASTER #BACKUP上修改为BACKUP
interface ens4
virtual_router_id 51
priority 100 #BACKUP上修改为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
193.168.140.80
}
}
virtual_server 193.168.140.80 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.255
protocol TCP
real_server 193.168.140.152 80 {
weight 10
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 193.168.140.224 80 {
weight 10
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

3.3.3 RS
为所有RS修改sysctl.conf
net.ipv4.conf.lo.arp_ignore= 1
net.ipv4.conf.lo.arp_announce= 2
net.ipv4.conf.all.arp_ignore= 1
net.ipv4.conf.all.arp_announce= 2
net.ipv4.ip_forward= 1
执行/sbin/ifconfig lo:0 193.168.140.80 broadcast 193.168.140.80 netmask 255.255.255.255
可用route –n查看是否成功
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 193.168.1.1 0.0.0.0 UG 100 0 0 ens4
193.168.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens4
193.168.140.80 0.0.0.0 255.255.255.255 UH 0 0 0 lo
若不成功执行/sbin/route add -host 193.168.140.80 dev lo:0
3.4 验证方法
3.4.1 所有机器关闭防火墙:
systemctl stop firewalld
3.4.2 所有RS写入测试页和开启httpd服务
RS1:echo “RS1″ > /var/www/html/index.html
RS2:echo “RS2″ > /var/www/html/index.html
systemctl start httpd
3.4.3 主副LVS开启keepalived服务
systemctl start keepalived
3.4.4 访问
浏览器访问#
刷新会轮番显示RS1或RS2
3.4.5 查看当前测试机的访问请求被转发到哪个服务器
ipvsadm –lcn
IPVS connection entries
pro expire state source virtual destination
TCP 01:54 FIN_WAIT 10.167.225.60:53882 193.168.140.80:80 192.168.102.163:80
TCP 00:37 NONE 10.167.225.60:0 193.168.140.80:80 192.168.102.163:80
3.4.6 测试
模拟宕掉主LVS,服务器照常工作,再宕掉Web1,这时只会显示Web2,这样就实现IP负载均衡,高可用集群。当主LVS恢复后,会切换成主动服务器,如果Keepalived监控模块检测web故障恢复后,恢复的主机又将此节点加入集群系统中。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfdpw.html