Keepalived+LVS (DR mode) installation and deployment practical detailed tutorial

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

The design purpose of Keepalived is to build a highly available LVS load balancing cluster. You can call the ipvsadm tool to create virtual servers and manage server pools, not just for dual-machine hot standby. Using Keepalived to build an LVS cluster is easier and easier to use. The main advantages are as follows: implement hot standby switching for the LVS load balancing scheduler to improve availability; perform health checks on nodes in the server pool, automatically remove failed nodes, and rejoin after recovery .
Note: When using Keepalived to build an LVS cluster, the ipvsadm management tool is also required, but most of the work will be done automatically by Keepalived, and there is no need to manually execute ipvsadm (except for viewing and monitoring the cluster)


1. Environmental preparation

Master Scheduler: IP: 10.101.37.22 VIP: 10.101.37.151
Slave Scheduler: IP: 10.101.37.28 VIP: 10.101.37.151
Application Node Server 1: IP: 10.101.37.27 lo:0: 10.101.37.151
Application Node Server 2: IP: 10.101.37.30 lo:0: 10.101.37.151

LVS DR模式调度器与应用服务器不要使用同一服务器,易出现双机互发广播风暴
LVS DR模式调动器与应用服务器必须处于同一网段中

Two-machine exchange broadcast wind:

When A is MASTER, the packet with destination IP 21 is sent to server A, LVS A has two routing options, "routing to Local" or "modifying MAC routing to B". After A modifies the MAC route to B, although the LVS on server B is in BACKUP state, it will still process this packet instead of being directly processed by the service of the application layer. The LVS of node B also has two choices, "modify MAC route to A" or "route to Local". When A is routed to B, and B is routed to A, packets will be sent repeatedly to each other.

A compromise solution:

In the configuration of the standby scheduler, the scheduling algorithm uses wrr, and then the weight of the main real_server in the virtual_server is set to 0, or the configuration of the main real_server is deleted directly.
The configuration of the main scheduler remains unchanged. Packets forwarded from the primary scheduler to the standby scheduler will no longer be forwarded by the standby scheduler to the primary scheduler, so a loop will not be formed.


2. Scheduler installation (Keepalived+LVS)

1. Modify sysctl.conf configuration

For the DR cluster mode, since the LVS load scheduler and each node need to share the VIP address, the redirection parameter of the Linux kernel should be turned off.

[root@localhost ~]# vi /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens160.send_redirects = 0
[root@localhost ~]# sysctl -p

2. Install Keepalived+LVS

[root@localhost ~]# yum -y install keepalived ipvsadm

3. Modify the Keepalived configuration

Taking the master scheduler as an example, the slave scheduler is similar

[root@localhost ~]# vi /etc/keepalived/keepalived.conf
! Configuration Fid_e for keepalived

global_defs {
    
    
   # router_id主机为master、备机为backup
   router_id master
}

vrrp_instance VI_1 {
    
    
	# state主机为MASTER、备机为BACKUP
    state MASTER
    # 绑定网卡
    interface ens160
    # 主备应保持一致
    virtual_router_id 66
    priority 100
    advert_int 1
    authentication {
    
    
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    
    
        10.101.37.151
    }
}
#DR模式:VIP端口应与RS端口保持一致
virtual_server 10.101.37.151 80 {
    
    
    delay_loop 6
    #为了测试轮休效果persistence_timeout可以注释掉,生产环境再开启
    #persistence_timeout 50
    lb_algo rr
    lb_kind DR
    protocol TCP

    real_server 10.101.37.30 80 {
    
    
        weight 1
        TCP_CHECK {
    
    
            connect_timeout 5
            connect_port 80
        }
    }
    real_server 10.101.37.27 80 {
    
    
        weight 1
        TCP_CHECK {
    
    
            connect_timeout 5
            connect_port 80
        }
    }

}


4. Turn on the Keepalived service

[root@localhost ~]# systemctl start keepalived
[root@localhost ~]# systemctl enable keepalived

5. Verify configuration

insert image description here

insert image description here


3. Application node installation

1. Modify sysctl.conf configuration

[root@localhost ~]# vi /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
[root@localhost ~]# sysctl -p

2. Configure the network card and routing

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=10.101.37.151
NETMASK=255.255.255.255
ONBOOT=yes
[root@localhost network-scripts]# ifup lo:0

[root@localhost network-scripts]# vi /etc/rc.local
/sbin/route add -host 10.101.37.151 dev lo:0

[root@localhost network-scripts]#route add -host 10.101.37.151 dev lo:0

4. Test

The httpd used by the application node is convenient for testing, and the content of the home page is changed to the server address: 27, 30
By visiting the VIP, you can see the effect of the rotation
insert image description here

You can also see the effect through the lvs call link of the 28 server
insert image description here


end

  • Thank you for your patience in reading. If you have any suggestions, please private message or comment.
  • If there is something to gain, please bother to support, follow, like, comment, and collect. The blogger will update it frequently and make progress together with everyone.

Guess you like

Origin blog.csdn.net/qq359605040/article/details/129584543