解决 Calico 报错 felix failed to apply ipsets iptables-restore
分析 Calico Felix 代理报 failed to apply ipsets iptables-restore 根因,提供 iptables-legacy 模式切换与锁竞争排查步骤。
问题现象
在 Kubernetes 节点上,Calico 节点代理 Pod(calico-node)状态变为 CrashLoopBackOff 或健康检查失败,容器日志中大量输出如下报错:
ERROR felix/dataplane.go: Apply failed
error=iptables-restore failed: exit status 1
output="iptables-restore: unable to initialize table 'filter'
iptables-restore: Memory allocation problem
"
或者提示:felix/ipsets.go: Failed to create ipset: ipset v7.x: Set cannot be created: set type not supported。
触发根因
- iptables 后端模式冲突(nftables vs legacy):操作系统(如 Debian 10+ / Ubuntu 20.04+)默认启用了
iptables-nft,而 Calico Felix 默认配置为legacy模式,导致规则恢复时底层系统调用失败。 - 系统缺少对应的内核模块:宿主机未加载
ip_set、ip_set_hash_net、iptable_filter等内核网络扩展模块。 - 并发锁冲突(xtables.lock 竞争):kube-proxy 与 calico-node 争夺 iptables 全局锁引发超时失败。
诊断排查步骤
- 检查宿主机当前使用的 iptables 版本与模式:
sudo update-alternatives --display iptables
- 检查内核 ipset 模块加载情况:
lsmod | grep -E "ip_set|xt_"
解决步骤与操作命令
步骤一:加载必要内核模块
在所有 Kubernetes 节点上加载模块并持久化:
sudo modprobe ip_set
sudo modprobe ip_set_hash_ip
sudo modprobe ip_set_hash_net
sudo modprobe iptable_filter
sudo bash -c 'cat <<EOF > /etc/modules-load.d/calico.conf
ip_set
ip_set_hash_ip
ip_set_hash_net
iptable_filter
EOF'
步骤二:切换宿主机 iptables 为 legacy 模式(如适用)
对于 Debian/Ubuntu 系统,切换默认实现:
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
步骤三:指定 Calico Felix 自动探测 iptables 模式
修改 Calico 守护进程的环境变量配置:
kubectl set env daemonset/calico-node -n calico-system FELIX_IPTABLESBACKEND=Auto
# 或者若确定为 legacy 模式:
# kubectl set env daemonset/calico-node -n calico-system FELIX_IPTABLESBACKEND=Legacy
步骤四:重启 calico-node 并验证
kubectl rollout restart daemonset calico-node -n calico-system
kubectl rollout status daemonset calico-node -n calico-system
calico-node Pod 恢复为 Running 状态且健康检查通过。


