目录
知识补充:
Linux网桥
真实网卡配置文件
– cat /etc/sysconfig/network-scripts/ifcfg-eth0
虚拟网卡配置文件
– cat /etc/sysconfig/network-scripts/ifcfg-eth0:0
TYPE=Ethernet
BOOTPROTO=static
… …
NAME=eth0:0
DEVICE=eth0:0
ONBOOT=yes
IPADDR=192.168.4.15
创建虚拟网桥
cat /etc/sysconfig/network-scripts/ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
… …
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.4.15
cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
… …
NAME=eth0
DEVICE=eth0
BRIDGE=br0
ONBOOT=yes
IPADDR=192.168.4.15
brctl show —-查看网桥信息
Docker网络拓扑
查看默认Docker创建的网络模型
docker network list
NETWORK ID NAME DRIVER SCOPE
c0ae28d57b18 bridge bridge local 桥接模型
b69d4c0c735f host host local 主机模型
4dc88be13b81 none null local 无网络
ip a s docker0
brctl show docker0 //启动容器会绑定该网桥
使用Docker创建网桥
~]# docker network create –driver bridge test01
~]# docker network list
~]# ip a s
~]# docker network inspect test01
自定义网段
]# docker network create –subnet=172.30.0.0/16 test01
]# docker run –network=test01 -id nginx
客户端访问容器内的资源
• 默认容器通过SNAT可以访问外网
• 但外部网络的主机不可以访问容器内的资源
• 端口映射
– 使用端口映射可以实现外部网络访问容器内的资源
docker run -p 8080 80 -id nginx
//如:真实机IP为192.168.4.5,
使用-p映射真实机的8080端口到容器中的80端口
案例一:实现docker进行子网隔离
在docker上默认有一个网桥172.17.0.1,在创建一个新的网桥192.168.100.0/24 这样就相当于拥有两个valn,同一个vlan可以进行相互的通信,不同的vlan将会隔离
[root@docker1 ~]# ifconfig docker
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:30ff:fe8d:9ac5 prefixlen 64 scopeid 0x20<link>
ether 02:42:30:8d:9a:c5 txqueuelen 0 (Ethernet)
RX packets 8 bytes 536 (536.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 648 (648.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@docker1 network-scripts]# docker network –help
Usage: docker network COMMAND
Manage Docker networks
Options:
–help Print usage
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
rm Remove one or more networks
Run ‘docker network COMMAND –help’ for more information on a command.
[root@docker1 network-scripts]# docker network create –subnet 192.168.100.0/24 doker1 —做一个网桥
0265d27fadf3c9b92879bc3abefa0613f6cbffd56b271be9f5f3484e67f6f7d7
[root@docker1 network-scripts]# docker network ls
NETWORK ID NAME DRIVER SCOPE
4ae4e1a890dd bridge bridge local
29f54dd01366 docker_gwbridge bridge local
0265d27fadf3 doker1 bridge local
a2e80ce2ef33 host host local
45de7265b4e2 none null local
[root@docker1 network-scripts]# ifconfig
br-0265d27fadf3: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.100.1 netmask 255.255.255.0 broadcast 0.0.0.0
ether 02:42:0a:d1:b1:a1 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
启动第一个容器
[root@docker1 ~]# docker run -it –network doker1 centos 使用网桥创建容器
[root@aee8b8561319 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.3 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 fe80::42:c0ff:fea8:6403 prefixlen 64 scopeid 0x20<link>
ether 02:42:c0:a8:64:03 txqueuelen 0 (Ethernet)
RX packets 7797 bytes 12868997 (12.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6096 bytes 467006 (456.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
启动第二个容器
[root@docker1 ~]# docker run -it –network doker1 centos
[root@ebc04173d787 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.4 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 fe80::42:c0ff:fea8:6404 prefixlen 64 scopeid 0x20<link>
ether 02:42:c0:a8:64:04 txqueuelen 0 (Ethernet)
RX packets 8398 bytes 12867619 (12.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6629 bytes 517452 (505.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
启动第三个容器
[root@docker1 ~]# docker run -it centos
[root@ec4270adb667 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:3 prefixlen 64 scopeid 0x20<link>
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 8412 bytes 12879335 (12.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7231 bytes 574112 (560.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
案例二:做端口映射,使得外界可以访问容器
创建一个新的容器,使用宿主机的端口 -p 宿主机端口:容器端口
[root@docker1 ~]# docker run -d -v /var/data:/usr/share/nginx/html/ -p 80:80 nginx
[root@docker1 ~]# cd /var/data/
[root@docker1 data]# ls
[root@docker1 data]# echo “hello world!” > index.html
[root@pc01 ~]# curl http://192.168.6.1 —doecker的宿主机
hello world