当前位置:网站首页>Lvs+keepalived highly available cluster

Lvs+keepalived highly available cluster

2022-06-10 11:19:00 Kiro Jun

One 、LVS+Keeplived High availability cluster

1.1 LVS

Linux Virtual server (Linux Virtual Server)
LVS It's actually equivalent to based on IP Address virtualization applications , Based on IP Load balancing of address and content request distribution presents an efficient solution


working principle :

In a LVS A service cluster usually has a master server (MASTER) And backup servers (BACKUP) Servers with two roles , But the external performance is a virtual reality IP, The master server will send VRRP Notify the backup server ;
When the backup server cannot receive VRRP When the news , When the main server is abnormal , The backup server will take over the virtual IP, Continued provision of services , Thus, high availability is guaranteed .

1.2 Keepalived

 Insert picture description here

  • Support for automatic fail over (Failover)
  • Support node health status check (Health Checking)
    Judge LVS Load balancing scheduler 、 Availability of node servers , When master When the host fails, switch to backup The node ensures normal business , When master After the failed host recovers, it is added to the cluster and the business is switched back to master node

keepalived Analysis of implementation principle :

  • keepalived use VRRP Hot backup protocol implementation Linux Multi machine hot standby function of server
  • VRRP( Virtual routing redundancy protocol ) Is a backup solution for routers
  • A hot backup group is composed of multiple routers , Through shared virtual machines IP Address provides services to the outside world
  • Each hot backup group has only one router at the same time , Other routers are redundant
  • If the current online route fails , Other routers will automatically take over the virtual router according to the set priority IP Address , Continued provision of services

The cluster should meet three characteristics :

  • Load balancing ( High concurrency )
  • health examination ( High performance )
  • Fail over ( High availability )

1.3 Keepalived The main modules of the system and their functions

keepalived There are three main modules in the architecture , Namely core、check and vrrp

  • core modular : by keepalived At the heart of , Responsible for the start of the main process 、 Maintenance and global configuration file loading and parsing .
  • vrrp modular : Is to achieve VRRP Agreed .
  • check modular : Responsible for health examination , Common ways are port checking and URL Check .

Two 、VRRP

2.1 VRRP summary

Virtual Router Redundancy Protocal, Virtual Router Redundancy Protocol

  • utilize VRRP, A set of routers ( The same LAN The interface ) Working together , But there is one in Master state , And undertake the actual data traffic forwarding task ;
  • One VRRP Multiple routers in the group share a virtual network IP Address , This address is used as the default gateway address of all hosts in the LAN ;
  • VRRP Decide which router is Master, It is responsible for receiving and forwarding packets sent to the user gateway , And the response PC For its gateway IP Of ARP request ;
  • Backup Router listening Master Router The state of , And in Master When the route fails , Take over his work

2.2 VRRP Working process of

(1) VRRP The devices in the group elect Master. Master The device is free by sending ARP message , Virtual MAC Address notification to the device or host to which it is connected , So as to undertake the task of message forwarding .
(2) Master Periodically, the device sends data to all Backup Device send VRRP Notification message .
(3) If Master The device is out of order ,VRRP In the backup group Backup Equipment re elects new Master..
(4) VRRP When the group state switches ,Master Switching from one device to another , new Master The device will immediately send the virtual machine with the virtual router MAC Address and virtual IP Free address information ARP message , Refresh... In the host or device to which it is connected MAC Table item , So as to lead the user traffic to the new Master On the equipment , The whole process is completely transparent to users .
(5) primary Master When the equipment fails to recover , If the device is IP Address owner ( Then its priority is 255), Switch directly to Master state . If the priority is less than 255, Switch first to Backup state , And its priority is restored to the priority configured before the failure .
(6)Backup The priority of the device is higher than Master Equipment time , from Backup How the device works ( Preemptive mode and non preemptive mode ) Decide whether to re elect Master.

Four 、LVS+Keepalived Construction of high availability cluster

Environmental Science :

Lord DR192.168.61.11ipvsadm、keepalived( Hot standby )
To prepare DR192.168.61.22ipvsadm、keepalived
Web1 The server 192.168.61.33
Web2 The server 192.168.61.44
vip192.168.61.12

