当前位置:网站首页>Keepalived configure virtual IP

Keepalived configure virtual IP

2022-06-09 04:18:00 Little black notes

YUM install

# yum install 
yum -y install keepalived
#  Check the installed version 
rpm -qa keepalived
#  Check the installation path 
rpm -ql keepalived

Or use the source code to install

Download here https://www.keepalived.org/download.html
 Insert picture description here

#  Installation dependency 
yum -y install gcc openssl-devel libnfnetlink-devel
 Download source package 
wget https://www.keepalived.org/software/keepalived-2.2.7.tar.gz #  Here, the version number 2.2.7 Modify the desired version number 
3) decompression 
tar -zxvf keepalived-2.2.7.tar.gz -C /usr/src
4) Compilation and installation 
cd /usr/src/keepalived-2.2.7/
./configure && make -j 4 && make install

Here are three hosts to make a virtual IP Get an example (2021 year )

Modify the configuration file contents /etc/keepalived/keepalived.conf
Start service usage instruction :service keepalived restart

# master Node configuration content :
! Configuration File for keepalived
vrrp_instance VI_1 {
    
    state MASTER # It's certain that master node , Multiple master I don't know if my brain will crack , Didn't try 
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    unicast_src_ip 192.168.163.134
    unicast_peer {
    
        192.168.163.132
        192.168.163.133
    }
    authentication {
    
        auth_type PASS
        auth_pass 1111 # The code is random , Each host requires the same 
    }
    virtual_ipaddress {
    
        192.168.163.135
    }
}

# node1 Node configuration content :
! Configuration File for keepalived
vrrp_instance VI_1 {
    
    state BACKUP # It's certain that backup node 
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    unicast_src_ip 192.168.163.132
    unicast_peer {
    
        192.168.163.133
        192.168.163.134
    }
    authentication {
    
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    
        192.168.163.135
    }
}

# node2 Node configuration 
! Configuration File for keepalived
vrrp_instance VI_1 {
    
    state BACKUP # It's certain that backup node 
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    unicast_src_ip 192.168.163.133
    unicast_peer {
    
        192.168.163.132
        192.168.163.134
    }
    authentication {
    
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    
        192.168.163.135
    }
}

Here is an example of two machines (2022 year )

# master Node configuration content :
! Configuration File for keepalived

global_defs {
    
   notification_email {
    
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

 vrrp_script web {
    
        script "/etc/keepalived/check_web_port.sh"
        interval 1
        weight -20

vrrp_instance VI_1 {
    
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
    
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    
        192.168.163.11
    }
    track_script {
    
        web
    }
}

virtual_server 192.168.163.11 8080 {
    
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    persistence_timeout 1
    protocol TCP

    real_server 192.168.163.136 8080 {
    
         weight 1
         TCP_CHECK {
    
         connect_timeout 1
         nb_get_retry 3
         delay_before_retry 3
         connect_port 8080
         }
    }

    real_server 192.168.163.137 8080 {
    
         weight 1
         TCP_CHECK {
    
         connect_timeout 1
         nb_get_retry 3
         delay_before_retry 3
         connect_port 8080
         }
    }
}


# node Node configuration content :
! Configuration File for keepalived

global_defs {
    
   notification_email {
    
     [email protected]rewall.loc
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

 vrrp_script web {
    
        script "/etc/keepalived/check_web_port.sh"
        interval 1
        weight -20

vrrp_instance VI_1 {
    
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
    
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    
        192.168.163.11
    }
    track_script {
    
        web
    }
}

virtual_server 192.168.163.11 8080 {
    
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    persistence_timeout 1
    protocol TCP

    real_server 192.168.163.136 8080 {
    
         weight 1
         TCP_CHECK {
    
         connect_timeout 1
         nb_get_retry 3
         delay_before_retry 3
         connect_port 8080
         }
    }

    real_server 192.168.163.137 8080 {
    
         weight 1
         TCP_CHECK {
    
         connect_timeout 1
         nb_get_retry 3
         delay_before_retry 3
         connect_port 8080
         }
    }
}


# Scripts involved  /etc/keepalived/check_web_port.sh"
#!/bin/bash
count=`nmap 127.0.0.1|grep 8080/tcp|wc -l`
if [ $count != 0 ]
then
    exit 0
else
    exit 1
fi
原网站

版权声明
本文为[Little black notes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090411447147.html