Centos7 deploys Keepalived dual-system hot standby + LVS high availability cluster

1. Overview of Keepalived

Keepalived was originally a powerful auxiliary tool designed specifically for LVS, mainly used to provide failover (Failover) and health check (Health Checking) function -to determine the availability of LVS load scheduler, node server, timely isolation and replacement The new server will rejoin the cluster when the failed host recovers. The official website of Keepalived is located at: http://www.keepalived.org/ . When used in a non-LVS cluster environment, Keepalived can also be used as a hot backup software.

1. Keepalived hot backup method

Keepalived adopts VRRP (Virtual Router Redundancy Protocol, Virtual Router Redundancy Protocol) hot backup protocol to realize the multi-machine hot backup function of Linux server in software. VRRP is a backup solution for routers—multiple routers form a hot standby group, which provides external services through a public virtual IP address; each hot standby group has only one main router providing services at the same time, and other routers In a redundant state, if the currently online router fails, other routers will automatically take over the virtual IP address (priority determines the order of succession) to continue providing services.

2. Deploy Keepalived dual-system hot backup

1. Preparation

CPU name operating system IP address
Master scheduler Centos7 192.168.1.1
Secondary scheduler Centos7 192.168.1.2
Client computer Win7 192.168.1.88

2. Configure the dispatch server

主调度器与辅调度器步骤一样

1) Install supporting software

挂光盘,并配置yum源

[root@master ~]# yum -y install keepalived ipvsadm
[root@master ~]# systemctl enable keepalived

2) Install httpd service

[root@master ~]# yum -y install httpd
为主调度器设置网页:
[root@master ~]# echo "<h1>This is master</h1>" > /var/www/html/index.html
为辅调度器设置网页:
[root@backup ~]# echo "<h1>This is backup</h1>" > /var/www/html/index.html

3) Back up the Keepalived main configuration file

[root@master ~]# cd /etc/keepalived/
[root@master keepalived]# cp keepalived.conf keepalived.conf.bak

Insert picture description here

4) Edit the main configuration file of Keepalived

主调度器上操作:

[root@master keepalived]# vi keepalived.conf
将原有数据删除,写入以下数据:
global_defs {
    
    
   router_id 1
}
vrrp_instance VI_1 {
    
    
    state MASTER
    interface ens33
    virtual_router_id 1
    priority 100
    advert_int 1
    authentication {
    
    
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
    
    
        192.168.1.188
    }
}
辅调度器上要稍作修改:
  router_id 1  改为  router_id 2
  state MASTER  改为  state BACKUP
  priority 100  改为  priority 99
其他一致即可

Insert picture description here

5) Enable service

[root@master keepalived]# systemctl start keepalived
[root@master keepalived]# systemctl start httpd

Check if the VIP is on this server

[root@master keepalived]# ip a

Main scheduler:
Insert picture description here
Auxiliary scheduler:
Insert picture description here
Because the priority of the auxiliary scheduler is lower than that of the main scheduler, the virtual IP is not in the auxiliary scheduler, but when the main scheduler fails, the virtual IP will drift to the auxiliary scheduler for external access

3. Verification

During this period, repeatedly disconnect and connect to the main server network card for viewing

1) Client ping drifting ip to test

If there is a short interruption, it will resume. Indicates that dual-system hot backup takes effect
Insert picture description here

2) Client access http for verification

If different content is displayed, it means that it takes effect to
Insert picture description here
open the main scheduler network card and visit again:
Insert picture description here

Three, Keepalived+LVS cluster

You can use the first experiment environment to fine-tune:
加两台Centos7机器

CPU name operating system IP address
web-1 Centos7 192.168.1.3
web-2 Centos7 192.168.1.4

1. Configure the dispatch server

主调度器和辅调度器一致:

1) Delete httpd service on the scheduler

[root@master ~]# systemctl stop httpd
[root@master ~]# yum -y remove httpd

2) Modify Keepalived main configuration file

[root@master ~]# vi /etc/keepalived/keepalived.conf
在后一行添加:
virtual_server 192.168.1.188 80 {
    
    
        delay_loop 15
        lb_algo rr
        lb_kind DR
        protocol TCP
        
                real_server 192.168.1.3 80 {
    
    
        weight 1
        TCP_CHECK {
    
    
                connect_port 80
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 4
                }
        }
                real_server 192.168.1.4 80 {
    
    
        weight 1
        TCP_CHECK {
    
    
                connect_port 80
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 4
                }
        }
}

Insert picture description here

3) Load the service module of the system kernel

[root@master ~]# modprobe ip_vs

4) View the operating status of system modules

[root@master ~]# lsmod | grep ip_vs

Insert picture description here

5) Load the ip_vs module at startup

[root@master ~]# echo "modprobe ip_vs" >> /etc/rc.local
[root@master ~]# systemctl restart keepalived

Insert picture description here

2. Configure LVS-DR cluster strategy

  • Because Keepalived also uses virtual interfaces, in order to avoid address conflicts, no binding is required

主调度器与辅调度器都要做

1) Adjust the /proc kernel parameters

[root@master ~]# cat <<END >> /etc/sysctl.conf 
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
END
[root@master ~]# sysctl -p

Insert picture description here

2) Configure LVS-DR cluster strategy

[root@master ~]# ipvsadm -A -t 192.168.1.188:80 -s rr
[root@master ~]# ipvsadm -a -t 192.168.1.188:80 -r 192.168.1.3 -g -w 1
[root@master ~]# ipvsadm -a -t 192.168.1.188:80 -r 192.168.1.4 -g -w 1
[root@master ~]# ipvsadm-save
[root@master ~]# systemctl enable ipvsadm
[root@master ~]# ipvsadm -ln

Insert picture description here

3. Web server pool configuration

挂光盘,并配置yum源

1) The web1 server and web2 server are also configured

[root@web-1 ~]# cat <<END >> /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.1.188
NETMASK=255.255.255.255
ONBOOT=yes
END
[root@web-1 ~]# systemctl restart network
[root@web-1 ~]# ip a

Insert picture description here

2) Add routing

[root@web-1 ~]# yum -y install net-tools       			  #安装路由工具
[root@web-1 ~]# echo "route add -host 192.168.1.188 dev lo:0" >> /etc/rc.local   #添加到开机自运行
[root@web-1 ~]# route add -host 192.168.1.188 dev lo:0    #临时添加
[root@web-2 ~]# route -n

Insert picture description here

3) Adjust the /proc kernel parameters and turn off the ARP response

[root@web-1 ~]# cat <<END >> /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
END
[root@web-1 ~]# sysctl -p

Insert picture description here

4) Install httpd service

Web1,Web2操作步骤一致

[root@web-1 ~]# yum -y install httpd
Web1测试页面:
[root@web-1 ~]# echo "This is web1" > /var/www/html/index.html
Web2测试页面:
[root@web-2 ~]# echo "This is web2" > /var/www/html/index.html
[root@web-1 ~]# systemctl enable httpd
[root@web-1 ~]# systemctl start httpd
[root@web-1 ~]# netstat -anpt | grep 80

4. Test lvs+Keepalived high availability cluster

1) Verify that LVS load balancing is normal

Visit 192.168.1.188 in the client browser to see if you can switch the webpage
Insert picture description here
Insert picture description here
or use the for loop statement to test:

[root@backup ~]# for i in $(seq 10);do curl http://192.168.1.188;done

Insert picture description here

2) Verify whether Keepalived dual-system hot backup is normal

Close the LVS master scheduler, whether the client can access the webpage normally
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46902396/article/details/108922030