Docker的iptables規則在iptables重啟後丟失

 

前因後果

1、在跳板機上使用ansible命令測試機器B時,報錯如下,於是就懷疑是網絡防火牆的問題

10.10.0.86 | FAILED >> {
    "failed": true, 
    "msg": "/bin/sh: /usr/bin/python: No such file or directory\r\nOpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n 7 Dec 2017\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 19: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug1: mux_client_request_session: master session id: 2\r\nShared connection to 10.10.0.86 closed.\r\n", 
    "parsed": false
}

 

2、登錄到機器B上,這是一台線上的機器,裏面運行docker容器,然後我看平時的經驗一般是網絡防火牆有問題就直接

iptables -F
systemctl stop iptables 
systemctl stop firewalld 

 

3、然後我想着這應該不會影響容器吧,就看容器的日誌,沒想到10幾個全部報錯,都是連接不上,有點慌…,重啟容器,完全是起不來的,報錯如下

docker: Error response from daemon: driver failed programming external connectivity on endpoint happy_ptolemy (9cedc114be35eb86cd6f7f7bb4f11f93b5f8d2c0745afc72664cef8e96aad439): iptables failed: iptables --wait -t filter -A DOCKER ! -i docker0 -o docker0 -p tcp -d 10.10.0.86 --dport 3000 -j ACCEPT: iptables: No chain/target/match by that name.

(exit status 1).

 

4、解決辦法,重啟docker

systemctl restart docker

 

知識點

 
1、netfilter與iptables的關係
 
Netfilter是在Linux內核中的一個防火牆框架,用於管理網絡數據包,不僅具有網絡地址轉換(NAT)的功能,也具有數據包內容修改,以及數據包過濾等防火牆功能。利用在用戶空間的應用軟件iptables等來控制Netfilter(iptables只是應用軟件,工具)。
 
2、iptables與firewalld的關係
 
firewalld和iptables一樣都是應用軟件,是工具,但是他的底層還是先通過iptables。
 
 
 
3、docker與firewalld、iptables的關係
 
1)docker安裝完成後,會自動接管iptables或者firewalld,在docker run的時候,會自動往iptables里加入規則;所以當iptables重啟後會丟失,只有再重啟docker就好了的原因。
2)當使用Systemd 的時候, firewalld 會在 Docker 之前啟動,但是如果你在 Docker 啟動之後再啟動 或者重啟 firewalld ,就需要重啟 Docker 進程了。
 

總結

1、對生產環境的敬畏
2、對知識點的理解不透徹,不知道什麼場景能用,什麼場景不能用
3、後面還需要把防火牆的規則再深入了解