Preparations Environment VIP 192.168.12.100 LVs_director_master 192.168.12.4 LVs_director_slave 192.168.12.8 nginx1 192.168.12.2 Nginx2 192.168.12.3 Tomcat1 192.168.12.6 Tomcat2 192.168.12.7

###What did you do today?

LVS as a load balancer, all requests are sent to LVS first. Therefore, LVS is in a very important position. If the LVS server breaks down, the backend Web server cannot provide services, causing serious impact.

To prevent the load balancing server from going down, you need to set up a backup machine. Both the master server and the backup machine run high availability monitors that monitor each other’s health by sending messages such as “I am alive”. When the backup machine cannot receive the message within a certain period of time, it takes over the service IP address of the primary server and continues to provide load balancing services. When the secondary server receives the message “I am alive” from the primary server, it releases the service IP address and the primary server starts to provide load balancing services again.

### What is Keepalived?

Keepalived is a service software for cluster management to ensure high availability of a cluster and to prevent a single point of failure.

The purpose of Keepalived is to detect the state of the web server. If a web server fails or fails, Keepalived will detect it and remove the faulty web server from the system. When the web server works properly, Keepalived automatically adds the Web server to the server farm. All the work is done automatically, without human intervention. All you need to do is to repair the faulty Web server.

### Keepalived works

Keepalived is based on Virtual Router Redundancy Protocol (VRRP), which stands for Virtual Router Redundancy Protocol.

Virtual route redundancy protocol can be regarded as a protocol to realize router high availability. That is, N routers providing the same function form a router group, which has a master and multiple backup. The master has a VIP that provides services (the default route is this VIP for other machines in the LAN where the router is located). The master sends multicast packets. If the backup fails to receive VRRP packets, the master considers that it has broken down. In this case, you need to elect a backup as master based on the VRRP priority to ensure high availability of the router.

Keepalived consists of three modules, namely core, Check and VRRP. The core module is the core of Keepalived, which is responsible for the startup and maintenance of the main process as well as the loading and parsing of the global configuration file. Check is responsible for health checks, including common checks. VRRP mode is used to implement the VRRP protocol.

###LVS+Keepalived to implement the active/standby process

The initial state

The host is down, and the standby machine provides services

The host to restore

  • To configure keepalived for 192.168.12.4 and 192.168.12.8, see my previous blog [Diary Report 2017-12-28] about FastDFS * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Keepalived After the basic configuration is complete, check whether the system service exists

chkconfig –list

Configuring log Files

1. Will keepalived log output to local0 vim/etc/sysconfig/keepalived, KEEPALIVED_OPTIONS = “0” – D – D – S

2. Add local0.* /var/log/keepalive.log to /etc/rsyslog.conf

3. Restart the Keepalived and Rsyslog services: service rsyslog restart service keepalived restart 4. Go under /var/log/ and find keepalive.log

5. Check keepalived.log. tail -f keepalived.log

Chmod +x /etc/init.d/keepalived

Keepalived # # # # configuration

  • Modify/etc/keepalived/keepalived. Conf, before we look at what wrote in it.
