For more information: man 8 apk
vpc-nat-gw-ns7-0:/kube-ovn# uname -a
Linux vpc-nat-gw-ns7-0 5.15.0-161-generic #171-Ubuntu SMP Sat Oct 11 08:17:01 UTC 2025 x86_64 Linux
vpc-nat-gw-ns7-0:/kube-ovn#
vpc-nat-gw-ns7-0:/kube-ovn#
vpc-nat-gw-ns7-0:/kube-ovn# iptables_cmd=$(which iptables)
iptables_save_cmd=$(which iptables-save)
if iptables-legacy -t nat -S INPUT 1 2>/dev/null; then
# use iptables-legacy for centos 7
iptables_cmd=$(which iptables-legacy)
iptables_save_cmd=$(which iptables-legacy-save)
fi
vpc-nat-gw-ns7-0:/kube-ovn#
vpc-nat-gw-ns7-0:/kube-ovn# ls -lv $iptables_cmd
lrwxrwxrwx 1 root root 20 Jan 29 10:07 /usr/sbin/iptables-legacy -> xtables-legacy-multi
vpc-nat-gw-ns7-0:/kube-ovn# ls -lv $iptables_save_cmd
lrwxrwxrwx 1 root root 20 Jan 29 10:07 /usr/sbin/iptables-legacy-save -> xtables-legacy-multi
vpc-nat-gw-ns7-0:/kube-ovn# iptables-legacy -t nat -S INPUT 1
vpc-nat-gw-ns7-0:/kube-ovn# echo $?
0
vpc-nat-gw-ns7-0:/kube-ovn#
vpc-nat-gw-ns7-0:/kube-ovn# iptables-legacy-save
# Generated by iptables-save v1.8.11 on Thu Feb 12 02:58:02 2026
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:DNAT_FILTER - [0:0]
:EXCLUSIVE_DNAT - [0:0]
:EXCLUSIVE_SNAT - [0:0]
:SHARED_DNAT - [0:0]
:SHARED_SNAT - [0:0]
:SNAT_FILTER - [0:0]
-A PREROUTING -j DNAT_FILTER
-A POSTROUTING -j SNAT_FILTER
-A DNAT_FILTER -j EXCLUSIVE_DNAT
-A DNAT_FILTER -j SHARED_DNAT
-A EXCLUSIVE_DNAT -d 10.34.251.131/32 -j DNAT --to-destination 10.177.0.3
-A EXCLUSIVE_SNAT -s 10.177.0.3/32 -j SNAT --to-source 10.34.251.131
-A SNAT_FILTER -j EXCLUSIVE_SNAT
-A SNAT_FILTER -j SHARED_SNAT
COMMIT
# Completed on Thu Feb 12 02:58:02 2026
vpc-nat-gw-ns7-0:/kube-ovn#
是的,你的观察非常准确。在 Kube-OVN 的设计逻辑中,vpc-nat-gw 的 Alpine 镜像确实是设计为始终使用 iptables-legacy 的。
这背后的原因可以从镜像定义和脚本逻辑两个层面来验证:
1. 镜像层面的强制安装
看你提供的 Dockerfile (Alpine 版本):
Dockerfile
RUN set -ex \
&& apk add --no-cache \
iptables iptables-legacy \
...
Alpine 的 iptables 软件包在较新版本中默认也开始转向 NFT,但 Kube-OVN 特意显式安装了 iptables-legacy。这意味着镜像内明确准备好了老版本的二进制文件,为脚本切换路径做好了铺垫。
2. 脚本层面的自动收口
再看 nat-gateway.sh 里的那段关键代码:
Bash
iptables_cmd=$(which iptables)
if iptables-legacy -t nat -S INPUT 1 2>/dev/null; then
# use iptables-legacy for centos 7
iptables_cmd=$(which iptables-legacy)
iptables_save_cmd=$(which iptables-legacy-save)
fi
在 Alpine 镜像中,由于安装了 iptables-legacy 包,if 条件几乎总是为真。脚本会毫不犹豫地将所有的防火墙操作重定向到 xtables-legacy-multi 工具链上。