在 CentOS 上实现 iptables 的“集群”,通常不是指 iptables 本身组成集群,而是指 多台服务器共享/同步 iptables 规则,或者 用 iptables 做集群流量调度(负载均衡)。下面按常见场景给你说明。
如果你是想像 LVS 一样,用 iptables 把流量分发到多台后端服务器:
iptables + statistic 模块(简单轮询)iptables -t nat -A PREROUTING \
-p tcp --dport 80 \
-m statistic --mode nth --every 2 --packet 0 \
-j DNAT --to-destination 192.168.1.11:80
iptables -t nat -A PREROUTING \
-p tcp --dport 80 \
-m statistic --mode nth --every 2 --packet 1 \
-j DNAT --to-destination 192.168.1.12:80
✅ 适合:
❌ 不适合:
iptables + clusterip(多播式集群)CentOS 早期支持 CLUSTERIP:
iptables -A INPUT -d 192.168.1.100 -j CLUSTERIP \
--new --hashmode sourceip \
--clustermac 01:00:5e:00:00:01 \
--total-nodes 2 --local-node 1
✅ 适合:
❌ 问题:
如果你是 多台服务器要保持 iptables 规则一致:
# 导出
iptables-save > iptables.rules
# 导入
iptables-restore < iptables.rules
配合:
✅ 简单可靠
❌ 非实时
CentOS 7/8 默认用 firewalld:
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
多台同步:
如果是生产环境,不建议只用 iptables:
你可以告诉我:
我可以直接给你一套可执行的配置方案。