docker的iptables策略详解和用户自定义策略的添加方式
GPT4.0+Midjourney绘画+国内大模型 会员永久免费使用!
【 如果你想靠AI翻身,你先需要一个靠谱的工具! 】
1. 需求
需求:
iptables增加策略,允许指定主机访问本机的指定端口,但是该端口是docker容器提供的服务。
2. 分析
不想了解原理,直接操作的可以跳过本节
2.1 缘起
- 如果不是docker,我们可以这样写:
1 2 | iptables -I INPUT -p tcp --dport 80 -j DROP iptables -I INPUT -s 10.10.181.198 -p tcp --dport 80 -j ACCEPT |
- 但是docker建立了自己的iptables规则,将绕过filter表的INPUT链,接下来我们分析docker的iptables规则:
2.2 docker的iptables规则
- 但是对于docker,访问则绕过了filter表的INPUT链
- 而是通
注意:但是本机访问docker服务或容器间互访,依然通过的是filter表的INPUT链
1)nat表
查看iptables的nat表,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | [root@liubei- test nginx01] # iptables -t nat -L Chain PREROUTING (policy ACCEPT) target prot opt source destination DOCKER all -- anywhere anywhere ADDRTYPE match dst- type LOCAL Chain INPUT (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination DOCKER all -- anywhere !loopback /8 ADDRTYPE match dst- type LOCAL Chain POSTROUTING (policy ACCEPT) target prot opt source destination MASQUERADE all -- 172.17.0.0 /16 anywhere MASQUERADE all -- 172.20.0.0 /16 anywhere MASQUERADE all -- 172.19.0.0 /16 anywhere MASQUERADE all -- 172.29.0.0 /16 anywhere MASQUERADE all -- 192.168.176.0 /20 anywhere MASQUERADE tcp -- 192.168.176.2 192.168.176.2 tcp dpt:netopia-vo2 MASQUERADE tcp -- 172.29.0.2 172.29.0.2 tcp dpt:20090 MASQUERADE tcp -- 172.29.0.2 172.29.0.2 tcp dpt:10090 MASQUERADE tcp -- 172.29.0.2 172.29.0.2 tcp dpt:lrp MASQUERADE tcp -- 172.20.0.2 172.20.0.2 tcp dpt:http MASQUERADE tcp -- 172.19.0.2 172.19.0.2 tcp dpt:http Chain DOCKER (2 references) target prot opt source destination RETURN all -- anywhere anywhere RETURN all -- anywhere anywhere RETURN all -- anywhere anywhere DNAT tcp -- anywhere anywhere tcp dpt:http to:172.20.0.2:80 |
1.Chain PREROUTING 将请求转发到DOCKER链处理:
ADDRTYPE
:iptables的一个扩展模块,用于根据地址类型进行匹配。dst-type LOCAL
:表示目标地址必须是本地地址
2.Chain DOCKER 修改了目标地址:
2)filter表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | [root@liubei- test src] # iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- 10.10.87.18 anywhere tcp dpt:2375 DROP tcp -- anywhere anywhere tcp dpt:2375 Chain FORWARD (policy DROP) target prot opt source destination DOCKER-USER all -- anywhere anywhere DOCKER-ISOLATION-STAGE-1 all -- anywhere anywhere ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED DOCKER all -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED DOCKER all -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED DOCKER all -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED DOCKER all -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain DOCKER (4 references) target prot opt source destination ACCEPT tcp -- anywhere 172.18.0.2 tcp dpt:http Chain DOCKER-ISOLATION-STAGE-1 (1 references) target prot opt source destination DOCKER-ISOLATION-STAGE-2 all -- anywhere anywhere DOCKER-ISOLATION-STAGE-2 all -- anywhere anywhere RETURN all -- anywhere anywhere Chain DOCKER-ISOLATION-STAGE-2 (2 references) target prot opt source destination DROP all -- anywhere anywhere DROP all -- anywhere anywhere RETURN all -- anywhere anywhere Chain DOCKER-USER (1 references) target prot opt source destination RETURN all -- anywhere anywhere |
1.因为nat表修改了访问的目标地址,因此不再由filter表的INPUT链处理,而是交给了filter表的FORWARD链处理
2.FORWARD链会将请求依次交给如下链处理
注意的是,iptables的规则是匹配到即跳出。
DOCKER-USER
- 作用:允许用户在此自定义规则
Chain DOCKER-ISOLATION-STAGE-1
- 选择交给Chain DOCKER-ISOLATION-STAGE-2 处理
- 作用:主要用于实现Docker容器之间的网络隔离
DOCKER
- docker自动创建的iptables规则
3. 操作
如上文,我们只需修改预留给我们的filter表的DOCKER-USER链即可
1 2 | iptables -I DOCKER-USER -p tcp --dport 80 -j DROP iptables -I DOCKER-USER -s 10.10.181.201 -p tcp --dport 80 -j ACCEPT |
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
- Docker iptables的错误解决
- docker的WARNING:bridge-nf-call-iptables is disabled的解决方案
- iptables使用及docker的iptables规则
- 解决docker安装完成报:bridge-nf-call-iptables is disabled问题
- Docker与iptables及实现bridge方式网络隔离与通信操作
- Docker中iptables规则在iptables重启后丢失的完整过程
- 详解Docker使用Linux iptables 和 Interfaces管理容器网络
- 在Docker容器中使用iptables时的最小权限的开启方法
- iptables如何限制宿主机跟Docker IP和端口访问(安全整改)
![](http://files.jb51.net/skin/2018/images/jb51ewm.png)
微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
本地Docker部署Navidrome音乐服务器与远程访问听歌详细教程(图文详解)
本文和大家分享一款目前在G站有11K+Star的开源跨平台音乐服务器 Navidrome,如何在 Linux 环境本地使用 Docker 部署,并结合cpolar 内网穿透工具配置公网地址,实现随时随地远程访问本地存储音乐的详细流程,感兴趣的朋友跟随小编一起看看吧2024-08-08
最新评论