docker的iptables策略詳解和用戶自定義策略的添加方式
1. 需求
需求:
iptables增加策略,允許指定主機訪問本機的指定端口,但是該端口是docker容器提供的服務。
2. 分析
不想了解原理,直接操作的可以跳過本節(jié)
2.1 緣起
- 如果不是docker,我們可以這樣寫:
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規(guī)則,將繞過filter表的INPUT鏈,接下來我們分析docker的iptables規(guī)則:
2.2 docker的iptables規(guī)則
- 但是對于docker,訪問則繞過了filter表的INPUT鏈
- 而是通
注意:但是本機訪問docker服務或容器間互訪,依然通過的是filter表的INPUT鏈
1)nat表
查看iptables的nat表,內(nèi)容如下:
[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 將請求轉(zhuǎn)發(fā)到DOCKER鏈處理:
DOCKER all -- anywhere anywhere ADDRTYPE match dst-type LOCAL
ADDRTYPE
:iptables的一個擴展模塊,用于根據(jù)地址類型進行匹配。dst-type LOCAL
:表示目標地址必須是本地地址
2.Chain DOCKER 修改了目標地址:
DNAT tcp -- anywhere anywhere tcp dpt:http to:172.20.0.2:80
2)filter表
[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的規(guī)則是匹配到即跳出。
DOCKER-USER
- 作用:允許用戶在此自定義規(guī)則
Chain DOCKER-ISOLATION-STAGE-1
- 選擇交給Chain DOCKER-ISOLATION-STAGE-2 處理
- 作用:主要用于實現(xiàn)Docker容器之間的網(wǎng)絡隔離
DOCKER
- docker自動創(chuàng)建的iptables規(guī)則
3. 操作
如上文,我們只需修改預留給我們的filter表的DOCKER-USER鏈即可
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
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 解決docker安裝完成報:bridge-nf-call-iptables is disabled問題
- 在Docker容器中使用iptables時的最小權限的開啟方法
- Docker中iptables規(guī)則在iptables重啟后丟失的完整過程
- 詳解Docker使用Linux iptables 和 Interfaces管理容器網(wǎng)絡
- Docker與iptables及實現(xiàn)bridge方式網(wǎng)絡隔離與通信操作
- iptables使用及docker的iptables規(guī)則
- iptables如何限制宿主機跟Docker IP和端口訪問(安全整改)
- Docker iptables的錯誤解決
- docker的WARNING:bridge-nf-call-iptables is disabled的解決方案
- 基于iptables的Docker端口白名單控制實現(xiàn)
相關文章
Docker基于macvlan實現(xiàn)跨主機容器通信
這篇文章主要介紹了Docker基于macvlan實現(xiàn)跨主機容器通信,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05

jenkins+gitlab+nginx部署前端應用實現(xiàn)

使用Docker快速搭建Oracle開發(fā)環(huán)境的方法教程