centos防火墙状态更改引起的docker网络问题的半解决方案

本文探讨了在CentOS系统中运行Docker时遇到的防火墙规则与Docker网络通信冲突的问题,提供了一种通过手动配置防火墙NAT规则或将iptables规则持久化的解决方案,确保在防火墙常开的生产环境下,Docker容器间网络通信不受影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

one

在centos系统运行docker时发现,防火墙状态发生变化的时候,docker之间的网络通信会收到影响,上网查找资料原因是:在宿主机上配置到容器的NAT(docker run -p)时,Docker会自动配置宿主机的防火墙NAT规则,但是这些规则并不会持久化到防火墙配置文件,重启防火墙后这些规则都将消失,直接导致运行的容器访问失效。

最简单的解决办法

是重启docker,让docker的规则再加载一次。

但是在生成环境下很经常性的重启docker明显是不可取的,那么怎样解决这个问题呢?在网上查找资料,找到部分解决方案。

一种思路

1)不使用docker run -p参数配置NAT,而是通过手动配置防火墙NAT规则来实现。

2)使用docker run -p参数配置NAT,容器启动之后使用iptables-save(centos)将防火墙规则同步到防火墙配置文件

这里介绍的是采用2方案:

1.iptables -nL -t nat —查看docker NAT规则是否存在

img

2.iptables-save 检查当前 IPTable 表是否空

3.如果IPtable 表不为空。 先备份IPtable表

cp /etc/sysconfig/iptables /etc/sysconfig/iptables_bak20180705

4.iptables-save > /etc/sysconfig/iptables 将所有IPtable 规则重新保存

这时候,重启防火墙也不会影响 容器使用了。

如果需要复原防火配置即可以使用命令:cp iptables_bak20180705 iptables

二种想法

文件/etc/sysconfig/iptables-config 里设定了iptables的种种配置,具体如下

# Load additional iptables modules (nat helpers)
#   Default: -none-
# Space separated list of nat helpers (e.g. 'ip_nat_ftp ip_nat_irc'), which
# are loaded after the firewall rules are applied. Options for the helpers are
# stored in /etc/modprobe.conf.
IPTABLES_MODULES=""

# Unload modules on restart and stop
#   Value: yes|no,  default: yes
# This option has to be 'yes' to get to a sane state for a firewall
# restart or stop. Only set to 'no' if there are problems unloading netfilter
# modules.
IPTABLES_MODULES_UNLOAD="yes"

# Load additional iptables modules (nat helpers)
#   Default: -none-
# Space separated list of nat helpers (e.g. 'ip_nat_ftp ip_nat_irc'), which
# are loaded after the firewall rules are applied. Options for the helpers are
# stored in /etc/modprobe.conf.
IPTABLES_MODULES=""

# Unload modules on restart and stop
#   Value: yes|no,  default: yes
# This option has to be 'yes' to get to a sane state for a firewall
# restart or stop. Only set to 'no' if there are problems unloading netfilter
# modules.
IPTABLES_MODULES_UNLOAD="yes"

# Save current firewall rules on stop.
#   Value: yes|no,  default: yes
# Saves all firewall rules to /etc/sysconfig/iptables if firewall gets stopped
# (e.g. on system shutdown).
IPTABLES_SAVE_ON_STOP="no"

# Save current firewall rules on restart.
#   Value: yes|no,  default: yes
# Saves all firewall rules to /etc/sysconfig/iptables if firewall gets
# restarted.
IPTABLES_SAVE_ON_RESTART="no"

# Save (and restore) rule and chain counter.
#   Value: yes|no,  default: no
# Save counters for rules and chains to /etc/sysconfig/iptables if
# 'service iptables save' is called or on stop or restart if SAVE_ON_STOP or
# SAVE_ON_RESTART is enabled.
IPTABLES_SAVE_COUNTER="no"

# Numeric status output
#   Value: yes|no,  default: yes
# Print IP addresses and port numbers in numeric format in the status output.
IPTABLES_STATUS_NUMERIC="yes"

# Verbose status output
#   Value: yes|no,  default: yes
# Print info about the number of packets and bytes plus the "input-" and
# "outputdevice" in the status output.
IPTABLES_STATUS_VERBOSE="no"

# Status output with numbered lines
#   Value: yes|no,  default: yes
# Print a counter/number for every rule in the status output.
IPTABLES_STATUS_LINENUMBERS="yes"

# Reload sysctl settings on start and restart
#   Default: -none-
# Space separated list of sysctl items which are to be reloaded on start.

IPTABLES_MODULES="ip_conntrack_netbios_ns"

在防火墙被激活时,指定一组独立的空间额外加载iptables模块,系统启动,加载防火墙模块,会打印:Loading additional iptables modules: ip_conntrack_netbios_ns[ OK ]

IPTABLES_MODULES_UNLOAD="yes"

在重新启动和停止iptables模块时,是否卸载此模块。

IPTABLES_SAVE_ON_STOP="no"

当防火墙停止时,保存当前防火墙规则到iptables文件,no :(默认值)不保存当前的规则到iptables文件。

IPTABLES_SAVE_ON_RESTART="no"

当防火墙重启时:service iptables restart,保存当前防火墙规则到iptables文件,no :(默认值)不保存当前的规则到iptables文件。

IPTABLES_SAVE_COUNTER="no"

保存并恢复所有chain和规则中的数据包和字节计数器,yes:保存计数器的值,no:(默认值)不保存计数器值。

IPTABLES_STATUS_NUMERIC="yes"

输出的IP地址是数字的格式,而不是域名和主机名的形式,yes:(默认值)在状态输出中只包括IP地址,no:在状态输出中返回域名或主机名。

IPTABLES_STATUS_VERBOSE="no"

