LVS+keepalived 实现web高可用负载均衡集群

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVS+keepalived 实现web高可用负载均衡集群相关的知识,希望对你有一定的参考价值。

1、实验环境

4台CentOS-7.5虚拟机
web-1:192.168.18.103
web-2:192.168.18.104
keepalived-1(LVS-DR模式):192.168.18.107
keepalived-2(LVS-DR模式):192.168.18.108
vip:192.168.18.110
其中keepalived和lvs安装在同一台机器,web单独一台安装
客户端为:个人win10笔记本(192.168.18.102)
4台虚机防火墙和selinux均已关闭

2、安装keepalived+LVS

keepalived-1端:
[root@CentOS-3 ~]#
[root@CentOS-3 ~]# yum install keepalived ipvsadm
[root@CentOS-3 ~]#
[root@CentOS-3 ~]# rpm -qa keepalived ipvsadm
技术图片

keepalived-2端:
[root@CentOS-4 ~]#
[root@CentOS-4 ~]# yum install keepalived ipvsadm
[root@CentOS-4 ~]#
[root@CentOS-4 ~]# rpm -qa keepalived ipvsadm
技术图片

3、配置keepalived结合LVS
说明:配置keepalived调动LVS进行工作

keepalived-1端:
[root@CentOS-3 ~]#
[root@CentOS-3 ~]# vim /etc/keepalived/keepalived.conf #配置keepalived主节点

! Configuration File for keepalived

global_defs {
router_id Director1
}

vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.110/24 dev ens32
}
}

virtual_server 192.168.18.110 80 {
delay_loop 3
lb_algo rr
lb_kind DR
protocol TCP

real_server 192.168.18.103 80 {
    weight 1
    TCP_CHECK {
        connect_timeout 3
            }
   }

   real_server 192.168.18.104 80 {
    weight 1
    TCP_CHECK {
        connect_timeout 3
            }
   }

}

[root@CentOS-3 ~]#

keepalived-2端:
[root@CentOS-4 ~]#
[root@CentOS-4 ~]# vim /etc/keepalived/keepalived.conf #配置keepalived备节点

! Configuration File for keepalived

global_defs {
router_id Director2
}

vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.110/24 dev ens32
}
}

virtual_server 192.168.18.110 80 {
delay_loop 3
lb_algo rr
lb_kind DR
protocol TCP

real_server 192.168.18.103 80 {
    weight 1
    TCP_CHECK {
        connect_timeout 3
            }
   }

   real_server 192.168.18.104 80 {
    weight 1
    TCP_CHECK {
        connect_timeout 3
            }
   }

}

[root@CentOS-4 ~]#

4、启动keepalived使前面的配置生效

keepalived-1端:
[root@CentOS-3 ~]#
[root@CentOS-3 ~]# systemctl start keepalived #启动
[root@CentOS-3 ~]# systemctl enable keepalived #设置开机启动
[root@CentOS-3 ~]#
[root@CentOS-3 ~]# reboot #重启服务器,使keepalived+lvs联动生效

keepalived-2端:
[root@CentOS-4 ~]#
[root@CentOS-4 ~]# systemctl start keepalived
[root@CentOS-4 ~]# systemctl enable keepalived
[root@CentOS-4 ~]#
[root@CentOS-4 ~]# reboot #重启服务器,使keepalived+lvs联动生效

5、安装web服务

web-1端:
[root@CentOS ~]#
[root@CentOS ~]# yum install httpd #安装Apache服务
[root@CentOS ~]# rpm -qa httpd
httpd-2.4.6-90.el7.centos.x86_64
[root@CentOS ~]#
[root@CentOS ~]# systemctl start httpd #启动Apache
[root@CentOS ~]# systemctl enable httpd #设置开机启动
[root@CentOS ~]# lsof -i:80
技术图片
[root@CentOS ~]#
[root@CentOS ~]# cat /var/www/html/index.html #修改web默认主页便于实验
web-1
[root@CentOS ~]#

