Article From:https://www.cnblogs.com/heiguu/p/9969180.html

keepalived

 

I. Introduction of keepalive:

      KeepalivedThe software was originally designed for LVS load balancing software to manage and monitor the status of each service node in the LVS cluster system. Later, the VRRP function was added to realize high availability. Therefore, Keepalived can not only manage LVS software, but also serve as other clothes.High availability solution software for business (e.g. Nginx, Haproxy, MySQL, etc.).
     KeepalivedThe function is to detect the status of the server. If a web server goes down or fails, Keepalived will detect and remove the failed server from the system. At the same time, other servers will be used to replace the server. When the server is working properly, KeepalIved automatically adds servers to the server cluster. All these tasks are automatically completed without manual intervention. What we need to do manually is to repair the failed servers.
    Therefore, Keepalived not only has the function of configuring and managing LVS, but also has the function of checking the health of the nodes under LVS. On the other hand, it can also realize the high availability of system network services.
 
   

2. The Working Principle of Keeping alive

     At work, the primary node sends packets, and the standby node receives packets. When the standby node can not receive the data packets sent by the primary node, it starts the takeover program to take over the open source of the primary node. Standby nodes can be multiple, through priority election, but the general Keepalived system operation and maintenance work is a pair.
     KeepalivedHigh-availability pairs communicate with each other through VRRP. VRRP defines the backup through election mechanism. The priority of the backup is higher than that of the backup. Therefore, the backup node will take over the resources of the main node when the backup node is hung up, and then the backup node will take over the resources of the main node.Replacement of the main node to provide services to the outside world.
      Between Keepalived service pairs, only the main server will send VRRP broadcast packets all the time, telling the standby that it is still alive. At this time, the standby will not occupy the gun. When the main is unavailable, that is, when the standby can not listen to the broadcast packets sent by the main server, it will start relevant services to take over resources to ensure business continuity.Sex. The fastest takeover speed can be less than 1 second.
 

3. Configuration of keepalive

Preparing four virtual machines (two DS and two RS)
 

Two DS terminal configurations

 yum -y install keepalived   #Install keepalive
 yum -y install ipvsadm       #Install ipvsadm
 yum -y install httpd            #Install httpd

 Modify configuration file

 vim /etc/keepalived/keepalived.conf
 
    ! Configuration File for keepalived
    #Global configuration
    global_defs {
       notification_email {
                  }
            }
 # VRRPTo configure
    vrrp_instance VI_1 {
        state MASTER                    #Role Type MASTER | BACKUP
        interface ens33                    #NIC name
        virtual_router_id 51            #Virtual routing ID (need to be consistent with BACKUP)
        priority 100                    #priority
        advert_int 1                    #No one second to check once.
        #nopreempt                        #Non preemptive mode
 
        authentication {
            auth_type PASS                #Authentication type
            auth_pass 1111                #Authentication password
        }
        virtual_ipaddress {
            192.168.88.250                #Virtual IP (VIP)
        }
    }
    #LVSTo configure
    virtual_server 192.168.88.250 80 {    
        delay_loop 3                    #Interval of health examination
        lb_algo rr                        #Load Balancing Scheduling Algorithms
        lb_kind DR                        #Load Balancing Forwarding Rules
        protocol TCP                    #Agreement
 
        real_server 192.168.88.50 80 {    #IP and port number of real_server to monitor
                weight 1                 #weight
            TCP_CHECK {                    #Checking based on TCP protocol
                connect_timeout 3          #Connection time timeout
                retry 3                    #Reconnection times
                delay_before_retry 3    #Reconnect interval
            }
        }
        real_server 192.168.88.51 80 {
                weight 1
            TCP_CHECK {
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }   
        }   
    }
    
    

DS1Configuration (192.168.88.49)

#Start service
 
     systemctl start keepalived.service
     systemctl start ipvsadm.service
     systemctl start httpd.service
 
ifconfig ens33:0 192.168.88.250 broadcast 192.168.88.250 netmask 255.255.255.255 up
route add -host 192.168.88.250 dev ens33    
 

DS2Configuration (192.168.88.48)

#Start service
                             
     systemctl start keepalived.service
     systemctl start ipvsadm.service
     systemctl start httpd.service
 
ifconfig ens33:0 192.168.88.250 broadcast 192.168.88.250 netmask 255.255.255.255 up
route add -host 192.168.88.250 dev ens33:0
 

RS1Configuration (192.168.88.50)

yum -y install httpd
systemctl start httpd.service
ifconfig lo:0 192.168.88.250 broadcast 192.168.88.250 netmask 255.255.255.255 up
route add -host 192.168.88.250 dev lo:0
 
echo “1”>/proc/sys/net/ipv4/conf/lo/arp_ignore
echo “2”>/proc/sys/net/ipv4/conf/lo/arp_announce
echo “1”>/proc/sys/net/ipv4/conf/all/arp_ignore
echo “2”>/proc/sys/net/ipv4/conf/all/arp_announce    
 

RS2Configuration (192.168.88.51)

yum -y install httpd
systemctl start httpd.service
ifconfig lo:0 192.168.88.250 broadcast 192.168.88.250 netmask 255.255.255.255 up
route add -host 192.168.88.250 dev lo:0
 
echo “1”>/proc/sys/net/ipv4/conf/lo/arp_ignore
echo “2”>/proc/sys/net/ipv4/conf/lo/arp_announce
echo “1”>/proc/sys/net/ipv4/conf/all/arp_ignore
echo “2”>/proc/sys/net/ipv4/conf/all/arp_announce
  

IV. Testing (DS1 master, DS2 auxiliary)

1、DS1On-line

Testing on Windows Command Line

View status on DS1

 

2、Close DS1 keepalive service

DS1Journal

  DS2Journal

Continue to test on the Windows command line (the service is still running, indicating that DS2 has taken over the DS1 service)

Re DS2 view status

 

 

3、When the DS1 main service was opened again, the service was not stopped, and DS1 took over the service again.

 

 

 

4、Online servers can’t let primary and secondary servers switch back and forth, so the preemptive mode should be turned on. The primary priority is greater than the secondary priority, so that the secondary service can take over when the primary service is down.

Examples are as follows:

 DS1                vim /etc/keepalived/keepalived.conf
  priority 100   #priority
 #nopreempt          #Non preemptive mode
 

 

DS2           vim /etc/keepalived/keepalived.conf

Stop DS1 Service DS2 and take over

 

Restart DS1 service, this time DS1 has no service, or DS2 is taking over the service, test:

 

DS1

DS2

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *