Docker 使用到的與 Linux 網絡有關的技術分別有:網絡名稱空間、Veth、Iptables、網橋、路由python
ps : 若是不一樣網絡名稱空間之間要進行通訊該怎麼辦呢?linux
ip netns list # 查看 ip netns add [namespace] # 添加 ip netns delete [namespace] # 刪除
[root@shawn ~]#ip netns add test1 [root@shawn ~]#ip netns add test2 [root@shawn ~]#ip netns list test2 test1
[root@shawn ~]#ip link add veth1 type veth peer name veth2 [root@shawn ~]#ip link
[root@shawn ~]#ip link set veth1 netns test1 [root@shawn ~]#ip link set veth2 netns test2
[root@shawn ~]#ip netns exec test1 ip link [root@shawn ~]#ip netns exec test2 ip link
[root@shawn ~]#ip netns exec test1 ip addr add 172.17.0.111/20 dev veth1 [root@shawn ~]#ip netns exec test2 ip addr add 172.17.0.112/20 dev veth2 [root@shawn ~]#ip netns exec test1 ip link set dev veth1 up [root@shawn ~]#ip netns exec test2 ip link set dev veth2 up
[root@shawn ~]#ip netns exec test1 ip a [root@shawn ~]#ip netns exec test2 ip a
[root@shawn ~]#ip netns exec test1 ping 172.17.0.112 [root@shawn ~]#ip netns exec test2 ping 172.17.0.111
docker network [命令參數]
⚽查看當前系統有哪些網橋 "ls" [root@shawn ~]#docker network ls ''' NETWORK ID NAME DRIVER SCOPE befd59194a71 bridge bridge local 94f8e35f3357 host host local 79fb28a9a12e none null local ''' ⚽建立網橋 "create" [root@shawn ~]#docker network create shawn ffac93578a0ce40395936d226bd097fd049ad077022419a9b6b074b6fe2f892b [root@shawn ~]#docker network ls ''' NETWORK ID NAME DRIVER SCOPE befd59194a71 bridge bridge local 94f8e35f3357 host host local 79fb28a9a12e none null local ffac93578a0c shawn bridge local #新建的網橋 ''' ⚽查看網橋信息,格式 : "docker network inspect [網橋的名稱|網橋ID]" [root@shawn ~]#docker network inspect shawn ''' [ { "Name": "shawn", "Id": "ffac93578a0ce40395936d226bd097fd049ad077022419a9b6b074b6fe2f892b", "Created": "2020-12-03T11:56:35.554136022+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.18.0.0/16", "Gateway": "172.18.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ] ''' ⚽刪除網橋 "rm" [root@shawn ~]#docker network rm shawn shawn [root@shawn ~]#docker network ls ''' NETWORK ID NAME DRIVER SCOPE befd59194a71 bridge bridge local 94f8e35f3357 host host local 79fb28a9a12e none null local ''' # "Shawn"被刪除了 ⚽清理網橋 "prune", 咱們先建立多個網橋,而後一次性清除 [root@shawn ~]#docker network create shawn1 f4d2f2b57b48cd35b3cc9eddad0377cae95d213032ed0d6a92e9de9571adeb4e #建立成功 [root@shawn ~]#docker network create shawn2 ea66ed0d06adbe7f5211f3ae38b6edeb47dffac0f53240e9204fd3dcaf4670d5 #建立成功 [root@shawn ~]#docker network create shawn3 222d6e298d6091cea0a6229e19c84825a41ea92b7c0b512db47c858dde7259f0 #建立成功 [root@shawn ~]#docker network prune WARNING! This will remove all custom networks not used by at least one container. Are you sure you want to continue? [y/N] y # 問你是否要這樣作"yes" Deleted Networks: # 將要刪除如下網橋 shawn1 shawn2 shawn3 [root@shawn ~]#docker network ls ''' NETWORK ID NAME DRIVER SCOPE befd59194a71 bridge bridge local 94f8e35f3357 host host local 79fb28a9a12e none null local ''' # 發現並無 "shawn"系列的網橋
network namespace | 主要提供了關於網絡資源的隔離,包括網絡設備、IPv4 和 IPv6 協議棧、IP 路由 表、防火牆、/proc/net 目錄、/sys/class/net 目錄、端口(socket)等 |
---|---|
linux Bridge | 功能至關於物理交換機,爲連在其上的設備(容器)轉發數據幀。如 docker0 網橋 |
iptables | 主要爲容器提供 NAT 以及容器網絡安全 |
veth pair | 兩個虛擬網卡組成的數據通道。在 Docker 中,用於鏈接 Docker 容器和 Linux Bridge。一端在容器中做爲 eth0 網卡,另外一端在 Linux Bridge 中做爲網橋的一 個端口 |
安裝Docker時,它會自動建立三個網絡,bridge(建立容器默認鏈接到此網絡)、 none 、hostnginx
docker network ls
: 查看當前系統有哪些網絡(網橋)[root@shawn ~]#docker network ls NETWORK ID NAME DRIVER SCOPE befd59194a71 bridge bridge local 94f8e35f3357 host host local 79fb28a9a12e none null local
網絡模式 | 設置方法 | 簡介 |
---|---|---|
Host | --network host | 容器將不會虛擬出本身的網卡,配置本身的IP等,而是使用宿主機的IP和端口 |
Bridge | --network bridge(默認此模式) | 此模式會爲每個容器分配、設置IP等,並將容器鏈接到一個docker0虛擬網橋,經過docker0網橋以及Iptables nat表配置與宿主機通訊 |
None | -- network none | 該模式關閉了容器的網絡功能 |
Container | --network "container:[共享容器名稱]" | 建立的容器不會建立本身的網卡,配置本身的IP,而是和一個指定的容器共享IP、端口範圍 |
docker run --network host [鏡像名稱或ID]
[root@shawn ~]#docker run -d --network host nginx:latest
docker run --network "containe:[共享容器名稱]" [鏡像名稱或ID]
⚽建立共享容器 "cont01" [root@shawn ~]#docker run -dit --name cont01 busybox:latest sh ⚽建立連接容器 "cont02", 並指定網絡模式 "container" [root@shawn ~]#docker run -dit --name cont02 --network "container:cont01" busybox:latest sh ⚽查看兩個容器的 "ip", 發現同樣 [root@shawn ~]#docker exec cont01 ip a [root@shawn ~]#docker exec cont02 ip a
docker run --network none [鏡像名稱或ID]
[root@shawn ~]#docker run -dit --network none --name none_test busybox:latest sh