输出iptables状态时,是否包含输入输出设备,yes:包含,no:(默认值)不包含。

IPTABLES_STATUS_LINENUMBERS="yes"

输出iptables状态时,是否同时输出每条规则的匹配数,yes:(默认值)输出,no:不输出。

根据描述,将IPTABLES_SAVE_ON_STOP="no"和IPTABLES_SAVE_ON_RESTART="no"修改为yes

修改完毕后,重新启动防火墙,即可生效

实际操作

按照方案一:cp /etc/sysconfig/iptables /etc/sysconfig/iptables_bak20180705时。系统提示/etc/sysconfig/iptables不存在。

解决方案。先执行下一步.iptables-save > /etc/sysconfig/iptables 创建出对应文件即可。

为了防止意外,在实际操作中,两个方案我都进行了使用。得到的结果是防火墙开启的状态下,(

既systemctl restart firewalld 或者systemctl stop firewalld+systemctl start firewalld操作)docker间的网络通信都不在受影响。

这样的结果确保了在防火墙开启的状态下,防火墙规则里不会丢失docker相关配置。

而防火墙关闭的时候,docker配置丢失,不可通信。

故这种方法适用防火墙常开的生产环境。

一点遗留

可以看出虽然centos使用的是firewalld命令。我们还是通过修改底层的iptables配置来实现来我们的需求。

但是我们只实现了在开启状态下的通信正常。而当我再同样使用iptables的ubantu上进行测试时,无论是开启还是关闭状态。docker规则都岿然不动的伫立在iptables配置里,这说明这种情况是可以实现的。只是限于对这方面了解太少,只能暂时做到满足生产需求,无法进一步优化。希望以后有机会再深入研究

参考文档:

https://www.cnblogs.com/JesseSong/articles/9269408.html

http://www.what21.com/sys/view/liunx_centos_1476930018956.html

### CentOS 7 上 Docker 运行失败的原因分析与解决方案CentOS 7 平台上运行 Docker 遇到问题可能由多种原因引起,以下是常见的几个方面以及对应的解决方法。 #### 1. **Docker 服务未启动** 如果 `docker` 的服务状态显示为停止或者异常,则需要手动启用并启动该服务。可以通过以下命令确认其状态: ```bash systemctl status docker ``` 若服务处于关闭状态,可以尝试重新加载配置文件并启动服务: ```bash systemctl daemon-reload && systemctl start docker ``` 此外,还可以验证 `docker.service` 和 `docker.socket` 是否已设置为开机自启[^1]: ```bash systemctl is-enabled docker.service systemctl is-enabled docker.socket ``` --- #### 2. **网络配置错误** Docker 默认会在宿主机上创建一个名为 `docker0` 的网桥设备用于容器通信。如果此网桥未能正常初始化或与其他网络发生冲突,可能导致容器无法连接外部网络。 可以通过检查 `/etc/hosts` 文件来排查是否存在不一致的 DNS 映射记录[^2]。例如,在某些情况下,Docker Desktop 会自动向 `/etc/hosts` 添加如下条目: ``` 127.0.0.1 kubernetes.docker.internal ``` 如果这些映射影响到了其他服务的功能,建议将其移除或调整优先级。 --- #### 3. **防火墙阻止了 Docker 流量** 当系统上的防火墙策略较为严格时,可能会拦截掉 Docker 容器之间的流量交换。此时需临时禁用防火墙测试效果: ```bash systemctl stop firewalld ``` 或者允许特定范围内的端口通行(假设目标端口为 80 和 443): ```bash firewall-cmd --permanent --add-port=80/tcp firewall-cmd --permanent --add-port=443/tcp firewall-cmd --reload ``` 同样需要注意的是,当新增加的服务试图绑定某个固定端口却遭到拒绝时,应当先利用工具检测是否有重复占用情况存在[^5]: ```bash netstat -nltp | grep [指定端口号] ``` --- #### 4. **SELinux 导致权限不足** SELinux 是一种强制访问控制机制,默认开启状态下会对许多操作施加额外约束条件。对于部分场景而言,它可能是造成 Docker 功能受限的主要因素之一。为了快速定位问题所在,可暂时切换至宽容模式观察变化: ```bash setenforce 0 ``` 修改完成后记得查看日志文件寻找线索: ```bash journalctl -xeu docker.service ``` 若要永久更改此项设定,则编辑 `/etc/selinux/config` 将其中的一行更改为: ```ini SELINUX=permissive ``` --- #### 5. **Docker Daemon 参数不当** 在实际应用过程中,有时默认参数并不完全满足需求,因此有必要根据实际情况定制化配置项。比如增加 MTU 值以便更好地适配底层物理链路特性[^3]: 编辑位于路径下的 systemd 单元定义文档: ```bash vim /lib/systemd/system/docker.service ``` 找到字段 `ExecStart=` ,在其后面追加上述选项即可生效: ```plaintext ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375 --mtu=1450 ``` 别忘了保存退出后再同步更新缓存数据结构: ```bash systemctl daemon-reload && systemctl restart docker ``` --- #### 6. **硬件资源配置不足** 虚拟环境中部署多个大型应用程序实例很容易耗尽可用 RAM/CPU 等计算资源,进而引发 OOM killer 杀死关键进程的现象。针对这种情况可以从以下几个角度入手优化性能表现: - 提升单台服务器规格; - 减少不必要的后台守护程序数量; - 使用 cgroups 控制组技术精细化管理各子系统的份额比例; --- ### 总结 上述列举了几种常见于 CentOS 7 下 Docker 不工作的情形及其对应处理办法。具体实施哪一步取决于当前遇到的具体症状描述。务必逐一排除干扰源直至恢复正常运作为止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值