1. Experimental environment
4 CentOS-7.5 virtual machines
web-1: 192.168.18.103
web-2: 192.168.18.104
keepalived-1 (LVS-DR mode): 192.168.18.107
keepalived-2 (LVS-DR mode): 192.168.18.108
vip: 192.168 .18.110,
where keepalived and lvs are installed on the same machine, and a separate installation
client for the web is: personal win10 notebook (192.168.18.102)
4 virtual machine firewalls and selinux are closed
2. Install 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. Configure keepalived combined with LVS
Description: Configure keepalived to mobilize LVS to work
Keepalived-1 end:
[root @ CentOS-3 ~] #
[root @ CentOS-3 ~] # vim /etc/keepalived/keepalived.conf #Configure keepalived master node
! 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 end:
[root @ CentOS-4 ~] #
[root @ CentOS-4 ~] # vim /etc/keepalived/keepalived.conf #Configure keepalived standby node
! 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. Start keepalived to make the previous configuration take effect
Keepalived-1 end:
[root @ CentOS-3 ~] #
[root @ CentOS-3 ~] # systemctl start keepalived #Start
[root @ CentOS-3 ~] # systemctl enable keepalived #Set boot start
[root @ CentOS-3 ~] #
[root @ CentOS-3 ~] # reboot #Restart the server to make keepalived + lvs linkage effective
Keepalived-2 end:
[root @ CentOS-4 ~] #
[root @ CentOS-4 ~] # systemctl start keepalived
[root @ CentOS-4 ~] # systemctl enable keepalived
[root @ CentOS-4 ~] #
[root @ CentOS-4 ~] # reboot #Restart the server to make keepalived + lvs linkage effective
5. Install web services
web-1:
[root @ CentOS ~] #
[root @ CentOS ~] # yum install httpd #Install Apache service
[root @ CentOS ~] # rpm -qa httpd
httpd-2.4.6-90.el7.centos.x86_64
[root @ CentOS ~] #
[root @ CentOS ~] # systemctl start httpd #Start Apache
[root @ CentOS ~] # systemctl enable httpd #Set boot start
[root @ CentOS ~] # lsof -i: 80
[root @ CentOS ~] #
[root @ CentOS ~] # cat /var/www/html/index.html #Modify the web default homepage for experiment
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. Configure permanent virtual addresses
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 #Restart the network card to configure Permanently effective
[root @ CentOS network-scripts] #
[root @ CentOS network-scripts] # ifconfig #Check the network card status
7. Configure permanent routing
[root @ CentOS network-scripts] #
[root @ CentOS network-scripts] # vi /etc/rc.local #Configure permanent routing
/ sbin / route add host 192.168.18.110 dev lo: 1 #Add this sentence to the configuration file
[root @ CentOS network-scripts] #
[root @ CentOS network-scripts] # cat /etc/rc.local #View configuration
8. Configure permanent ARP
[root @ CentOS network-scripts] #
[root @ CentOS network-scripts] # vi /etc/sysctl.conf #Permanent configuration
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 #View configuration
[root @ CentOS network-scripts] #
[root @ CentOS network-scripts] # reboot #Configuration finished After that, restart the server to make the configuration take effect
web-2 side:
web-2 side and web-1 side can do the same operation, after the configuration is completed, you need to restart the server. The
specific configuration process is omitted ...
9. Test whether LVS + keepalived configuration is successful
1) Observe the
keepalived-1 end of the LVS routing entry :
[root @ CentOS-3 ~] #
[root @ CentOS-3 ~] # ipvsadm -L
keepalived-2端:
[root@CentOS-4 ~]#
[root@CentOS-4 ~]# ipvsadm -L
2) Check which machine the VIP is on
keepalived-1端:
[root@CentOS-3 ~]#
[root@CentOS-3 ~]# ip addr
keepalived-2端:
[root@CentOS-4 ~]#
[root@CentOS-4 ~]# ip addr
3) Client access VIP
C: \ Users \ Administrator>
C: \ Users \ Administrator> curl 192.168.18.110 #Client access VIP
4) Close the keepalived service on the master (keepalived-1), and visit the VIP
keepalived-1 again :
[root @ CentOS-3 ~] #
[root @ CentOS-3 ~] # systemctl stop keepalived
[root @ CentOS-3 ~ ] #
C: \ Users \ Administrator>
C: \ Users \ Administrator> curl 192.168.18.110
5) Shut down the web-1 server and visit the VIP
web-1 end again :
[root @ CentOS ~] #
[root @ CentOS ~] # init 0
C: \ Users \ Administrator>
C: \ Users \ Administrator> curl 192.168.18.110
10. Experimental conclusion
Using LVS + keepalived successfully achieved high availability and load balancing of web server cluster