Docker自定義網路
阿新 • • 發佈:2021-07-10
Docker自定義網路
檢視所有的docker網路
# 檢視docker network的幫助命令 docker network --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 # 檢視docker network docker network ls NETWORK ID NAME DRIVER SCOPE 94fc848a4fd9 bridge bridge local 7340c21cddff host host local 1338cd2e6834 none null local
網路模式
bridge: 橋接模式(預設,自己建立也使用bridge)
none:不配製網路
host:和宿主機共享網路
container:容器內網路聯通!(用的少,侷限性很大)
測試
# 我們直接啟動的命令 --net bridge, 這個是我們的docker0 docker run -d -P --name tomcat01 --net bridge tomcat # docker0的特點, 預設的,域名是不能訪問的, --link是可以打通連線 # 自定一個網路 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.0.0/16 --gateway 192.168.0.1 mynet bfb8374fa9c4f7822777e8e30ed25bb450ee46151377cd954425697d34fd2a7c # 檢視自定義的網路是否建立成功 docker network ls NETWORK ID NAME DRIVER SCOPE 94fc848a4fd9 bridge bridge local 7340c21cddff host host local bfb8374fa9c4 mynet bridge local 1338cd2e6834 none null local # 檢視自己建立的自定義網路 docker network inspect mynet [ { "Name": "mynet", "Id": "bfb8374fa9c4f7822777e8e30ed25bb450ee46151377cd954425697d34fd2a7c", "Created": "2021-07-10T06:34:17.9819573Z", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "192.168.0.0/16", "Gateway": "192.168.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ] # 測試下自己的容器網路 docker run -d -P --name tomcat-net-01 --net mynet tomcat d18bedc300d7f7f5e854875ac88594b56311bb417c223cd89f8fed02178a614e docker run -d -P --name tomcat-net-02 --net mynet tomcat 283b394949d0230ce159e675f12f765fa6e7c9978d8103521d75da5b95ffde46 # 啟動容器後,檢視自己建立網路的容器情況 docker network inspect mynet [ { "Name": "mynet", "Id": "bfb8374fa9c4f7822777e8e30ed25bb450ee46151377cd954425697d34fd2a7c", "Created": "2021-07-10T06:34:17.9819573Z", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "192.168.0.0/16", "Gateway": "192.168.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": { "283b394949d0230ce159e675f12f765fa6e7c9978d8103521d75da5b95ffde46": { "Name": "tomcat-net-02", "EndpointID": "784507211c5661cae82fc7461d866af69669bc440d48e821e31144b5fd4a7877", "MacAddress": "02:42:c0:a8:00:03", "IPv4Address": "192.168.0.3/16", "IPv6Address": "" }, "d18bedc300d7f7f5e854875ac88594b56311bb417c223cd89f8fed02178a614e": { "Name": "tomcat-net-01", "EndpointID": "7ddc1493e42c89ef3793bdfcc273df3c8513693d342a613c670846ac2101a302", "MacAddress": "02:42:c0:a8:00:02", "IPv4Address": "192.168.0.2/16", "IPv6Address": "" } }, "Options": {}, "Labels": {} } ] # 檢視下自定義網路是否能ping通容器的ip docker exec -it tomcat-net-01 ping 192.168.0.3 PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data. 64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.171 ms 64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.306 ms 64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.349 ms 64 bytes from 192.168.0.3: icmp_seq=4 ttl=64 time=0.239 ms 64 bytes from 192.168.0.3: icmp_seq=5 ttl=64 time=0.341 ms 64 bytes from 192.168.0.3: icmp_seq=6 ttl=64 time=0.383 ms # 檢視自定義的網路是否能ping通容器名 docker exec -it tomcat-net-01 ping tomcat-net-02 PING tomcat-net-02 (192.168.0.3) 56(84) bytes of data. 64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.066 ms 64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.291 ms 64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.317 ms 64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=4 ttl=64 time=0.451 ms 64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=5 ttl=64 time=1.39 ms 64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=6 ttl=64 time=0.257 ms
自定義網路的好處
不同的叢集使用不同的網路,保證叢集是安全和健康的