docker、firewalld和iptables之间的关系

要注意docker命令中使用 -p 暴露端口时,实现需要依赖iptables。CentOS 7默认使用的是firewalld,但是是否需要关闭firewalld并启动iptables呢?

参考多篇博文,答案应该是不不需要的。

Note. You need to distinguish between the iptables service and the iptables command. Although firewalld is a replacement for the firewall management provided by iptables service, it still uses the iptables command for dynamic communication with the kernel packet filter (netfilter). So it is only the iptables service that is replaced, not the iptables command. That can be a confusing distinction at first.

在实际使用过程中,没有使用iptables.service,docker的端口转发也是正常的,因为iptables一直都在。docker会创建自己的iptables链,如果firewalld重启,docker创建的链也需要重新创建。

下面是停用firewalld服务,启用iptables-services
#!/bin/sh

#停止firewalld服务
systemctl stop firewalld    
#禁用firewalld服务
systemctl mask firewalld

yum install -y iptables
yum update iptables
yum install -y iptables-services

systemctl enable iptables.service
systemctl start iptables.service

#暴露docker swarm需要的端口,如果不使用docker swarm不需要打开端口
iptables -A INPUT -p tcp --dport 2377 -j ACCEPT
iptables -A INPUT -p tcp --dport 7946 -j ACCEPT
iptables -A INPUT -p udp --dport 7946 -j ACCEPT
iptables -A INPUT -p tcp --dport 4789 -j ACCEPT
iptables -A INPUT -p udp --dport 4789 -j ACCEPT

service iptables save
systemctl restart iptables.service

#开启转发
echo 'net.ipv4.ip_forward=1'> /usr/lib/sysctl.d/00-system.conf

systemctl restart network

直接使用firewalld

sudo firewall-cmd --permanent --zone=trusted --add-interface=docker0
sudo firewall-cmd --permanent --zone=trusted --add-port=xxxx/tcp#       xxxx改为你希望的端口号
sudo firewall-cmd --reload



 

猜你喜欢

转载自blog.csdn.net/u011537073/article/details/82853465
今日推荐