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规则是否存在
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