欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

docker的iptables策略詳解和用戶自定義策略的添加方式

 更新時間:2024年10月10日 11:06:46   作者:玄德公筆記  
在Docker環(huán)境下,直接修改iptables以允許特定主機訪問指定端口時,需要考慮Docker自身的iptables規(guī)則,Docker通過修改nat表的PREROUTING鏈和filter表的FORWARD鏈來處理外部對Docker容器的訪問,繞過了filter表的INPUT鏈

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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

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

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

    在日常開發(fā)中,往往可能同時多個項目并行進行開發(fā),本文介紹了jenkins+gitlab+nginx部署前端應用實現(xiàn),感興趣的可以了解一下
    2021-05-05
  • 聊聊docker?單機部署redis集群的問題

    聊聊docker?單機部署redis集群的問題

    這篇文章主要介紹了docker?單機部署redis集群,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • 使用Docker快速搭建Oracle開發(fā)環(huán)境的方法教程

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

    這篇文章主要給大家介紹了使用Docker快速搭建Oracle開發(fā)環(huán)境的方法教程,文中給出了詳細的解決方法,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-06-06
  • 給debian的docker容器添加crontab定時任務

    給debian的docker容器添加crontab定時任務

    這篇文章主要介紹了給debian的docker容器添加crontab定時任務的相關知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • docker 復制容器的實現(xiàn)步驟

    docker 復制容器的實現(xiàn)步驟

    本文主要介紹了docker 復制容器的實現(xiàn)步驟,主要介紹了三種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-10-10
  • 最新評論