基于 Keepalived 双主模式与 LVS 实现 Web 与数据库业务分离的高可用架构实践

发布时间:2026/8/11 1:06:11
基于 Keepalived 双主模式与 LVS 实现 Web 与数据库业务分离的高可用架构实践 双主模式代理不同业务实现高可用1.实验环境#web服务已经设定完成#在rs中设定lo添加vip2 172.25.254.200、32#在rs中搭建数据库[rootrs12 ~]# dnf install mariadb-server -y[rootrs12 ~]# systemctl enable --now mariadb[rootrs12 ~]# mysqlWelcome to the MariaDB monitor. Commands end with;or\g. Your MariaDB connectionidis3Server version:10.5.27-MariaDB MariaDB Server Copyright(c)2000,2018, Oracle, MariaDB Corporation Ab and others. Typehelp;or\hforhelp. Type\ctoclearthe current input statement. MariaDB[(none)]CREATEUSERlee%identified bylee;Query OK,0rows affected(0.001sec)MariaDB[(none)]GRANT ALL ON *.* TO lee%;Query OK,0rows affected(0.001sec)#测试[rootrs1 ~]# mysql -ulee -plee -h172.25.254.10Welcome to the MariaDB monitor. Commands end with;or\g. Your MariaDB connectionidis4Server version:10.5.27-MariaDB MariaDB Server Copyright(c)2000,2018, Oracle, MariaDB Corporation Ab and others. Typehelp;or\hforhelp. Type\ctoclearthe current input statement. MariaDB[(none)]quit[rootrs1 ~]# mysql -ulee -plee -h172.25.254.20Welcome to the MariaDB monitor. Commands end with;or\g. Your MariaDB connectionidis4Server version:10.5.27-MariaDB MariaDB Server Copyright(c)2000,2018, Oracle, MariaDB Corporation Ab and others. Typehelp;or\hforhelp. Type\ctoclearthe current input statement. MariaDB[(none)]quit2.实现不同vip代理不同业务#KA1和KA2虚拟服务器配置[rootKA12 ~]# vim /etc/keepalived/keepalived.confinclude /etc/keepalived/conf.d/webserver.conf include /etc/keepalived/conf.d/datebase.conf[rootKA12 ~]# vim /etc/keepalived/conf.d/webserver.conf#172.25.254.100:80 Web 服务virtual_server172.25.254.10080{delay_loop6# 后端节点健康检查间隔单位秒每6秒检查一次RSlb_algo rr# 调度算法轮询 rr请求依次分给10、20两台机器lb_kind DR# LVS工作模式DR直接路由模式protocol TCP# 检测TCP协议real_server172.25.254.1080{weight1# 权重两台一样流量均分HTTP_GET{# 对172.25.254.10 使用HTTP方式健康检查url{path /# 访问根目录 /status_code200# 返回码200代表节点正常}connect_timeout1# 连接超时1sretry3# 失败最多重试3次delay_before_retry1# 重试间隔1s}}real_server172.25.254.2080{weight1TCP_CHECK{# 对172.25.254.20 使用TCP端口探测只探测80端口通不通不做http访问connect_timeout5retry3delay_before_retry3connect_port80}}}[rootKA1 ~]# vim /etc/keepalived/conf.d/datebase.conf#172.25.254.200:3306 MySQL 数据库服务virtual_server172.25.254.2003306{delay_loop6lb_algo rr lb_kind DR protocol TCP real_server172.25.254.103306{weight1TCP_CHECK{# MySQL只能TCP探测不能HTTP_GETconnect_timeout5retry3delay_before_retry3connect_port3306}}real_server172.25.254.203306{weight1TCP_CHECK{# MySQL只能TCP探测不能HTTP_GETconnect_timeout5retry3delay_before_retry3connect_port3306}}}[rootKA12 ~]# systemctl restart keepalived.service3.测试[rootrs2 ~]# mysql -ulee -plee -h172.25.254.200Welcome to the MariaDB monitor. Commands end with;or\g. Your MariaDB connectionidis89Server version:10.5.27-MariaDB MariaDB Server Copyright(c)2000,2018, Oracle, MariaDB Corporation Ab and others. Typehelp;or\hforhelp. Type\ctoclearthe current input statement. MariaDB[(none)][Administrator.DESKTOP-VJ307M3]➤curl172.25.254.100 RS1 -172.25.254.10[2026-01-2911:58.55]~[Administrator.DESKTOP-VJ307M3]➤curl172.25.254.100 RS2 -172.25.254.20