【云原生 | 16】Docker网络之NetWork网络隔离

583 阅读4分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第16天,点击查看活动详情

作者简介:🏅云计算领域优质创作者🏅新星计划第三季python赛道TOP1🏅 阿里云ACE认证高级工程师🏅
✒️个人主页:小鹏linux
💊个人社区:小鹏linux(个人社区)欢迎您的加入!

网络

    联通性

        容器 与 容器间的通讯:veth Bridge

        容器与外部网络:SNAT

        外部网络与容器内部:DNAT

    隔离性

        NameSpace network

1. 进程隔离方式

2. NetWork网络隔离

netns 是在 linux 中提供网络虚拟化的一个项目,使用 netns 网络空间虚拟化可以在本地虚拟化出多个网络环境,目前 netns 在 lxc 容器中被用来为容器提供网络

使用 netns 创建的网络空间独立于当前系统的网络空间,其中的网络设备以及 iptables 规则等都是独立的,就好像进入了另外一个网络一样 .

2.1 传统虚拟化网络

2.2 容器级虚拟化

3. bridge网桥模式实验

3.1 bridge网桥模式实验原理

每个容器启动时会创建一对虚拟网卡,一个网卡在容器内的虚拟空间内,一个在主机的docker网桥上,然后网卡一连接网卡二,网卡二连接网桥。其他容器的虚拟网卡也都连接到网桥(三层交换)上,就可以使不同的容器之间互相通信了。

3.2 bridge网桥模式实验步骤

[root@localhost ~]# ip netns add r1					# 创建虚拟网络空间,相当于分割出新的r1空间。
[root@localhost ~]# ip netns list					#可以查看到r1空间
[root@localhost ~]# ip link add veth1.1 type veth peer name veth1.2	#创建一对虚拟网络设备veth1.1和veth1.2
[root@localhost ~]# ip link set veth1.1 netns r1	#将veth1.1的虚拟网卡放入r1虚拟空间
[root@localhost ~]# ip netns exec r1 bash			#进入r1虚拟网络空间
[root@localhost ~]# ip link set veth1.1 name eth0	#更改网络名称
[root@localhost ~]# ip link set lo up				#启动本地回环接口,使其正常工作
[root@localhost ~]# ip link set eth0 up				#启动本地回环接口内的eth0虚拟网卡,使其正常工作
[root@localhost ~]# ip addr add 192.168.34.163/24 dev eth0	#设置ip地址
[root@localhost ~]# exit							#退出当前虚拟空间
[root@localhost ~]# ip netns add r2					# 再创建虚拟网络空间,相当于分割出新的r2空间。
[root@localhost ~]# ip link add veth2.1 type veth peer name veth2.2	#创建第二对虚拟网络设备veth2.1和veth2.2
[root@localhost ~]# ip link set veth2.1 netns r2	#将veth2.1的虚拟网卡放入r2虚拟空间
[root@localhost ~]# ip netns exec r2 bash			#进入r2虚拟网络空间
[root@localhost ~]# ip link set veth2.1 name eth0	#更改网络名称
[root@localhost ~]# ip link set lo up				#启动本地回环接口,使其正常工作
[root@localhost ~]# ip link set eth0 up				#启动本地回环接口内的eth0虚拟网卡,使其正常工作
[root@localhost ~]# ip addr add 192.168.34.164/24 dev eth0	#设置ip地址(两个虚拟空间的网卡必须同一网段)
[root@localhost ~]# exit							#退出当前虚拟空间
[root@localhost ~]# ip link add name br0 type bridge		#创建网桥br0
[root@localhost ~]# ip link set br0 up				#启动网桥br0
[root@localhost ~]# ip addr add 192.168.34.162/24 dev br0	#设置网桥ip
[root@localhost ~]# ip link set dev veth1.2  master br0	#将veth1.2虚拟网卡连接到网桥
[root@localhost ~]# ip link set dev veth2.2  master br0	#将veth2.2虚拟网卡连接到网桥
[root@localhost ~]# ip link set veth1.2 up				#启动虚拟网卡veth1.2
[root@localhost ~]# ip link set veth2.2 up				#启动虚拟网卡veth1.2

互相ping一下查看网络是否连通

3.3 Docker NS 显示

docker inspect --format='{{.State.Pid}} ' $container_id	    # 查看当前容器的名字空间 ID
mkdir /var/run/netns	            # 创建目录,防止未创建
ln -s /proc/1234/ns/net /var/run/netns/1234	    #在 /proc 目录(保存进程的所有相关信息)下,把对应的网络名字空间文件链接到 /var/run/netns 下面
ip netns show	
ip netns exec 1234 ifconfig eth0 172.16.0.10/16...		#查看或访问容器的名字空间

3.4 firewalld 规则

iptables -t nat -A POSTROUTING -s 172.17.0.0/16 -o docker0 -j MASQUERADE	#容器访问外部网络
docker run -d -p 80:80 apache 	#外部网络访问容器
iptables -t nat -A  PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
iptables -t nat -A DOCKER ! -i docker0 -p tcp -m tcp --dport 80 -j  DNAT --to-destination 172.17.0.2:80

👑👑👑结束语👑👑👑