Table of contents
1. Experimental topology diagram
3. Configuring hot standby for the scheduler
Preface
Keepalived and LVS (Linux Virtual Server) are two commonly used open source software that are often used in combination to provide high availability and load balancing solutions.
Keepalived is a software for high availability that monitors the health of a server and automatically switches to a backup server if the primary server fails. It uses the VRRP (Virtual Router Redundancy Protocol) protocol to achieve redundancy and fault recovery between servers to ensure service continuity. Keepalived can also be used with other load balancing software (such as LVS) to provide a more reliable high-availability solution.
LVS (Linux Virtual Server) is a software used to build high-performance and scalable load balancing clusters. LVS uses IP load balancing technology to distribute client requests to multiple back-end servers, improving the processing capacity and throughput of the entire system. LVS supports a variety of load balancing algorithms (such as polling, weighted polling, least connections, etc.) and can be flexibly configured according to actual needs. LVS has multiple working modes, among which DR (Direct Routing) mode is a commonly used mode. It forwards requests by modifying the destination MAC address of data packets, improving forwarding efficiency.
Taken together, the solution using Keepalived+LVS(dr) can achieve high availability and load balancing effects. Keepalived is responsible for monitoring the health status of the server and automatically switching to the backup server when the main server fails to ensure service continuity. LVS is responsible for distributing client requests to multiple back-end servers to improve system performance and scalability. By combining these two software, a stable and reliable high-availability and load-balancing system architecture can be built.
1. Experimental topology diagram
Prepare the following equipment as required
2. Configure LVS (dr) mode
Not much more to say, let’s get started!
1. Configure 2 web nodes
##安装nginx
yum -y install epel
yum -y install nginx
##分别书写两台web服务器的测试页面
echo nginx1 > /usr/share/nginx/html/index.html
echo nginx2 > /usr/share/nginx/html/index.html
##启动nginx
systemctl start nginx
test
2. Adjust the APR parameters of the web node
##调整ARP参数
vim /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
##是它生效
sysctl -p
3. Configure web node virtual IP
##配置虚拟IP地址
cd /etc/sysconfig/network-scripts/
cp ifcfg-lo ifcfg-lo:0
vim ifcfg-lo:0
##插入
DEVICE=lo:0
IPADDR=192.168.115.200
NETMASK=255.255.255.255
ONBOOT=yes
NAME=loopback:0
##重启网卡并添加路由
systemctl restart network
route add -host 192.168.115.200/32 dev lo:0
4. Configure the main dispatcher 192.168.115.128
##设置APR参数
vim /etc/sysctl.conf
##插入
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects=0
net.ipv4.conf.ens33.send_redirects = 0
##是之生效
sysctl -p
##配置网卡
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens33 ifcfg-ens33:0
##插入
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.115.200
PREFIX=24
##安装ipvsadm
yum -y install ipvsadm
##加载查看模块
modprobe ip_vs
lsmod |grep ip_vs
##启动ipvsadm
touch /etc/sysconfig/ipvsadm
systemctl start ipvsadm.service
##添加策略
ipvsadm -A -t 192.168.115.200:80 -s rr
ipvsadm -a -t 192.168.115.200:80 -r 192.168.115.131:80 -g
ipvsadm -a -t 192.168.115.200:80 -r 192.168.115.134:80 -g
##保存策略
ipvsadm-save > /etc/sysconfig/ipvsadm
##重启网卡
systemctl restart network
Browser verification
3. Configuring hot standby for the scheduler
1. Go to our load scheduler and download keepalived on both schedulers.
192.168.115.128 as master
192.168.115.131 as backup
yum -y install keepalived
2. Configure the main keepalived
vim /etc/keepalived/keepalived.conf
####此处我就设置了一个虚拟IP192.168.115.200一个虚拟IP对应着我2个真实的web服务器IP
! Configuration File for keepalived
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_DEVEL1 ##名字(主从不要一样)
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER ##身份为主
interface ens33 ##网卡
virtual_router_id 51 ##主从 id要一致
priority 100 ##优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.115.200 ###虚拟IP
}
}
virtual_server 192.168.115.200 80 { ##虚拟ip,下面对应的是2台web的真实ip
delay_loop 6
lb_algo rr ##轮询模式
lb_kind DR ##这里我们是dr模式下进行的,所以设置为dr
persistence_timeout 50
protocol TCP
real_server 192.168.115.131 80 { ##web1真实IP
weight 1
SSL_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.115.134 80 { ##web2真实IP
weight 1
SSL_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
#########如果想配置多个虚拟IP,那么参照上面的方法照猫画虎###########
3. Configure the slave load allocator (it is recommended to use scp to transfer the master's load and then modify it, which is easier) and install the ipvsadm service.
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
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_DEVEL2 ###服务名
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP ###角色
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.115.200
}
}
virtual_server 192.168.115.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.115.131 80 {
weight 1
SSL_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.115.134 80 {
weight 1
SSL_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
Start the keepalived of the active and standby respectively and check the ipvsadm policy of the active and standby, ip
##启动
systemctl start keepalived
##查看
ipvsadm -Ln
main ip
Strategy
Prepare IP
Strategy
Let me explain here, as long as the configured keepalived is correct, after starting keepalived, the policy will be generated based on the configuration file.
4. Page test
There is no problem accessing here, and it is polling. Let’s grab a packet and see that only the address 200 conforms to the DR mode.
4. Test
Shut down the main scheduler and see if polling can still be implemented.
Check the IP of the standby scheduler. IP drift indicates that the active and standby are effective.
Access the web page, it can be accessed and polling is normal
Capture packets, same as before
ok, configuration successful
Summarize
The main thing to understand here is the relationship between keepalived and ipvsadm; generally speaking, ipvsadm will also start after keepalived is started, and the strategy will be generated based on the keepalived file; relatively speaking, the configuration file of keepalived is very long and needs to be understood and remembered. You can refer to my above Configuration, the main thing here is to understand the configuration file and clarify the ideas. In this experiment, it is easy to confuse a large number of IP addresses configured.