网站不能风格,高新企业建设网站价格,百度关键词流量查询,台州关键词排名优化背景#xff1a;
在Linux上docker映射了端口#xff0c;想着对服务端口进行限制指定IP访问#xff0c;发现在filter表的INPUT链限制无效
环境#xff1a;
主机192.168.56.132上的docker容器部署了nginx并将容器80端口映射到主机8000端口
[rootlocalhost ~]# docker ps …背景
在Linux上docker映射了端口想着对服务端口进行限制指定IP访问发现在filter表的INPUT链限制无效
环境
主机192.168.56.132上的docker容器部署了nginx并将容器80端口映射到主机8000端口
[rootlocalhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
79e2d2824b97 dockerproxy.cn/library/nginx /docker-entrypoint.… 4 hours ago Up 2 hours 0.0.0.0:8000-80/tcp, :::8000-80/tcp nginx-test
[rootlocalhost ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 83216/docker-proxy
tcp6 0 0 :::8000 :::* LISTEN 83220/docker-proxy需求
仅允许192.168.56.128主机访问此容器其他主机全部禁止访问。 此时默认iptables策略为:
[rootlocalhost ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination Chain FORWARD (policy DROP)
target prot opt source destination
DOCKER-USER all -- 0.0.0.0/0 0.0.0.0/0
DOCKER-ISOLATION-STAGE-1 all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
DOCKER all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 Chain OUTPUT (policy ACCEPT)
target prot opt source destination Chain DOCKER (1 references)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 172.17.0.2 tcp dpt:80Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target prot opt source destination
DOCKER-ISOLATION-STAGE-2 all -- 0.0.0.0/0 0.0.0.0/0
RETURN all -- 0.0.0.0/0 0.0.0.0/0 Chain DOCKER-ISOLATION-STAGE-2 (1 references)
target prot opt source destination
DROP all -- 0.0.0.0/0 0.0.0.0/0
RETURN all -- 0.0.0.0/0 0.0.0.0/0 Chain DOCKER-USER (1 references)
target prot opt source destination
RETURN all -- 0.0.0.0/0 0.0.0.0/0 操作
按照常规操作添加策略
在INPUT表中直接添加drop所有地址访问8000端口
# 添加策略
[rootserver ~]# iptables -I INPUT -p tcp --dport 8000 -j DROP
# 查看
[rootserver ~]# iptables -nL INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8000
使用其他机器测试发现所添加防火墙策略无效
[rootclient ~]# curl 192.168.56.132:8000
!DOCTYPE html
html
head
titleWelcome to nginx!/title
style那尝试禁用80端口看看是否有效
[rootserver ~]# iptables -I INPUT -p tcp --dport 80 -j DROP
[rootserver ~]# iptables -nL INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8000测试连接,发现策略依旧无效。
[rootclient ~]# curl 192.168.56.132:8000
!DOCTYPE html
html
head
titleWelcome to nginx!/title
style查找资料发现 Docker安装了两个定制的iptables链名为DOCKER-USER和DOCKER它确保传入的数据包总是首先由这两个链检查。 Docker的所有iptables规则都被添加到Docker链中。不要手动操作该链条。如果您需要添加在Docker规则之前加载的规则请将它们添加到DOCKER-USER链中。这些规则是在Docker自动创建任何规则之前应用的。 添加到转发链中的规则——无论是手动添加的还是由另一个基于iptables的防火墙添加的——都会在这些链之后进行评估。这意味着如果您通过Docker暴露一个端口无论您的防火墙配置了什么规则该端口都会被暴露。如果您希望这些规则即使在端口通过Docker暴露时也适用那么您必须将这些规则添加到DOCKER-USER链中。
在DOCKER-USER链中配置策略
Docker容器创建时会自动创建iptables策略Docker使用的规则链是DOCKER-USER所以需使用iptables对DOCKER-USER链做限制才可以完成需求。 加上试试
# 先删掉return的策略不然会被直接返回进行下一步操作
[rootserver ~]# iptables -D DOCKER-USER -j RETURN
[rootserver ~]# iptables -I DOCKER-USER -p tcp --dport 8000 -j DROP
[rootserver ~]# iptables -nL DOCKER-USER
Chain DOCKER-USER (1 references)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8000
访问测试还是不行
[rootclient ~]# curl 192.168.56.132:8000
!DOCTYPE html
html
head
titleWelcome to nginx!/title
style
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
/style
加上日志追踪看一下到底什么原因
[rootserver ~]# echo kern.debug /var/log/kern.debug.log /etc/rsyslog.conf
[rootserver ~]# touch /var/log/kern.debug.log
[rootserver ~]# systemctl restart rsyslog[rootserver ~]# iptables -I DOCKER-USER 1 -p tcp -m limit --limit 4/min -j LOG --log-prefix Iptables-DROP: --log-level 7
[rootserver ~]# iptables -nL DOCKER-USER
Chain DOCKER-USER (1 references)
target prot opt source destination
LOG tcp -- 0.0.0.0/0 0.0.0.0/0 limit: avg 4/min burst 5 LOG flags 0 level 7 prefix Iptables-DROP:
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8000 # 其他机器测试连接一下肯定是可以连通的。
[rootclient ~]# curl 192.168.56.132:8000
!DOCTYPE html
html
head
titleWelcome to nginx!/title
style# 查看日志
[rootserver ~]# tailf /var/log/kern.debug.log
Oct 28 14:55:45 server kernel: Iptables-DROP: INens33 OUTdocker0 MAC00:0c:29:87:29:40:00:50:56:c0:00:08:08:00 SRC192.168.56.1 DST172.17.0.2 LEN40 TOS0x00 PREC0x00 TTL127 ID35633 DF PROTOTCP SPT43556 DPT80 WINDOW0 RES0x00 ACK FIN URGP0 # 重点部分
SRC192.168.56.1 # 源地址
DST172.17.0.2 # 目标地址
DPT80 # 目标端口在日志中发现原地址目标地址和目标端口都有现在就发现问题了目标端口不是我们所映射出来的8000端口而是容器内部的80端口那尝试禁止访问80端口试试
[rootserver ~]# iptables -I DOCKER-USER -p tcp --dport 80 -j DROP
[rootserver ~]# iptables -nL DOCKER-USER
Chain DOCKER-USER (1 references)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
LOG tcp -- 0.0.0.0/0 0.0.0.0/0 limit: avg 4/min burst 5 LOG flags 0 level 7 prefix Iptables-DROP:
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8000测试访问发现被拦截了,那我们的需求就达到了只要放开想要放行的IP地址就可以了
[rootclient ~]# curl 192.168.56.132:8000
curl: (7) Failed to connect to 192.168.56.132 port 8000: Connection timed out#server端放行192.168.56.128
[rootserver ~]# iptables -I DOCKER-USER -s 192.168.56.128 -p tcp --dport 80 -j ACCEPT
[rootserver ~]# iptables -nL DOCKER-USER
Chain DOCKER-USER (1 references)
target prot opt source destination
ACCEPT tcp -- 192.168.56.128 0.0.0.0/0 tcp dpt:80
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
LOG tcp -- 0.0.0.0/0 0.0.0.0/0 limit: avg 4/min burst 5 LOG flags 0 level 7 prefix Iptables-DROP:
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8000#再次测试发现可行了找个其他机器测试一下发现除了放行的地址其他地址无法访问。
[rootclient ~]# curl 192.168.56.132:8000
!DOCTYPE html
html
head
titleWelcome to nginx!/title
style会出现的问题
问题复现
上面操作就完成了我们的需求但是这样做会有一个问题如果我机器上容器比较多多个容器内部端口都是80端口那是不是也就不通了答案是是的因为他是根据内部端口禁用了。 再拉一个nginx测试一下,映射了一个9000端口出来 [rootserver ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a7040a0040c dockerproxy.cn/library/nginx /docker-entrypoint.… 6 minutes ago Up 6 minutes 0.0.0.0:9000-80/tcp, :::9000-80/tcp nginx-test-2
79e2d2824b97 dockerproxy.cn/library/nginx /docker-entrypoint.… 5 hours ago Up About an hour 0.0.0.0:8000-80/tcp, :::8000-80/tcp nginx-test
# 他会自动在DOCKER链中生成一条新的不用管它。
[rootserver ~]# iptables -nL DOCKER
Chain DOCKER (1 references)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 172.17.0.2 tcp dpt:80
ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:80
# 我们将放行的机器剔除
rootserver ~]# iptables -D DOCKER-USER -s 192.168.56.128 -p tcp --dport 80 -j ACCEPT
[rootserver ~]# iptables -nL DOCKER-USER
Chain DOCKER-USER (1 references)
target prot opt source destination
LOG tcp -- 0.0.0.0/0 0.0.0.0/0 limit: avg 4/min burst 5 LOG flags 0 level 7 prefix Iptables-DROP:
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80# 测试访问两个端口发现都无法访问这就很可能会影响我们其他容器的业务[rootclient ~]# curl 192.168.56.132:9000
curl: (7) Failed to connect to 192.168.56.132 port 9000: Connection timed out[rootclient ~]# curl 192.168.56.132:8000
curl: (7) Failed to connect to 192.168.56.132 port 8000: Connection timed out解决方法
解决方法一
策略指定目的地址我们每创建一个容器他都会生成一个他自己的地址在加防火墙的时候增加上目的地址可以解决此问题。 例如我现在9000端口的容器就想要被所有人访问8000端口只让192.168.56.128一台可以访问可以这样做就是比较麻烦首先得获取每个容器的ip地址
# 先清理掉之前的规则
[rootserver ~]# iptables -D DOCKER-USER -p tcp --dport 80 -j DROP
# 增加一条带有目的地址的策略
[rootserver ~]# iptables -A DOCKER-USER -d 172.17.0.2 -p tcp --dport 80 -j DROP
[rootserver ~]# iptables -nL DOCKER-USER
Chain DOCKER-USER (1 references)
target prot opt source destination
LOG tcp -- 0.0.0.0/0 0.0.0.0/0 limit: avg 4/min burst 5 LOG flags 0 level 7 prefix Iptables-DROP:
DROP tcp -- 0.0.0.0/0 172.17.0.2 tcp dpt:80# 测试达成了我们的目的
[rootclient ~]# curl 192.168.56.132:9000
!DOCTYPE html
html
head
titleWelcome to nginx!/title
style[rootclient ~]# curl 192.168.56.132:8000
curl: (7) Failed to connect to 192.168.56.132 port 8000: Connection timed out#然后放行我们想要放行的地址即可
[rootserver ~]# iptables -I DOCKER-USER -s 192.168.56.128 -d 172.17.0.2 -p tcp --dport 80 -j ACCEPT
[rootserver ~]# iptables -nL DOCKER-USER
Chain DOCKER-USER (1 references)
target prot opt source destination
ACCEPT tcp -- 192.168.56.128 172.17.0.2 tcp dpt:80
LOG tcp -- 0.0.0.0/0 0.0.0.0/0 limit: avg 4/min burst 5 LOG flags 0 level 7 prefix Iptables-DROP:
DROP tcp -- 0.0.0.0/0 172.17.0.2 tcp dpt:80#测试访问
[rootclient ~]# curl 192.168.56.132:9000
!DOCTYPE html
html
head
titleWelcome to nginx!/title
style
[rootclient ~]# curl 192.168.56.132:8000
!DOCTYPE html
html
head
titleWelcome to nginx!/title
style解决方法二
在raw表中增加策略 解决方法二 在raw表中增加策略。 了解一下什么是raw表做什么用的 iptables有5个链PREROUTINGINPUTFORWARDOUTPUTPOSTROUTING 4个表filternatmangleraw 4个表的优先级由高到低的顺序为:raw–mangle–nat–filter 举例来说:如果PRROUTING链上,即有mangle表,也有nat表,那么先由mangle处理,然后由nat表处理 RAW表只使用在PREROUTING链和OUTPUT链上,因为优先级最高从而可以对收到的数据包在连接跟踪前进行处理。一但用户使用了RAW表,在某个链上,RAW表处理完后,将跳过NAT表和 ip_conntrack处理,即不再做地址转换和数据包的链接跟踪处理了. RAW表可以应用在那些不需要做nat的情况下以提高性能。如大量访问的web服务器可以让80端口不再让iptables做数据包的链接跟踪处理以提高用户的访问速度。
Docker端口映射工作流程 当你使用 docker run -p host_port:container_port 启动一个容器时Docker 会通过 iptables 将宿主机的 host_port 映射到容器的 container_port。 PREROUTING 链中的规则会捕获外部发向 host_port 的流量重定向到容器的 IP 地址和 container_port。 总的来说Docker 的端口映射主要通过 NAT 表 中的 PREROUTING 链 和 DOCKER 链 来实现的。这些链协作确保外部请求可以被正确地转发到容器中对应的端口那么根据下面的流程图可以看出我们可以在NAT表转换端口之前去拦截过滤原始端口外部主机访问的宿主机端口那只能在RAW表的PREROUTING和mangle表的PREROUTING链上面去加策略。 但是mangle表是用来修改数据包的不做过滤操作那么只能在raw表上去做。
理论结束实战开始 先把策略恢复到一开始的没操作的状态
[rootserver ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination Chain FORWARD (policy DROP)
target prot opt source destination
DOCKER-USER all -- 0.0.0.0/0 0.0.0.0/0
DOCKER-ISOLATION-STAGE-1 all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
DOCKER all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 Chain OUTPUT (policy ACCEPT)
target prot opt source destination Chain DOCKER (1 references)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 172.17.0.2 tcp dpt:80
ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:80Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target prot opt source destination
DOCKER-ISOLATION-STAGE-2 all -- 0.0.0.0/0 0.0.0.0/0
RETURN all -- 0.0.0.0/0 0.0.0.0/0 Chain DOCKER-ISOLATION-STAGE-2 (1 references)
target prot opt source destination
DROP all -- 0.0.0.0/0 0.0.0.0/0
RETURN all -- 0.0.0.0/0 0.0.0.0/0 Chain DOCKER-USER (1 references)
target prot opt source destination
RETURN all -- 0.0.0.0/0 0.0.0.0/0 直接在raw 表的PREROUTING链上做8000端口的拦截
[rootserver ~]# iptables -t raw -I PREROUTING -p tcp -m tcp --dport 8000 -j DROP测试连接情况
[rootclient ~]# curl 192.168.56.132:8000
curl: (7) Failed to connect to 192.168.56.132 port 8000: Connection timed out# 不影响9000端口的容器访问
[rootclient ~]# curl 192.168.56.132:9000
!DOCTYPE html
html
head
titleWelcome to nginx!/title
style放开指定ip地址
[rootserver ~]# iptables -t raw -I PREROUTING -s 192.168.56.128 -p tcp -m tcp --dport 8000 -j ACCEPT[rootserver ~]# iptables -nL -t raw
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 192.168.56.128 0.0.0.0/0 tcp dpt:8000
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8000Chain OUTPUT (policy ACCEPT)
target prot opt source destination 再次测试被放开的主机已经可以通了测试一下其他主机发现无法访问仅放开的主机可以访问且不干扰其他端口
[rootclient ~]# curl 192.168.56.132:9000
!DOCTYPE html
html
head
titleWelcome to nginx!/title
style[rootclient ~]# curl 192.168.56.132:8000
!DOCTYPE html
html
head
titleWelcome to nginx!/title
stylePS在raw表上加策略用的人少不知道会有什么影响希望有大佬可以解答。