1 ) Docker0 网络
ip addr
可以看到Docker0网络相关信息docker run -itd --name centos1 centos /bin/bash
docker run -itd --name centos2 centos /bin/bash
docker run -itd --name centos3 centos /bin/bash
ip addr
, 又多了 3 条docker exec -it centos1 ip addr
查看ip地址docker exec -it centos2 ip addr
查看ip地址docker exec -it centos3 ip addr
查看ip地址2 )通信原理
3 )查看网络
$ docker network --h
Flag shorthand -h has been deprecated, please use --help
Usage: docker network COMMAND
Manage networks
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
prune Remove all unused networks
rm Remove one or more networks
Run 'docker network COMMAND --help' for more information on a command.
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
1abcdbddaf9d bridge bridge local
64c08dba087e host host local
9b5e3b4957b1 none null local
1abcdbddaf9d bridge bridge local
是docker0对应的网络$ docker network inspect bridge
4 )使用默认网络的问题
5 )关于微服务通信
docker exec -it centos2 /bin/bash
ping centos1
发现ping不通6 )解决方案
要解决默认网络的问题,就需要自定义网络
创建自己的网络
docker network create --help
Usage: docker network create [OPTIONS] NETWORK
Create a network
Options:
--attachable Enable manual container attachment
--aux-address map Auxiliary IPv4 or IPv6 addresses used by
Network driver (default map[])
--config-from string The network from which to copy the configuration
--config-only Create a configuration only network
-d, --driver string Driver to manage the Network (default "bridge")
--gateway strings IPv4 or IPv6 Gateway for the master subnet
--ingress Create swarm routing-mesh network
--internal Restrict external access to the network
--ip-range strings Allocate container ip from a sub-range
--ipam-driver string IP Address Management Driver (default "default")
--ipam-opt map Set IPAM driver specific options (default map[])
--ipv6 Enable IPv6 networking
--label list Set metadata on a network
-o, --opt map Set driver specific options (default map[])
--scope string Control the network's scope
--subnet strings Subnet in CIDR format that represents a
network segment
通过上述命令创建好网络后,启动容器并加入该网络
docker network create --driver bridge --subnet 192.168.1.0/24 --gateway 192.168.1.1 docker1
创建docker1网络docker network create --driver bridge --subnet 192.168.2.0/24 --gateway 192.168.2.1 docker2
创建docker2网络docker run -tid --name centos1 --net docker1 centos /bin/bash
将cenos1加入docker1网络docker run -tid --name centos2 --net docker1 centos /bin/bash
将cenos2加入docker1网络docker run -tid --name centos3 --net docker2 centos /bin/bash
将cenos3加入docker2网络docker exec -it centos1 ping centos2
同一网络下的 centos1 和 centos2 两个容器可以 ping 通docker exec -it centos1 ping centos3
不同网络下的 centos1 和 centos3 两个容器实现了网络隔离(ping 不通)关于 /16 和 /24
上面创建网络时,需要制定网络类型,网络类型,有如下四种
Docker 网络模式 | 配置 | 说明 |
---|---|---|
host 模式 | --net=host | 容器和宿主机共享 Network namespace |
container 模式 | --net=container:NAMEorID | 容器和另外一个容器共享 Network namespace。kubernetes中的pod就是多个容器共享一个Network namespace |
none 模式 | --net=none | 容器有独立的Network namespace, 但并没有对其进行任何网络设置,如分配 veth pair 和网桥连接,配置IP等 |
bridge 模式 | --net=bridge | (默认为该模式) |
docker network connect --help
Usage: docker network connect [OPTIONS] NETWORK CONTAINER
Connect a container to a network
Options:
--alias strings Add network-scoped alias for the container
--driver-opt strings driver options for the network
--ip string IPv4 address (e.g., "172.30.100.104")
--ip6 string IPv6 address (e.g., "2001:db8::33")
--link list Add link to another container
--link-local-ip strings Add a link-local address for the container
docker network connect docker2 centos2
把centos2容器加入docker2网络中docker exec -it centos2 ping centos3
发现ping通docker network create --driver bridge --subnet 192.168.1.0/24 --gateway 192.168.1.1 dockerNet1
docker network create --driver bridge --subnet 192.168.2.0/24 --gateway 192.168.2.1 dockerNet2
docker run -it --name c1 --net dockerNet1 centos /bin/bash
docker run -it --name c2 --net dockerNet2 centos /bin/bash
route
可查看路由表route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.220.111
route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.220.110
route
发现多了路由的规则iptables -t nat -I PREROUTING -s 192.168.1.0/24 -d 192.168.2.0/24 -j DNAT --to 192.168.1.1
iptables -t nat -I PREROUTING -s 192.168.2.0/24 -d 192.168.1.0/24 -j DNAT --to 192.168.2.1
-s
本主机配置的网段-d
目标主机的网段-j
地址转换--to
本主机网关docker exec -it c1 ping c2
可以畅通docker exec -it c2 ping c1
可以畅通