一、背景
在Linux上docker映射了端口筷屡,想著對服務(wù)端口進(jìn)行限制指定IP訪問涧偷,發(fā)現(xiàn)在filter表的INPUT鏈限制無效。
二毙死、 分析
# iptables -L -nv --line-numbers
Chain INPUT (policy ACCEPT 200K packets, 8397K bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP tcp -- * * 192.168.4.200 0.0.0.0/0 tcp dpt:443
Docker容器啟動防火墻上的變動,如果暴露本機(jī)端口喻鳄,在nat表的DOCKER鏈上增加一條規(guī)則:
DNAT tcp -- !<docker-network> 0.0.0.0/0 0.0.0.0/0 tcp dpt:<dest port> to:<new address>:<new port>
可以通過命令sudo iptables -t nat -nvL DOCKER查看扼倘。
通過在nat表增加規(guī)則來drop掉所有訪問容器暴露端口的連接,最后處理結(jié)果如下:
# iptables -L -nv --line-numbers
Chain DOCKER (2 references)
num pkts bytes target prot opt in out source destination
1 0 0 DROP tcp -- * * 192.168.4.202 0.0.0.0/0 tcp
2 3 180 DROP tcp -- * * 192.168.4.200 0.0.0.0/0 tcp
3 2 120 DROP tcp -- * * 192.168.4.200 0.0.0.0/0 tcp dpt:443
4 0 0 ACCEPT tcp -- !br-9c0d36154c4e br-9c0d36154c4e 0.0.0.0/0 172.18.0.11 tcp dpt:6379
在Linux上除呵,Docker操縱iptables提供網(wǎng)絡(luò)隔離的規(guī)則再菊。
雖然這是一個(gè)實(shí)現(xiàn)細(xì)節(jié),您不應(yīng)該修改Docker插入到您的iptables政策颜曾,如果你想在Docker管理的政策之外擁有自己的政策纠拔,它確實(shí)對你需要做的事情有一些影響。
如果您在一臺暴露于互聯(lián)網(wǎng)的主機(jī)上運(yùn)行Docker泛豪,那么您可能希望有iptables策略來防止對容器或其他在您的主機(jī)上運(yùn)行的服務(wù)的未授權(quán)訪問稠诲。
Docker安裝了兩個(gè)定制的iptables鏈,名為DOCKER-USER和DOCKER诡曙,它確保傳入的數(shù)據(jù)包總是首先由這兩個(gè)鏈檢查臀叙。
Docker的所有iptables規(guī)則都被添加到Docker鏈中,不要手動操作該鏈條价卤。
如果您需要添加在Docker規(guī)則之前加載的規(guī)則劝萤,請將它們添加到DOCKER-USER鏈中。這些規(guī)則是在Docker自動創(chuàng)建任何規(guī)則之前應(yīng)用的慎璧。
添加到轉(zhuǎn)發(fā)鏈中的規(guī)則——無論是手動添加的床嫌,還是由另一個(gè)基于iptables的防火墻添加的——都會在這些鏈之后進(jìn)行評估跨释。這意味著如果您通過Docker暴露一個(gè)端口,無論您的防火墻配置了什么規(guī)則厌处,該端口都會被暴露鳖谈。
如果您希望這些規(guī)則即使在端口通過Docker暴露時(shí)也適用,那么您必須將這些規(guī)則添加到DOCKER-USER鏈中嘱蛋。
三蚯姆、實(shí)例
只允許 192.168.1.101 ~ 192.168.1.103 的主機(jī)訪問本機(jī)的 1~65535/tcp 1~65535/udp。
# cat set_iptables_rules.sh
#!/bin/bash
nic="eth0"
ips="
192.168.1.101
192.168.1.102
192.168.1.103
"
iptables -I DOCKER -i ${nic} -p tcp --dport 1:65535 -j DROP
iptables -I DOCKER -i ${nic} -p udp --dport 1:65535 -j DROP
for ip in ${ips}
do
iptables -I DOCKER -i ${nic} -s ${ip} -p tcp --dport 1:65535 -j ACCEPT
iptables -I DOCKER -i ${nic} -s ${ip} -p udp --dport 1:65535 -j ACCEPT
done
注:此操作是臨時(shí)生效洒敏,服務(wù)器重啟失效
四龄恋、將iptables策略注冊成系統(tǒng)服務(wù),實(shí)現(xiàn)開機(jī)自啟
# setenforce 0
# sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
# cat /etc/systemd/system/iptables.service
[Unit]
Description=iptables rules service
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/bash /usr/sbin/set_iptables_rules.sh
ExecStop=/usr/sbin/iptables -P INPUT ACCEPT
ExecStop=/usr/sbin/iptables -F
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
# cat /usr/sbin/set_iptables_rules.sh
#!/bin/bash
nic="eth0"
ips="
192.168.1.101
192.168.1.102
192.168.1.103
"
iptables -I DOCKER -i ${nic} -p tcp --dport 1:65535 -j DROP
iptables -I DOCKER -i ${nic} -p udp --dport 1:65535 -j DROP
for ip in ${ips}
do
iptables -I DOCKER -i ${nic} -s ${ip} -p tcp --dport 1:65535 -j ACCEPT
iptables -I DOCKER -i ${nic} -s ${ip} -p udp --dport 1:65535 -j ACCEPT
done
# systemctl daemon-reload
# systemctl start iptables.service
# systemctl enable iptables.service
# systemctl status iptables.service
# iptables -nvL --line
Chain INPUT (policy ACCEPT 188 packets, 19400 bytes)
num pkts bytes target prot opt in out source destination
Chain FORWARD (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DOCKER-ISOLATION all -- * * 0.0.0.0/0 0.0.0.0/0
2 0 0 DOCKER all -- * docker0 0.0.0.0/0 0.0.0.0/0
3 0 0 ACCEPT all -- * docker0 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
4 0 0 ACCEPT all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0
5 0 0 ACCEPT all -- docker0 docker0 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT 154 packets, 21532 bytes)
num pkts bytes target prot opt in out source destination
Chain DOCKER (1 references)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT udp -- eth0 * 192.168.1.103 0.0.0.0/0 udp dpts:1:65535
2 0 0 ACCEPT tcp -- eth0 * 192.168.1.103 0.0.0.0/0 tcp dpts:1:65535
3 0 0 ACCEPT udp -- eth0 * 192.168.1.102 0.0.0.0/0 udp dpts:1:65535
4 0 0 ACCEPT tcp -- eth0 * 192.168.1.102 0.0.0.0/0 tcp dpts:1:65535
5 0 0 ACCEPT udp -- eth0 * 192.168.1.101 0.0.0.0/0 udp dpts:1:65535
6 0 0 ACCEPT tcp -- eth0 * 192.168.1.101 0.0.0.0/0 tcp dpts:1:65535
7 0 0 DROP udp -- eth0 * 0.0.0.0/0 0.0.0.0/0 udp dpts:1:65535
8 0 0 DROP tcp -- eth0 * 0.0.0.0/0 0.0.0.0/0 tcp dpts:1:65535
Chain DOCKER-ISOLATION (1 references)
num pkts bytes target prot opt in out source destination
1 0 0 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0
# systemctl stop iptables.service
# iptables -nvL --line
Chain INPUT (policy ACCEPT 27 packets, 2968 bytes)
num pkts bytes target prot opt in out source destination
Chain FORWARD (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 24 packets, 3141 bytes)
num pkts bytes target prot opt in out source destination
Chain DOCKER (0 references)
num pkts bytes target prot opt in out source destination
Chain DOCKER-ISOLATION (0 references)
num pkts bytes target prot opt in out source destination
注意: 對運(yùn)行docker的主機(jī)執(zhí)行 iptables -F 凶伙,會直接清空所有的docker的iptables策略郭毕,需要重啟docker
五、參考
Docker and iptables
https://docs.docker.com/network/iptables
iptables匹配端口范圍函荣,映射显押,網(wǎng)絡(luò)狀態(tài)
http://t.zoukankan.com/Carr-p-7396031.html
iptables阻止了應(yīng)用連到 容器化的MySQL
https://www.cnblogs.com/shaoyang0123/p/15065439.html
iptables限制docker端口禁止對某臺主機(jī)進(jìn)行提供服務(wù)
https://blog.csdn.net/qq_50573146/article/details/125833273
iptables限制Docker IP和端口訪問
https://blog.csdn.net/yeqinghanwu/article/details/125979997
使用iptables為docker容器配置防火墻策略
https://blog.csdn.net/m0_37814112/article/details/121229022
Linux運(yùn)維實(shí)戰(zhàn)總結(jié)
https://blog.csdn.net/m0_37814112/category_10867749.html