4.1 Configure the load scheduler ( Lord :192.168.61.11)

########  To configure Keepalived master The server  ##########
  1. # Turn off firewall 
     systemctl stop firewalld.service
     setenforce 0
  2. # Installation services 
     yum -y install ipvsadm keepalived
  3. # Modify the configuration file keeplived.conf
     cd /etc/keepalived/
     cp keepalived.conf keepalived.conf.bak
     vim keepalived.conf
  
  ......
  global_defs {						# Define global parameters 
  --10 That's ok -- modify , The mail service points to local 
  	smtp_server 127.0.0.1
  --12 That's ok -- modify , Specify the server ( Router ) The name of , The names of the primary and standby servers must be different , Mainly LVS_01, Prepare for LVS_02
  	router_id LVS_01
  }
  
  vrrp_instance VI_1 {				# Definition VRRP Hot standby instance parameters 
  --20 That's ok -- modify , Specify the hot standby status , Mainly MASTER, Prepare for BACKUP
      state MASTER
  --21 That's ok -- modify , Specify bearer vip Physical interface of address 
      interface ens33
  --22 That's ok -- modify , Specify the virtual router ID Number , Each hot standby group is consistent 	
      virtual_router_id 10
  --23 That's ok -- modify , Assign priority , The higher the value, the higher the priority , Mainly 100, Prepare for 99
      priority 100
      advert_int 1					# Seconds between announcements ( heart rate )
      authentication {				# Define authentication information , Each hot standby group is consistent 
  		auth_type PASS				# Authentication type 
  --27 That's ok -- modify , Specify the authentication password , The primary and standby servers are consistent 
          auth_pass 123123
      }
      virtual_ipaddress {				# Specify the cluster vip Address 
          192.168.61.12
      }
  }
  --36 That's ok -- modify , Specify the virtual server address (VIP)、 port , Define virtual servers and Web Server pool parameters 
  virtual_server 192.168.61.12 80 {
      delay_loop 6					# Intervals between health checks ( second )
      lb_algo rr						# Specify scheduling algorithm , polling (rr)
  --39 That's ok -- modify , Specify the cluster operating mode , Direct routing (DR)
      lb_kind DR
      persistence_timeout 50			# Connection retention time ( second )
      protocol TCP					# The application service adopts  TCP agreement 
  --43 That's ok -- modify , Specify the first Web The address of the node 、 port 
      real_server 192.168.61.33 80 {
          weight 1					# Weight of nodes 
  --45 That's ok -- Delete , Add the following health check methods 		
          TCP_CHECK {
  			connect_port 80			# Add the target port to check 
  			connect_timeout 3		# Add connection timeout ( second )
  			nb_get_retry 3			# Add retry times 
  			delay_before_retry 3	# Add retry interval 
  		}
  	}
  
  ​```
  real_server 192.168.61.44 80 {		# Add a second  Web The address of the node 、 port 
      weight 1
      TCP_CHECK {
  		connect_port 80
  		connect_timeout 3
  		nb_get_retry 3
  		delay_before_retry 3
  	}
  }
  ​```
  
  }
  ## Delete the redundant configuration ##
  
  4. # Start the service 、 View virtual network card vip
     systemctl start keepalived
     ip addr show dev ens33	
  5. # adjustment proce Response parameter , close Linux Kernel redirection parameter response 
     vim /etc/sysctl.conf
     net.ipv4.ip_forward = 1
     net.ipv4.conf.all.send_redirects = 0
     net.ipv4.conf.default.send_redirects = 0
     net.ipv4.conf.ens33.send_redirects = 0
  6. # refresh 
     sysctl -p
  
###############  To configure ipvsadm ###############
  
  7. # Configure load distribution policies , And start the service 
     ipvsadm-save >/etc/sysconfig/ipvsadm
     systemctl start ipvsadm.service
  8. # Empty ipvsadm, And make strategies 
     ipvsadm -C
     ipvsadm -A -t 192.168.61.12:80 -s rr
     ipvsadm -a -t 192.168.61.12:80 -r 192.168.61.33:80 -g
     ipvsadm -a -t 192.168.61.12:80 -r 192.168.61.44:80 -g
  9. # Save settings 
     ipvsadm
     ipvsadm -ln
     ipvsadm-save >/etc/sysconfig/ipvsadm
     

