keepalived+LVS NAT模式集群配置与firewall防火墙配置 |
| 发表者:admin分类:应用服务2022-03-21 15:07:24 阅读[1031] |
keepalived+LVS NAT模式集群配置与firewall防火墙配置
一,环境介绍
下面的示例在 NAT 模式下使用 Keepalived 在两台服务器上实现简单的故障转移和负载平衡配置。一台服务器充当主服务器,另一台服务器充当备份。
主服务器的优先级高于备份服务器。每个服务器都有两个网络接口,其中一个接口连接到面向外部网络的一侧 (192.168.1.0/24),
另一个接口连接到可访问两个 Web 服务器的内部网络 ( 10.0.0.0/24)。
下图显示了 Keepalived master。Keepalived backup服务器 Web 服务器 的IP地址信息 。
DIP1:192.168.1.10 192.168.1.1 10.0.0.10 10.0.0.100
DIP2:192.168.1.11 10.0.0.11
websvr1 10.0.0.71websvr2 10.0.0.72
二,网络拓扑
图 NAT 模式下负载平衡的保持启用配置示例

三,keepalived配置
您可以在 主服务器上使用以下配置:/etc/keepalived/keepalived.conf
global_defs { notification_email { root@mydomain.com } notification_email_from svr1@mydomain.com smtp_server localhost smtp_connect_timeout 30}
vrrp_sync_group VRRP1 {# Group the external and internal VRRP instances so they fail over together group { external internal }}
vrrp_instance external { state MASTER interface enp0s8 virtual_router_id 91 priority 200 advert_int 1 authentication { auth_type PASS auth_pass 1215 }# Define the virtual IP address for the external network interface virtual_ipaddress { 192.168.1.1/24 }}
vrrp_instance internal { state MASTER interface enp0s9 virtual_router_id 92 priority 200 advert_int 1 authentication { auth_type PASS auth_pass 1215 }# Define the virtual IP address for the internal network interface virtual_ipaddress { 10.0.0.100/24 }}
# Define a virtual HTTP server on the virtual IP address 192.168.1.1virtual_server 192.168.1.1 80 { delay_loop 10 protocol TCP# Use round-robin scheduling in this example lb_algo rr# Use NAT to hide the back-end servers lb_kind NAT# Persistence of client sessions times out after 2 hours persistence_timeout 7200
real_server 10.0.0.71 80 { weight 1 TCP_CHECK { connect_timeout 5 connect_port 80 } }
real_server 10.0.0.72 80 { weight 1 TCP_CHECK { connect_timeout 5 connect_port 80 } }}这里采用 NAT 模式,这意味着 Keepalived 服务器代表后端服务器处理客户端的入站和出站网络流量
备份服务器的配置是相同的,只需要修改个别配置。
global_defs { notification_email { root@mydomain.com } notification_email_from svr2@mydomain.com smtp_server localhost smtp_connect_timeout 30}
vrrp_sync_group VRRP1 {# Group the external and internal VRRP instances so they fail over together group { external internal }}
vrrp_instance external { state BACKUP interface enp0s8 virtual_router_id 91 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1215 }# Define the virtual IP address for the external network interface virtual_ipaddress { 192.168.1.1/24 }}
vrrp_instance internal { state BACKUP interface enp0s9 virtual_router_id 92 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1215 }# Define the virtual IP address for the internal network interface virtual_ipaddress { 10.0.0.100/24 }}
# Define a virtual HTTP server on the virtual IP address 192.168.1.1virtual_server 192.168.1.1 80 { delay_loop 10 protocol TCP# Use round-robin scheduling in this example lb_algo rr# Use NAT to hide the back-end servers lb_kind NAT# Persistence of client sessions times out after 2 hours persistence_timeout 7200
real_server 10.0.0.71 80 { weight 1 TCP_CHECK { connect_timeout 5 connect_port 80 } }
real_server 10.0.0.72 80 { weight 1 TCP_CHECK { connect_timeout 5 connect_port 80 } }}还需要另外两个配置更改:
- 如第 8.7.1 节 "为保持潜行 NAT 模式负载平衡配置防火墙规则"中所述,在配置为负载平衡器的每台 Keepalived 服务器(主服务器和备份)上配置防火墙规则。
- 如第 8.7.2 节 "为 Keepalived NAT 模式负载平衡配置后端服务器路由"中所述,在打算与 Keepalived 负载平衡器一起使用的每台后端服务器上,为负载平衡器的内部网络接口的虚拟 IP 地址配置默认路由。
有关如何安装和配置 Keepalived 的详细信息,请参见第 8.5 节 "安装和配置 Keepalived"。
四,配置firewall防火墙与路由规则
8.7.1 为保持启用 NAT 模式负载平衡配置防火墙规则
如果将 Keepalived 配置为使用 NAT 模式对内部网络上的服务器进行负载平衡,则 Keepalived 服务器将处理所有入站和出站网络流量,
并通过使用外部网络接口的虚拟 IP 地址在传出数据包中重写真实后端服务器的源 IP 地址来隐藏后端服务器的存在。
要将 Keepalived 服务器配置为使用 NAT 模式进行负载平衡,请执行以下操作:
配置防火墙,以便外部网络端的接口与内部网络端的接口位于不同的区域中。
下面的示例演示如何在接口保留在区域中的情况下将接口移动到区域:
enp0s9 是internal区域, enp0s8 是public 区域
# firewall-cmd --get-active-zonespublicinterfaces: enp0s8 enp0s9# firewall-cmd --zone=public --remove-interface=enp0s9success# firewall-cmd --zone=internal --add-interface=enp0s9success# firewall-cmd --permanent --zone=public --remove-interface=enp0s9success# firewall-cmd --permanent --zone=internal --add-interface=enp0s9success# firewall-cmd --get-active-zonesinternalinterfaces: enp0s9publicinterfaces: enp0s8在外部网络接口上配置 NAT 模式(伪装),例如:
# firewall-cmd --zone=public --add-masqueradesuccess# firewall-cmd --permanent --zone=public --add-masqueradesuccess# firewall-cmd --zone=public --query-masqueradeyes# firewall-cmd --zone=internal --query-masqueradeno如果尚未为防火墙启用,请在外部和内部网络接口之间配置转发规则,例如:
x# firewall-cmd --direct --permanent --add-rule ipv4 filter FORWARD 0 \-i enp0s8 -o enp0s9 -m state --state RELATED,ESTABLISHED -j ACCEPT# firewall-cmd --direct --permanent --add-rule ipv4 filter FORWARD 0 \-i enp0s9 -o enp0s8 -j ACCEPT# 注意入口与出口与上一条规则相反# firewall-cmd --direct --permanent --add-rule ipv4 filter FORWARD 0 \-j REJECT --reject-with icmp-host-prohibited# firewall-cmd --reload启用对希望 Keepalived 处理的服务或端口的访问。
1,启用keepalived的vrrp协议防火墙规则,根据vrrp协议作用的接口配置规则。
# firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface eth0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT# firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 --out-interface eth0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT2, 要启用对 HTTP 的访问并使此规则在重新启动后保持不变,请输入以下命令:
# firewall-cmd --zone=public --add-service=httpsuccess# firewall-cmd --permanent --zone=public --add-service=httpsuccess
8.7.2 为保持启用 NAT 模式负载平衡配置后端服务器路由
在要与 Keepalived 负载平衡器一起使用的每个后端真实服务器上,请确保路由表包含负载平衡器内部网络接口的虚拟 IP 地址的默认路由。
例如,如果虚拟 IP 地址为 ,则可以使用 ip 命令检查路由表并设置默认路由:10.0.0.100
# ip route show10.0.0.0/24 dev enp0s8 proto kernel scope link src 10.0.0.71 # ip route add default via 10.0.0.100 dev enp0s8# ip route showdefault via 10.0.0.100 dev enp0s8 10.0.0.0/24 dev enp0s8 proto kernel scope link src 10.0.0.71 要使默认路由在重新启动后持续存在,请创建文件: enp0s8 /etc/sysconfig/network-scripts/route-enp0s8
# echo "default via 10.0.0.100 dev enp0s8" > /etc/sysconfig/network-scripts/route-enp0s8转载请标明出处【keepalived+LVS NAT模式集群配置与firewall防火墙配置】。
《www.micoder.cc》
虚拟化云计算,系统运维,安全技术服务.
| Tags: | [阅读全文...] |
最新评论