docker的iptables策略詳解和用戶自定義策略的添加方式
1. 需求
需求:
iptables增加策略,允許指定主機(jī)訪問本機(jī)的指定端口,但是該端口是docker容器提供的服務(wù)。
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ī)則
- 但是對(duì)于docker,訪問則繞過了filter表的INPUT鏈
- 而是通
注意:但是本機(jī)訪問docker服務(wù)或容器間互訪,依然通過的是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 將請(qǐng)求轉(zhuǎn)發(fā)到DOCKER鏈處理:
DOCKER all -- anywhere anywhere ADDRTYPE match dst-type LOCAL
ADDRTYPE
:iptables的一個(gè)擴(kuò)展模塊,用于根據(jù)地址類型進(jìn)行匹配。dst-type LOCAL
:表示目標(biāo)地址必須是本地地址
2.Chain DOCKER 修改了目標(biāo)地址:
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.因?yàn)閚at表修改了訪問的目標(biāo)地址,因此不再由filter表的INPUT鏈處理,而是交給了filter表的FORWARD鏈處理
2.FORWARD鏈會(huì)將請(qǐng)求依次交給如下鏈處理
注意的是,iptables的規(guī)則是匹配到即跳出。
DOCKER-USER
- 作用:允許用戶在此自定義規(guī)則
Chain DOCKER-ISOLATION-STAGE-1
- 選擇交給Chain DOCKER-ISOLATION-STAGE-2 處理
- 作用:主要用于實(shí)現(xiàn)Docker容器之間的網(wǎng)絡(luò)隔離
DOCKER
- docker自動(dòng)創(chuàng)建的iptables規(guī)則
3. 操作
如上文,我們只需修改預(yù)留給我們的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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Docker iptables的錯(cuò)誤解決
- docker的WARNING:bridge-nf-call-iptables is disabled的解決方案
- iptables使用及docker的iptables規(guī)則
- 解決docker安裝完成報(bào):bridge-nf-call-iptables is disabled問題
- Docker與iptables及實(shí)現(xiàn)bridge方式網(wǎng)絡(luò)隔離與通信操作
- Docker中iptables規(guī)則在iptables重啟后丟失的完整過程
- 詳解Docker使用Linux iptables 和 Interfaces管理容器網(wǎng)絡(luò)
- 在Docker容器中使用iptables時(shí)的最小權(quán)限的開啟方法
- iptables如何限制宿主機(jī)跟Docker IP和端口訪問(安全整改)
相關(guān)文章
docker安裝并運(yùn)行rabbitmq的實(shí)例代碼
在本篇文章里小編給大家整理了關(guān)于docker安裝并運(yùn)行rabbitmq的實(shí)例代碼以及相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。2019-10-10Docker安裝ClickHouse并初始化數(shù)據(jù)測(cè)試
clickhouse作為現(xiàn)在流行的數(shù)據(jù)分析數(shù)據(jù)庫(kù),非常熱門,docker如何安裝ClickHouse,很多朋友并不是很明白,今天小編抽空給大家分享一篇教程關(guān)于Docker安裝ClickHouse并初始化數(shù)據(jù)測(cè)試的問題,一起看看吧2021-06-06Docker容器host與none網(wǎng)絡(luò)的使用
本文主要介紹了Docker容器host與none網(wǎng)絡(luò)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-06-06docker安裝Jenkins執(zhí)行構(gòu)建jar運(yùn)行方式
這篇文章主要介紹了docker安裝Jenkins執(zhí)行構(gòu)建jar運(yùn)行方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Windows10系統(tǒng)下docker安裝指南以及踩坑解決
這篇文章主要給大家介紹了關(guān)于Windows10系統(tǒng)下docker安裝指南以及踩坑解決的相關(guān)資料,相信大家都知道Docker有很多種安裝的選擇,其中支持最好的是Ubuntu系統(tǒng),而且docker如果想在windows上運(yùn)行必須借助docker-machine,需要的朋友可以參考下2023-08-08Docker 解決獲取容器獲取的時(shí)間不是本地時(shí)間的問題
這篇文章主要介紹了Docker 解決獲取容器獲取的時(shí)間不是本地時(shí)間的問題的相關(guān)資料,需要的朋友可以參考下2016-11-11