web-2端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# yum install httpd
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# systemctl start httpd
[root@CentOS-2 ~]# systemctl enable httpd
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# lsof -i:80
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# cat /var/www/html/index.html
web-2
[root@CentOS-2 ~]#

6、配置永久虚拟地址

web-1端:
[root@CentOS ~]#
[root@CentOS ~]# cd /etc/sysconfig/network-scripts/
[root@CentOS network-scripts]# cp ifcfg-lo ifcfg-lo:1
[root@CentOS network-scripts]#
[root@CentOS network-scripts]# vi ifcfg-lo:1

DEVICE=lo:1
IPADDR=192.168.18.110
NETMASK=255.255.255.255
ONBOOT=yes

[root@CentOS network-scripts]#
[root@CentOS network-scripts]# cat ifcfg-lo:1
技术图片
[root@CentOS network-scripts]#
[root@CentOS network-scripts]# systemctl restart network #重启网卡使配置永久生效
[root@CentOS network-scripts]#
[root@CentOS network-scripts]# ifconfig #查看网卡状态
技术图片

7、配置永久路由
[root@CentOS network-scripts]#
[root@CentOS network-scripts]# vi /etc/rc.local #配置永久路由

/sbin/route add host 192.168.18.110 dev lo:1 #配置文件中加入这一句

[root@CentOS network-scripts]#
[root@CentOS network-scripts]# cat /etc/rc.local #查看配置
技术图片

8、配置永久ARP
[root@CentOS network-scripts]#
[root@CentOS network-scripts]# vi /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

[root@CentOS network-scripts]#
[root@CentOS network-scripts]# cat /etc/sysctl.conf #查看配置
技术图片
[root@CentOS network-scripts]#
[root@CentOS network-scripts]# reboot #配置完以后,重启服务器使配置生效

web-2端:
web-2端和web-1端做同样的操作即可,配置完成以后同样需要重启服务器
具体配置过程省略……

9、测试LVS+keepalived配置是否成功

1)观察LVS路由条目
keepalived-1端:
[root@CentOS-3 ~]#
[root@CentOS-3 ~]# ipvsadm -L
技术图片

keepalived-2端:
[root@CentOS-4 ~]#
[root@CentOS-4 ~]# ipvsadm -L
技术图片

2)查看VIP在哪台机器上

keepalived-1端:
[root@CentOS-3 ~]#
[root@CentOS-3 ~]# ip addr
技术图片

keepalived-2端:
[root@CentOS-4 ~]#
[root@CentOS-4 ~]# ip addr
技术图片

3)客户端访问VIP
C:UsersAdministrator>
C:UsersAdministrator>curl 192.168.18.110 #客户端访问VIP
技术图片

4)关闭master(keepalived-1)上面的keepalived服务,再次访问VIP
keepalived-1端:
[root@CentOS-3 ~]#
[root@CentOS-3 ~]# systemctl stop keepalived
[root@CentOS-3 ~]#
C:UsersAdministrator>
C:UsersAdministrator>curl 192.168.18.110
技术图片

5)关闭web-1服务器,再次访问VIP
web-1端:
[root@CentOS ~]#
[root@CentOS ~]# init 0
C:UsersAdministrator>
C:UsersAdministrator>curl 192.168.18.110
技术图片

10、实验结论

使用 LVS+keepalived 成功实现了web服务器集群的高可用和负载均衡

以上是关于LVS+keepalived 实现web高可用负载均衡集群的主要内容,如果未能解决你的问题,请参考以下文章

高可用集群技术之keepalived实现lvs高可用并负载均衡web服务

keepalived+LVS实现高可用的Web负载均衡

DR+keepalived实现web群集的负载均衡和高可用性

Centos 7搭建LVS+Keepalived高可用Web服务群集

Keepalived+LVS实战案例: 单主架构实现WEB负载均衡及可用

keepalived+LVS 实现双机热备负载均衡失效转移 高性能 高可用 高伸缩性 服务器集群