我们有一个系统,它已经从传统的服务转移到了码头。我们只希望在生产中有一个防火墙,因此我们希望iptables配置只在服务器中,而不是在docker的配置中。
我们有下列要求:
在阅读了几个小时/几天之后,我来到了以下配置:
# Delete old entries if any
iptables -F INPUT
iptables -F DOCKER-USER
iptables -F OUTPUT
# Set firewall
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT # Allow established connections
iptables -A INPUT -i lo -j ACCEPT # Allow localhost communication
iptables -A OUTPUT -o lo -j ACCEPT # Allow output to the internet from localhost
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT # Allow ICMP
iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow SSH
iptables -A INPUT -j DROP # Drop eveything else
# Docker specific, allow connections to the ports of the web services:
iptables -A DOCKER-USER -i ens192 -s 192.168.69.0/24 -p tcp -m conntrack --ctorigdstport 8080 --ctdir ORIGINAL -j ACCEPT
iptables -A DOCKER-USER -i ens192 -s 192.168.69.0/24 -p tcp -m conntrack --ctorigdstport 9000 --ctdir ORIGINAL -j ACCEPT
iptables -A DOCKER-USER -i ens192 -s 192.168.69.0/24 -p tcp -m conntrack --ctorigdstport 19900 --ctdir ORIGINAL -j ACCEPT
iptables -A DOCKER-USER -i docker0 -j ACCEPT # Allow input from other containers
iptables -A DOCKER-USER -i ens192 -j DROP # Drop all access to the containers through default interface在这一点上:
但现在,当我们试图从一个容器访问互联网时,它是无法访问的。(例如,ping到google,或本地网络)。但从本地人的角度来看,这是可行的。
我尝试过多种选择,如以下多种组合:
iptables -A DOCKER-USER -o docker0 -j ACCEPT
iptables -A OUTPUT -o docker0 -j ACCEPT
iptables -A DOCKER-USER -o ens192 -j ACCEPT但他们似乎都不允许我从码头的集装箱上跳下去。
发布于 2020-07-17 14:00:45
我想我找到了一个可行的解决方案,但我不确定是否有更好的方法。
在我尝试过的事情中,我还必须声明DOCKER用户集的已建立连接,并与我的问题相比较,添加这两行:
iptables -A DOCKER-USER -i docker0 -j ACCEPT
> iptables -A DOCKER-USER -o docker0 -j ACCEPT
> iptables -A DOCKER-USER -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A DOCKER-USER -i ens192 -j DROP发布于 2020-07-17 14:20:58
从您的问题中,我假设这些iptables规则应该安装在端口主机上。
您在这里遗漏了一个要点:防火墙规则只对主机本身有效,而不是对容器有效!
假设您的docker0接口的ip范围为172.16.0.0/16,主机接口地址为172.16.0.1,您的第一个容器的地址可能为172.16.0.2。
但是,从容器到internet的任何传出流量都需要从主机的主网络接口(即ens192 )发送出去。因此,任何用于容器的防火墙规则都必须驻留在前向链中!(别忘了让主机启用ip_forwarding )
https://serverfault.com/questions/1025628
复制相似问题