Turn off firewall
 Insert picture description here
Installation services
 Insert picture description here
Modify the configuration file keeplived.conf
 Insert picture description here  Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
Start the service 、 View virtual network card vip
At this time, the service may not be visible vip Address , because 80 The port is occupied , Use “lsof -i:80” Kill the corresponding process after viewing the command , Restart and you can see
 Insert picture description here

adjustment proce Response parameter , close Linux Kernel redirection parameter response
 Insert picture description here

refresh
 Insert picture description here
Configure load distribution policies , And start the service
 Insert picture description here
Empty ipvsadm, And make strategies
 Insert picture description here
Save settings
 Insert picture description here

4.2 Configure the load scheduler ( To prepare :192.168.61.22)

  #############  To configure Keepalived master The server  ###############
  
  1. # Turn off firewall 
     systemctl stop firewalld.service
     setenforce 0
  2. # Installation services 
     yum -y install ipvsadm keepalived
  3. # Modify the configuration file keeplived.conf
     cd /etc/keepalived/
     cp keepalived.conf keepalived.conf.bak
     vim keepalived.conf
  
  ......
  global_defs {						# Define global parameters 
  --10 That's ok -- modify , The mail service points to local 
  	smtp_server 127.0.0.1
  --12 That's ok -- modify , Specify the server ( Router ) The name of , The names of the primary and standby servers must be different , Mainly LVS_01, Prepare for LVS_02
  	router_id LVS_01
  }
  
  vrrp_instance VI_1 {				# Definition VRRP Hot standby instance parameters 
  --20 That's ok -- modify , Specify the hot standby status , Mainly MASTER, Prepare for BACKUP
      state BACKUP
  --21 That's ok -- modify , Specify bearer vip Physical interface of address 
      interface ens33
  --22 That's ok -- modify , Specify the virtual router ID Number , Each hot standby group is consistent 	
      virtual_router_id 10
  --23 That's ok -- modify , Assign priority , The higher the value, the higher the priority , Mainly 100, Prepare for 99
      priority 99
      advert_int 1					# Seconds between announcements ( heart rate )
      authentication {				# Define authentication information , Each hot standby group is consistent 
  		auth_type PASS				# Authentication type 
  --27 That's ok -- modify , Specify the authentication password , The primary and standby servers are consistent 
          auth_pass 123123
      }
      virtual_ipaddress {				# Specify the cluster vip Address 
          192.168.61.12
      }
  }
  --36 That's ok -- modify , Specify the virtual server address (VIP)、 port , Define virtual servers and Web Server pool parameters 
  virtual_server 192.168.61.12 80 {
      delay_loop 6					# Intervals between health checks ( second )
      lb_algo rr						# Specify scheduling algorithm , polling (rr)
  --39 That's ok -- modify , Specify the cluster operating mode , Direct routing (DR)
      lb_kind DR
      persistence_timeout 50			# Connection retention time ( second )
      protocol TCP					# The application service adopts  TCP agreement 
  --43 That's ok -- modify , Specify the first Web The address of the node 、 port 
      real_server 192.168.61.33 80 {
          weight 1					# Weight of nodes 
  --45 That's ok -- Delete , Add the following health check methods 		
          TCP_CHECK {
  			connect_port 80			# Add the target port to check 
  			connect_timeout 3		# Add connection timeout ( second )
  			nb_get_retry 3			# Add retry times 
  			delay_before_retry 3	# Add retry interval 
  		}
  	}
  
  real_server 192.168.61.44 80 {		# Add a second  Web The address of the node 、 port 
      weight 1
      TCP_CHECK {
  		connect_port 80
  		connect_timeout 3
  		nb_get_retry 3
  		delay_before_retry 3
  	}
  }

  }
  ## Delete the redundant configuration ##
  
  4. # Start the service 、 View virtual network card vip
     systemctl start keepalived
     ip addr show dev ens33	
  5. # adjustment proce Response parameter , close Linux Kernel redirection parameter response 
     vim /etc/sysctl.conf
     net.ipv4.ip_forward = 1
     net.ipv4.conf.all.send_redirects = 0
     net.ipv4.conf.default.send_redirects = 0
     net.ipv4.conf.ens33.send_redirects = 0
  6. # refresh 
     sysctl -p
  
  ###########  To configure ipvsadm ###########
  
  7. # Configure load distribution policies , And start the service 
     ipvsadm-save >/etc/sysconfig/ipvsadm
     systemctl start ipvsadm.service
  8. # Empty ipvsadm, And make strategies 
     ipvsadm -C
     ipvsadm -A -t 192.168.61.12:80 -s rr
     ipvsadm -a -t 192.168.61.12:80 -r 192.168.61.33:80 -g
     ipvsadm -a -t 192.168.61.12:80 -r 192.168.61.44:80 -g
  9. # Save settings 
     ipvsadm
     ipvsadm -ln
     ipvsadm-save >/etc/sysconfig/ipvsadm

