![(img-1G0ojS4c-1680514267280)(E:\培训\docker-iptables.png)\]](https://img-blog.csdnimg.cn/976b2010c9f64df397ebbe3a393ef682.png)

1.规划好ip地址

docker1和docker2使用NAT模式,Linux网关服务器(后面用router称呼)使用ens33桥接模式,ens37使用NAT模式。
docker1(ens33):
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
BOOTPROTO="none"
DEFROUTE="yes"
NAME="ens33"
UUID="5632f613-0e37-4aca-8651-d323ca0aaa10"
DEVICE="ens33"
IPADDR='192.168.10.201'
PREFIX=24
GATEWAY='192.168.10.254'
DNS1=114.114.114.114
ONBOOT="yes"

docker2(ens33):
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO="none"
NAME="ens33"
UUID="5632f613-0e37-4aca-8651-d323ca0aaa10"
DEVICE="ens33"
IPADDR='192.168.10.202'
PREFIX=24
GATEWAY='192.168.10.254'
DNS1=114.114.114.114
ONBOOT="yes"

router(ens37):
[root@router ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens37
BOOTPROTO="none"
NAME="ens37"
DEVICE="ens37"
ONBOOT="yes"
IPADDR=192.168.10.254
PREFIX=24

router(ens33):
[root@router ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO="none"
NAME="ens33"
UUID="38ee3425-66e3-4565-884b-02ac1fc85c71"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.1.205
GATEWAY=192.168.1.1
PREFIX=24
DNS1=114.114.114.114

改完ip后所有机器都要刷新服务

service network restart

2.在router里编写snat。

[root@router ~]# vim snat.sh

#!/bin/bash
#打开路由功能
echo 1 > /proc/sys/net/ipv4/ip_forward

#清空iptables里的所有规则
iptables -F
iptables -t nat -F
iptables -P INPUT ACCEPT

#配置SNAT策略
iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o ens33 -j SNAT --to-source 192.168.1.205
bash snat.sh

通过snat可以实现内网(nginx和mysql)访问外部网络

windows机器需要添加一条路由

管理员打开cmd输入以下语句

route add 192.168.10.0/24 192.168.1.205

在docker1和docker2执行ping命令,成功

ping www.baidu.com
PING www.a.shifen.com (14.119.104.189) 56(84) bytes of data.
64 bytes from 14.119.104.189 (14.119.104.189): icmp_seq=1 ttl=53 time=61.9 ms
64 bytes from 14.119.104.189 (14.119.104.189): icmp_seq=2 ttl=53 time=42.6 ms
64 bytes from 14.119.104.189 (14.119.104.189): icmp_seq=3 ttl=53 time=42.6 ms
64 bytes from 14.119.104.189 (14.119.104.189): icmp_seq=4 ttl=53 time=23.7 ms
64 bytes from 14.119.104.189 (14.119.104.189): icmp_seq=5 ttl=53 time=68.8 ms

3.在router编写dnat脚本

#!/bin/bash
#关闭防火墙
service firewalld stop
setenforce 0
#打开路由功能
echo 1 > /proc/sys/net/ipv4/ip_forward

#清除所有规则
#iptables -F
#iptables -t nat -F
#iptables -P INPUT ACCEPT

#开启dnat(docker1)
iptables -t nat -A PREROUTING -d 192.168.1.205 -p tcp --dport 80 -j DNAT --to-destination 192.168.10.201:8091
#(docker2)
iptables -t nat -A PREROUTING -d 192.168.1.205 -p tcp --dport 3306 -j DNAT --to-destination 192.168.10.202:33060
bash dnat.sh

4.测试

dnat脚本用于实现外部网络访问内部(nginx和mysql),因为执行snat脚本时已经添加了路由,所以清除规则这里就注释了。

在docker1使用docker启动一台nginx

因为我已经run了一台,所以这里直接使用start开启一台nginx,端口映射为80:8091

基于iptables的snat和dnat的docker发布项目_mysql

使用另一台windows机器(192.168.1.145)访问nginx

这里是访问192.168.1.205(router的ens33的ip),返回的结果是docker里面nginx的index.html内容,说明dnat在实现了外网访问docker1.

基于iptables的snat和dnat的docker发布项目_mysql_02

在docker2使用docker启动一台mysql

[root@localhost ~]# docker run -d --name sc-mysql-1 -p 33060:3306 -e MYSQL_ROOT_PASSWORD='xzx527416' mysql:5.7.41
Unable to find image 'mysql:5.7.41' locally
5.7.41: Pulling from library/mysql
e83e8f2e82cc: Pull complete 
0f23deb01b84: Pull complete 
f5bda3b184ea: Pull complete 
ed17edbc6604: Pull complete 
33a94a6acfa7: Pull complete 
3686cf92b89d: Pull complete 
f81535a6a8bf: Pull complete 
4bffb03ea5e2: Pull complete 
49348ef8dcaa: Pull complete 
509d665d0cf5: Pull complete 
adc919b937fd: Pull complete 
Digest: sha256:bf18020f32cc5d8f5e2add516d52fbf3afc3de431457076340e938596c528171
Status: Downloaded newer image for mysql:5.7.41
63523519747bcc49536d36ba8bebcc9911e7c7723219b4bf66b4245451e3dd78
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS                                                    NAMES
63523519747b   mysql:5.7.41   "docker-entrypoint.s…"   8 seconds ago   Up 6 seconds   33060/tcp, 0.0.0.0:33060->3306/tcp, :::33060->3306/tcp   sc-mysql-1

在windows使用navicat连接一下此容器

基于iptables的snat和dnat的docker发布项目_mysql_03

![\外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

测试成功。

到此,关于docker、snat、dnat、iptables的实验完成了。