快乐de馒头 2020-04-11
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端:
[ ~]#
[ ~]# yum install keepalived ipvsadm
[ ~]#
[ ~]# rpm -qa keepalived ipvsadm
keepalived-2端:
[ ~]#
[ ~]# yum install keepalived ipvsadm
[ ~]#
[ ~]# rpm -qa keepalived ipvsadm
3、配置keepalived结合LVS
说明:配置keepalived调动LVS进行工作
keepalived-1端:
[ ~]#
[ ~]# 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 } }
}
[ ~]#
keepalived-2端:
[ ~]#
[ ~]# 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 } }
}
[ ~]#
4、启动keepalived使前面的配置生效
keepalived-1端:
[ ~]#
[ ~]# systemctl start keepalived #启动
[ ~]# systemctl enable keepalived #设置开机启动
[ ~]#
[ ~]# reboot #重启服务器,使keepalived+lvs联动生效
keepalived-2端:
[ ~]#
[ ~]# systemctl start keepalived
[ ~]# systemctl enable keepalived
[ ~]#
[ ~]# reboot #重启服务器,使keepalived+lvs联动生效
5、安装web服务
web-1端:
[ ~]#
[ ~]# yum install httpd #安装Apache服务
[ ~]# rpm -qa httpd
httpd-2.4.6-90.el7.centos.x86_64
[ ~]#
[ ~]# systemctl start httpd #启动Apache
[ ~]# systemctl enable httpd #设置开机启动
[ ~]# lsof -i:80
[ ~]#
[ ~]# cat /var/www/html/index.html #修改web默认主页便于实验
web-1
[ ~]#
web-2端:
[ ~]#
[ ~]# yum install httpd
[ ~]#
[ ~]# systemctl start httpd
[ ~]# systemctl enable httpd
[ ~]#
[ ~]# lsof -i:80
[ ~]#
[ ~]# cat /var/www/html/index.html
web-2
[ ~]#
6、配置永久虚拟地址
web-1端:
[ ~]#
[ ~]# cd /etc/sysconfig/network-scripts/
[ network-scripts]# cp ifcfg-lo ifcfg-lo:1
[ network-scripts]#
[ network-scripts]# vi ifcfg-lo:1
DEVICE=lo:1
IPADDR=192.168.18.110
NETMASK=255.255.255.255
ONBOOT=yes
[ network-scripts]#
[ network-scripts]# cat ifcfg-lo:1
[ network-scripts]#
[ network-scripts]# systemctl restart network #重启网卡使配置永久生效
[ network-scripts]#
[ network-scripts]# ifconfig #查看网卡状态
7、配置永久路由
[ network-scripts]#
[ network-scripts]# vi /etc/rc.local #配置永久路由
/sbin/route add host 192.168.18.110 dev lo:1 #配置文件中加入这一句
[ network-scripts]#
[ network-scripts]# cat /etc/rc.local #查看配置
8、配置永久ARP
[ network-scripts]#
[ 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
[ network-scripts]#
[ network-scripts]# cat /etc/sysctl.conf #查看配置
[ network-scripts]#
[ network-scripts]# reboot #配置完以后,重启服务器使配置生效
web-2端:
web-2端和web-1端做同样的操作即可,配置完成以后同样需要重启服务器
具体配置过程省略……
9、测试LVS+keepalived配置是否成功
1)观察LVS路由条目
keepalived-1端:
[ ~]#
[ ~]# ipvsadm -L
keepalived-2端:
[ ~]#
[ ~]# ipvsadm -L
2)查看VIP在哪台机器上
keepalived-1端:
[ ~]#
[ ~]# ip addr
keepalived-2端:
[ ~]#
[ ~]# ip addr
3)客户端访问VIP
C:\Users\Administrator>
C:\Users\Administrator>curl 192.168.18.110 #客户端访问VIP
4)关闭master(keepalived-1)上面的keepalived服务,再次访问VIP
keepalived-1端:
[ ~]#
[ ~]# systemctl stop keepalived
[ ~]#
C:\Users\Administrator>
C:\Users\Administrator>curl 192.168.18.110
5)关闭web-1服务器,再次访问VIP
web-1端:
[ ~]#
[ ~]# init 0
C:\Users\Administrator>
C:\Users\Administrator>curl 192.168.18.110
10、实验结论
使用 LVS+keepalived 成功实现了web服务器集群的高可用和负载均衡