Turn off firewall
 Insert picture description here
Installation services
 Insert picture description here
Modify the configuration file keeplived.conf
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

Start the service 、 View virtual network card vip

 Insert picture description here
adjustment proce Response parameter , close Linux Kernel redirection parameter response
 Insert picture description here
refresh
 Insert picture description here

Configure load distribution policies , And start the service
 Insert picture description here
Empty ipvsadm, And make strategies
 Insert picture description here

Save settings
 Insert picture description here

4.3 Configure the node server web1(192.168.61.33)

1. # Turn off firewall 
systemctl stop firewalld
setenforce 0
 
2. # Install and turn on httpd service 
yum -y install httpd
systemctl start httpd
 
3. # Configure site files 
vim /var/www/html/index.html
this is kiro web!
 
4. # Configure virtual vip
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.61.12
NETMASK=255.255.255.255
 
5. # Restart network service , Open virtual network card 
systemctl restart network
ifup lo:0
ifconfig lo:0
 
6. # Set the routing 
route add -host 192.168.61.12 dev lo:0
route -n
 
7. # adjustment  proc  Response parameter 
# The added system only responds to the purpose IP For the local IP Of ARP request 
# The system does not use the original address to set ARP The source address of the request , It's physics mac The address on the IP
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
 
8. # Refresh proc Parameters 
sysctl -p

Turn off firewall
 Insert picture description here
Install and turn on httpd service
 Insert picture description here
 Insert picture description here
Configure site files
 Insert picture description here
Configure virtual vip
 Insert picture description here
Restart network service , Open virtual network card
 Insert picture description here
Set the routing
 Insert picture description here
adjustment proc Response parameter
 Insert picture description here
Refresh proc Parameters
 Insert picture description here

4.4 Configure the node server web2(192.168.61.44)

1. # Turn off firewall 
systemctl stop firewalld
setenforce 0
 
2. # Install and turn on httpd service 
yum -y install httpd
systemctl start httpd
 
3. # Configure site files 
vim /var/www/html/index.html
this is dhc web!
 
4. # Configure virtual vip
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.61.12
NETMASK=255.255.255.255
 
5. # Restart network service , Open virtual network card 
systemctl restart network
ifup lo:0
ifconfig lo:0
 
6. # Set the routing 
route add -host 192.168.61.12 dev lo:0
route -n
 
7. # adjustment  proc  Response parameter 
# The added system only responds to the purpose IP For the local IP Of ARP request 
# The system does not use the original address to set ARP The source address of the request , It's physics mac The address on the IP
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
 
8. # Refresh proc Parameters 
sysctl -p

Turn off firewall
 Insert picture description here
Install and turn on httpd service
 Insert picture description here
Configure site files
 Insert picture description here
Configure virtual vip
 Insert picture description here
Restart network service , Open virtual network card
 Insert picture description here
Set the routing
 Insert picture description here
adjustment proc Response parameter
 Insert picture description here
Refresh proc Parameters
 Insert picture description here

test

The currently used is mainly DR The server

 Insert picture description here

 Insert picture description here

Disconnect the Lord DR Server's Keepalived test

 Insert picture description here
 Insert picture description here

Test on the client side

 Insert picture description here
 Insert picture description here

原网站

版权声明
本文为[Kiro Jun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101111332788.html