Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Configure Virtual IP Failover in Linux

In a network environment, ensuring high availability and minimizing downtime is crucial. Virtual IP failover is a technique used to achieve this by automatically switching the IP address from one server to another in the event of a failure. This ensures uninterrupted service to clients and reduces the impact of server failures.

In Linux, virtual IP failover can be achieved using the Linux Virtual Server (LVS) framework, which provides load balancing and high availability features. LVS uses the IPVS (IP Virtual Server) module to distribute incoming network traffic across multiple real servers.

To configure virtual IP failover in Linux, follow these steps:

  1. Install the required packages:

    sudo apt-get install ipvsadm keepalived
  2. Configure the virtual IP (VIP) on the primary server:

    sudo ip addr add <VIP>/<CIDR> dev <interface>

    Replace <VIP> with the desired virtual IP address and <CIDR> with the appropriate subnet mask. <interface> should be the network interface on which the VIP will be assigned.

  3. Configure IPVS to distribute traffic to real servers:

    sudo ipvsadm -A -t <VIP>:<port> -s rr
    sudo ipvsadm -a -t <VIP>:<port> -r <real_server>:<port> -g

    Replace <VIP> with the virtual IP address and <port> with the desired port number. <real_server> should be the IP address of the real server to which traffic will be forwarded.

  4. Configure keepalived for failover: Edit the /etc/keepalived/keepalived.conf file and add the following configuration:

    vrrp_script chk_heartbeat {
       script "<path_to_script>"
       interval <interval_in_seconds>
       weight <weight>
    }
    
    vrrp_instance VI_1 {
       state MASTER
       interface <interface>
       virtual_router_id <router_id>
       priority <priority>
       advert_int <advertisement_interval>
       authentication {
           auth_type PASS
           auth_pass <password>
       }
       virtual_ipaddress {
           <VIP>/<CIDR> dev <interface>
       }
       track_script {
           chk_heartbeat
       }
    }

    Replace <path_to_script> with the path to a script that checks the health of the real servers. <interval_in_seconds> specifies how often the script should be executed. <weight> determines the priority of the script.

    <interface> should be the network interface on which the VIP is assigned. <router_id> is a unique identifier for the virtual router. <priority> determines the priority of the primary server. <advertisement_interval> specifies how often the primary server advertises its state. <password> is the authentication password for the VRRP protocol.

  5. Start and enable the keepalived service:

    sudo systemctl start keepalived
    sudo systemctl enable keepalived

    This will ensure that keepalived starts automatically on system boot.

With the above configuration, the virtual IP failover setup is complete. If the primary server becomes unavailable, keepalived will automatically switch the virtual IP to a backup server, ensuring continuous availability.

Examples:

Example 1: Configuring virtual IP failover with two real servers

sudo apt-get install ipvsadm keepalived

sudo ip addr add 192.168.1.100/24 dev eth0

sudo ipvsadm -A -t 192.168.1.100:80 -s rr
sudo ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.101:80 -g
sudo ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.102:80 -g

sudo vi /etc/keepalived/keepalived.conf

Example 2: Configuring virtual IP failover with health checks

sudo apt-get install ipvsadm keepalived

sudo ip addr add 192.168.1.100/24 dev eth0

sudo ipvsadm -A -t 192.168.1.100:80 -s rr
sudo ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.101:80 -g
sudo ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.102:80 -g

sudo vi /etc/keepalived/keepalived.conf

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.