LVS+keepalived 的DR模式的两种做法
LVS DR模式搭建
准备工作
三台机器:
1 2 3 4 5 | dr:192.168.13.15 rs1:192.168.13.16 rs2: 192.168.13.17 vip:192.168.13.100 |
修改DR上的/etc/sysctl.conf文件
1 | net.ipv4.ip_forward=0改为net.ipv4.ip_forward=1 |
第一种做法lo
Dr上的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | ! Configuration File for Keepalived ! --------------------------------------------------------------------------- ! GLOBAL ! --------------------------------------------------------------------------- global_defs { ! this is who emails will go to on alerts notification_email { wan@os.cn ! add a few more email addresses here if you would like } notification_email_from wan@os.cn ! mail relay server smtp_server 127.0.0.1 smtp_connect_timeout 30 ! each load balancer should have a different ID ! this will be used in SMTP alerts, so you should make ! each router easily identifiable router_id LVS_13.100 } vrrp_instance VI1_LVS_CN { state MASTER interface eth1 ! interface to run LVS sync daemon on lvs_sync_daemon_interface eth1 !mcast_src_ip 192.168.13.15 virtual_router_id 100 priority 100 advert_int 1 smtp_alert authentication { auth_type PASS auth_pass qw_web } ! these are the IP addresses that keepalived will setup on this ! machine. Later in the config we will specify which real ! servers are behind these IPs without this block, keepalived ! will not setup and takedown any IP addresses virtual_ipaddress { 192.168.13.100 } } virtual_server 192.168.13.100 80 { ! interval between checks in seconds delay_loop 5 ! use weighted least connection as a load balancing algorithm lb_algo wrr ! lvs_sched wrr ! we are doing Direct Routing lb_kind DR ! lvs_method DR protocol TCP ! WEB01 real_server 192.168.13.16 80 { weight 100 HTTP_GET { url { path /.keepalived status_code 200 } connect_timeout 10 nb_get_retry 3 delay_before_retry 5 } } ! WEB02 real_server 192.168.13.17 80 { weight 100 HTTP_GET { url { path /.keepalived status_code 200 } connect_timeout 10 nb_get_retry 3 delay_before_retry 5 } } } |
两台Rs上的这配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [root@local shell]# more realserver.sh #!/bin/bash vip=192.168.13.100 case "$1" in start) ifdown lo ifup lo ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up /sbin/route add -host $vip lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce ;; stop) ifdown lo ifup lo /sbin/route del -host $vip lo:0 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0 |
第二种做法iptables
Dr上的配置(同第一种一致)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | ! Configuration File for Keepalived ! --------------------------------------------------------------------------- ! GLOBAL ! --------------------------------------------------------------------------- global_defs { ! this is who emails will go to on alerts notification_email { wan@os.cn ! add a few more email addresses here if you would like } notification_email_from wan@os.cn ! mail relay server smtp_server 127.0.0.1 smtp_connect_timeout 30 ! each load balancer should have a different ID ! this will be used in SMTP alerts, so you should make ! each router easily identifiable router_id LVS_13.100 } vrrp_instance VI1_LVS_CN { state MASTER interface eth1 ! interface to run LVS sync daemon on lvs_sync_daemon_interface eth1 !mcast_src_ip 192.168.13.15 virtual_router_id 100 priority 100 advert_int 1 smtp_alert authentication { auth_type PASS auth_pass qw_web } ! these are the IP addresses that keepalived will setup on this ! machine. Later in the config we will specify which real ! servers are behind these IPs without this block, keepalived ! will not setup and takedown any IP addresses virtual_ipaddress { 192.168.13.100 } } virtual_server 192.168.13.100 80 { ! interval between checks in seconds delay_loop 5 ! use weighted least connection as a load balancing algorithm lb_algo wrr ! lvs_sched wrr ! we are doing Direct Routing lb_kind DR ! lvs_method DR protocol TCP ! WEB01 real_server 192.168.13.16 80 { weight 100 HTTP_GET { url { path /.keepalived status_code 200 } connect_timeout 10 nb_get_retry 3 delay_before_retry 5 } } ! WEB02 real_server 192.168.13.17 80 { weight 100 HTTP_GET { url { path /.keepalived status_code 200 } connect_timeout 10 nb_get_retry 3 delay_before_retry 5 } } } |
两台Rs上的这配置
1 2 | [root@local shell]# iptables -t nat -A PREROUTING -p tcp -d 192.168.13.100 --dport 80 -j REDIRECT [root@local shell]# iptables -t nat -A OUTPUT -p tcp -d 192.168.13.100 --dport 80 -j REDIRECT |
以上两种方式均可实现DR模式。
分类:
keepalived相关
, linux
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端