! Configuration File forkeepalived global_defs { notification_email { [email protected] [email protected] [email protected] } Notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 Router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 Authentication {auth_type PASS auth_pass 1111} virtual_ipaddress {192.168.200.16 192.168.200.17 192.168.200.18}} Virtual_server 192.168.200.100 443 {delay_loop 6 lb_algo rr lb_kind NAT nat_mask 255.255.255.0 persistence_timeout 50 Protocol TCP real_server 192.168.201.100 443 {weight 1 SSL_GET {url {path/digest ff20ad2481f97b1754ef3e12ecd3a9cc}  url { path /mrtg/ digest 9b3a0c85a887a256d6939da88aabd8cd} connect_timeout 3 nb_get_retry 3 delay_before_retry 3}}} virtual_server 10.10.10.2 1358 {delay_loop 6 lb_ALgo RR Lb_kind NAT persistence_timeout 50 protocol TCP sorry_server 192.168.200.200 weight 1 HTTP_GET { url { path /testurl/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91C463FAC6334D} connect_timeout 3 nb_get_retry 3 delay_before_retry 3}} real_server 192.168.200.3 1358 {weight  1 HTTP_GET { url { path /testurl/test.jsp digest 640205b7b0fc66c1ea91c463fac6334c
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91C463FAC6334C} connect_timeout 3 nb_get_retry 3 delay_before_retry 3}}} virtual_server 10.10.10.3 1358 { Delay_loop 3 Lb_ALgo RR Lb_kind NAT nat_mask 255.255.255.0 Persistence_TIMEOUT 50 protocol TCP real_server 192.168.200.4  1358 { weight 1 HTTP_GET { url { path /testurl/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91C463FAC6334D} connect_timeout 3 nb_get_retry 3 delay_before_retry 3}} real_server 192.168.200.5 1358 {weight  1 HTTP_GET { url { path /testurl/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
Copy the code
  • We refer to the official configuration and make corresponding modifications to the keepalive. conf of the main LVS, as follows:
! Configuration File for keepalived

global_defs {
   notification_email {
    # Email address sent when failure occurs
    #[email protected]                                   
   }
   # Which email address to use
   #notification_email_from [email protected]
   # Sender server
   #smtp_server xxx.com                                  
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script check_lvs {
    script "/etc/keepalived/lvs_check.sh"         ## Monitor script
    interval 2                                      Time interval, 2 seconds
    weight -20                                        # # the weight
}

vrrp_instance VI_1 {
    state MASTER             # indicate primary LVS
    interface eth0           # HA check port
    virtual_router_id 51     The virtual_router_id of the active and standby devices must be the same
    priority 100             The secondary LVS are slightly smaller than the primary LVS
    advert_int 1             # VRRP Multicast Number of broadcast cycles in seconds
    authentication {         # define authentication
        auth_type PASS       The authentication mode is password authentication
        auth_pass 1111       # define password
    }

   track_script {
        check_lvs        # monitor script
   }

    virtual_ipaddress {      # define the VIP
        192.168.12.100        # Multiple VIPs can be added line feed}} virtual_server 192.168.12.100 80 {delay_loop 6Check the status of the realServer every 6 seconds
    lb_algo wlc        # The scheduling algorithm is weighted minimum number of connections
    lb_kind DR         LVS works in DR(direct routing) modeNat_mask 255.255.255.0 persistence_timeout 50# Connections with the same IP are allocated to the same realserver within 50 seconds.
    protocol TCP            Use TCP to monitor the status of the realServerReal_server 192.168.12.2 80 {# define the realservers
        weight 3                       # define weight
        TCP_CHECK {  # Note the Spaces between TCP_CHECK and {, if not, only the first realserver will be added
            connect_timeout 3          No response for three seconds timed outNb_get_retry 3 delay_before_retry 3 connect_port 80}} real_server 192.168.12.380 {weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }Copy the code
  • Modify the standby under the LVS/etc/keepalived/keepalived conf file. Note the following: Change state to BACKUP, priority to lower than MASTER, and virtual_router_id to the same as MASTER.

  • In the /etc/keepalived/ directory of the active and standby LVS, create the lvs_check.sh script as follows:

#! /bin/sh
aa=`ipvsadm -ln`  
str="Route"  
bb=`echo $aa|grep $str|wc -l`  
if [ $bb= 0];then  
    service lvsdr start  
    sleep 3  
    aa=`ipvsadm -ln`  
    bb=`echo $aa|grep $str|wc -l`  
    if [ $bb= 0];then  
        killall keepalived  
    fi  
fi 
Copy the code
  • Disable keepalived and LVSDR services in active and standby LVS first.

service lvsdr stop service keepalived stop

  • Then enable the Keepalived service in the active and standby LVS.

We then checked with ipvsadm to see if LVS was enabled and found that LVS was enabled!

  • Enable the nginx and LVSDR services of 192.168.12.2 and 192.16812.3.

  • Enable tomcat for 192.168.12.6 and 192.168.12.7

  • Then we look at the virtual IP through the IP a command

Virtual IP address of the active LVS device

Virtual IP address of the standby LVS device

  • This does not seem to be what we expected. We can turn off the firewall first. We first turned off keepalived for the master LVS and then turned off Keepalived for the standby LVS. Then restart the Keepalived service for the primary and standby LVS. After rebooting, we found that the eth0 node of the standby LVS had no virtual ip192.168.12.100, as I expected!

  • Log in to 192.168.12.100 for the first time after the active and standby LVS are successfully started.

  • Second access to 192.168.12.100

  • When we stopped the main LVS, 192.168.12.100 was still accessible. In the standby LVS, IP A is used to check the virtual IP address 192.168.12.100 on node eth0

  • Then the master LVS is unlocked, and the control is back to the master LVS.

###Summary

Tomorrow we will summarize the problem and then implement LVS+Keepalived dual main mode.