您好,登錄后才能下訂單哦!
小編這次要給大家分享的是Docker容器如何實現網絡管理和網絡隔離,文章內容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
一、Docker網絡的管理
1、Docker容器的方式
1)Docker訪問外網
Docker容器連接到宿主機的Docker0網橋訪問外網;默認自動將docker0網橋添加到docker容器中。
2)容器和容器之間通信
需要管理員創建網橋;將不同的容器連接到網橋上實現容器和容器之間相互訪問。
3)外部網絡訪問容器
通過端口映射或者同步docker宿主機網絡配置實現通信。
2、Docker容器網絡通信的模式
1)bridge
默認容器訪問外網通信使用;依賴docker0網橋。
2)none
需要給容器創建獨立的網絡命名空間;不會給創建的容器配置TCP/IP信息。
3)container
容器和容器通信使用;容器需要共享容器名稱空間,通過共享容器名稱空間實現不同容器通信。
4)host
容器內部網絡和宿主機保持同步。
3、配置bridge網絡通信模式
[root@centos01 yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo <!--安裝centos7源--> [root@centos01 ~]# yum -y install docker <!--安裝docker--> [root@centos01 ~]# systemctl start docker <!--啟動docker--> [root@centos01 ~]# systemctl enable docker <!--設置docker開機自動啟動--> [root@centos01 ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf <!--開啟路由功能--> [root@centos01 ~]# sysctl -p <!--刷新配置--> net.ipv4.ip_forward = 1 [root@centos01 ~]# docker pull hub.c.163.com/public/centos:7.2-tools <!--下載鏡像--> [root@centos01 ~]# docker images <!--查看鏡像--> REPOSITORY TAG IMAGE ID CREATED SIZE hub.c.163.com/public/centos 7.2-tools 4a4618db62b9 3 years ago 515 MB [root@centos01 ~]# docker run -d --net=bridge --name centos7.201 hub.c.163.com/public/centos:7.2-tools <!--配置創建的容器橋接網絡通信,容器訪問互聯網使用--> b308fb5c097fd455073f2f4a280d2660e6943fe1a62d6409e8ebcd3b86469438 [root@centos01 ~]# docker ps <!--查看運行的容器--> CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b308fb5c097f hub.c.163.com/public/centos:7.2-tools "/usr/bin/supervisord" 20 seconds ago Up 19 seconds 22/tcp centos7.201 [root@centos01 ~]# ifconfig <!--查看Docker宿主機IP地址信息--> docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 [root@centos01 ~]# docker exec -it centos7.201 /bin/bash <!--登錄centos7.201容器--> [root@b308fb5c097f /]# ifconfig <!--查看IP地址--> eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0 [root@b308fb5c097f /]# ping www.baidu.com <!--centos7.201容器ping公網測試--> PING www.a.shifen.com (39.156.66.18) 56(84) bytes of data. 64 bytes from 39.156.66.18: icmp_seq=1 ttl=50 time=18.4 ms 64 bytes from 39.156.66.18: icmp_seq=2 ttl=50 time=18.3 ms 64 bytes from 39.156.66.18: icmp_seq=3 ttl=50 time=16.9 ms [root@b308fb5c097f /]# ping 192.168.100.10 <!--ping宿主機IP測試 --> PING 192.168.100.10 (192.168.100.10) 56(84) bytes of data. 64 bytes from 192.168.100.10: icmp_seq=1 ttl=64 time=0.043 ms 64 bytes from 192.168.100.10: icmp_seq=2 ttl=64 time=0.086 ms 64 bytes from 192.168.100.10: icmp_seq=3 ttl=64 time=0.150 ms
4、配置none網絡通信模式
[root@centos01 ~]# docker run -d --net=none --name centos7.202 hub.c.163.com/public/centos:7.2-tools <!--配置docker容器不需要連接到網絡,容器無法通信--> e2c4837d67818e7ef4d7cedf964db21d98cabb594d12091d7f69da4e8fb3f30f [root@centos01 ~]# docker ps <!--查看運行的容器--> CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e2c4837d6781 hub.c.163.com/public/centos:7.2-tools "/usr/bin/supervisord" 57 seconds ago Up 56 seconds centos7.202 b308fb5c097f hub.c.163.com/public/centos:7.2-tools "/usr/bin/supervisord" 7 minutes ago Up 7 minutes 22/tcp centos7.201 [root@centos01 ~]# docker exec -it centos7.202 /bin/bash <!--登錄centos7.202容器--> [root@e2c4837d6781 /]# ifconfig <!--查看IP地址--> lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 [root@e2c4837d6781 /]# ping www.baidu.com <!--ping公網發現是不通的--> ping: unknown host www.baidu.com [root@e2c4837d6781 /]# [root@e2c4837d6781 /]# ping 192.168.100.10 <!--ping宿主機IP地址發現是不通的--> connect: Network is unreachable
5、配置host網絡通信模式
[root@centos01 ~]# docker run -d --net=host --name centos7.203 -v /data1 hub.c.163.com/public/centos:7.2-tools <!--配置運行的容器和宿主機網絡保持同步--> 2911358be486720c4ee93c8de22cd77301236f48c5baf22ea63bb3c54450032e [root@centos01 ~]# ls /var/lib/docker/volumes/ <!--查看創建的數據卷--> dc755f3b6036f167471435629918d06264e1c2c6a8b175426fa80da36143a87e metadata.db [root@centos01 ~]# docker ps <!--查看運行的容器--> CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2911358be486 hub.c.163.com/public/centos:7.2-tools "/usr/bin/supervisord" About a minute ago Up About a minute centos7.203 e2c4837d6781 hub.c.163.com/public/centos:7.2-tools "/usr/bin/supervisord" 15 minutes ago Up 15 minutes centos7.202 b308fb5c097f hub.c.163.com/public/centos:7.2-tools "/usr/bin/supervisord" 21 minutes ago Up 21 minutes 22/tcp centos7.201 [root@centos01 ~]# docker exec -it centos7.203 /bin/bash <!--登錄到centos7.203容器--> [root@centos01 /]# ifconfig <!--查看IP地址--> docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.10 netmask 255.255.255.0 broadcast 192.168.100.255 ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.126 netmask 255.255.255.0 broadcast 192.168.0.255 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 vethc39178a: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::7c4b:a6ff:fe1c:a37f prefixlen 64 scopeid 0x20<link> virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 [root@centos01 ~]# docker exec -it centos7.203 /bin/bash <!--登錄centos7.203容器--> [root@centos01 /]# ping www.baidu.com <!--ping公網測試--> PING www.a.shifen.com (39.156.66.14) 56(84) bytes of data. 64 bytes from 39.156.66.14: icmp_seq=1 ttl=51 time=20.0 ms 64 bytes from 39.156.66.14: icmp_seq=2 ttl=51 time=19.1 ms 64 bytes from 39.156.66.14: icmp_seq=3 ttl=51 time=15.9 ms [root@centos01 /]# ping 192.168.100.10 <!--ping宿主機IP地址測試--> PING 192.168.100.10 (192.168.100.10) 56(84) bytes of data. 64 bytes from 192.168.100.10: icmp_seq=1 ttl=64 time=0.020 ms 64 bytes from 192.168.100.10: icmp_seq=2 ttl=64 time=0.060 ms 64 bytes from 192.168.100.10: icmp_seq=3 ttl=64 time=0.030 ms <!---Centos7.203容器安裝Nginx--> [root@centos01 ~]# cp /mnt/nginx-1.6.0.tar.gz ./ <!--拷貝Nginx壓縮包--> [root@centos01 ~]# ls anaconda-ks.cfg initial-setup-ks.cfg nginx-1.6.0.tar.gz [root@centos01 ~]# cp nginx-1.6.0.tar.gz /var/lib/docker/volumes/dc755f3b6036f167471435629918d06264e1c2c6a8b175426fa80da36143a87e/_data/ <!--將Nginx壓縮包通過數據卷共享到centos7.203容器--> [root@centos01 ~]# docker exec -it centos7.203 /bin/bash <!--登錄到centos7.203容器--> [root@centos01 /]# ls anaconda-post.log bin data1 dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@centos01 /]# cd data1/ <!--查看宿主機共享的數據--> [root@centos01 data1]# ls nginx-1.6.0.tar.gz [root@centos01 /]# yum -y install pcre-devel zlib-devel <!--安裝Nginx依賴程序--> [root@centos01 /]# useradd -M -s /sbin/nologin nginx <!--創建管理Nginx用戶--> [root@centos01 /]# tar zxvf /data1/nginx-1.6.0.tar.gz -C /usr/src/ <!--解壓縮Nginx包--> [root@centos01 /]#yum -y install gcc pcre-devel zlib-devel make <!--先安裝依賴--> [root@centos01 /]# cd /usr/src/nginx-1.6.0/ [root@centos01 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --with-http_stub_status_module && make && make install <!--配置Nginx并 編譯安裝nginx--> [root@centos01 nginx-1.6.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ <!--優化Nginx執行命令--> [root@centos01 nginx-1.6.0]# echo "www.docker.nginx.com" > /usr/local/nginx/html/index.html <!--修改Nginx網站主頁內容--> [root@centos01 nginx-1.6.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ <!--在centos7.203容器中啟動Nginx服務--> [root@centos01 nginx-1.6.0]# netstat -anptu | grep nginx <!--監聽Nginx服務端口號是否正在運行--> tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6268/nginx: master [root@centos01 ~]# curl http://192.168.100.10 <!--docker宿主機訪問centos7.203容器中的nginx--> www.docker.nginx.com [root@centos01 nginx-1.6.0]# cat /usr/local/nginx/logs/access.log <!--查看centos7.203容器中成功訪問Nginx的日志--> 192.168.100.10 - - [12/May/2020:21:42:47 +0800] "GET / HTTP/1.1" 200 21 "-" "curl/7.29.0"
6、配置docker0網卡參數
[root@centos01 ~]# ifconfig <!--查看docker宿主機IP地址--> docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 [root@centos01 ~]# systemctl stop docker <!--停止docker服務--> [root@centos01 ~]# ip link set dev docker0 down <!--停止docker0網橋--> [root@centos01 ~]# brctl delbr docker0 <!--刪除系統默認的docker0網橋--> [root@centos01 ~]# brctl addbr docker0 <!--創建新的網橋,名字是docker0--> [root@centos01 ~]# ip addr add 192.168.20.1/24 dev dokcer0 <!--新的網橋docker0配置IP地址--> [root@centos01 ~]# ip link set dev docker0 up <!--啟動新的docker0網橋--> [root@centos01 ~]# vim /etc/docker/daemon.json <!--修改docker配置文件加載新的網橋docker0--> {"registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com"]} {"bip":"192.168.20.1/24"} <!--加此行--> [root@centos01 ~]# systemctl start docker <!--啟動docker服務--> [root@centos01 ~]# ifconfig <!--查看docker宿主機IP詳細信息--> docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.20.1 netmask 255.255.255.0 broadcast 0.0.0.0 [root@centos01 ~]# docker run -it -d --name centos7.2v1 hub.c.163.com/public/centos:7.2-tools <!--創建一個容器在后臺運行--> d0b5392e60cef37f3c44d79a9fb73916720cfc44faa7b73862bee05fb2d6ce7b [root@centos01 ~]# docker exec -it centos7.2v1 /bin/bash <!--登錄centos7.2v1容器--> [root@d0b5392e60ce /]# ifconfig <!--查看IP地址詳細信息--> eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.20.2 netmask 255.255.255.0 broadcast 0.0.0.0
二、Docker網絡隔離
1、Docker網絡隔離原理
需要管理創建網絡空間名稱;將不同的容器加載到不同的網絡空間名稱中實現隔離;默認不配置網絡隔離默認給容器分配的docker0網絡空間名稱。
2、Docker容器自帶的網絡空間名稱類型
3、配置Docker網絡名稱空間隔離
[root@centos01 ~]# docker network ls <!--查看docker默認的網絡名稱空間--> NETWORK ID NAME DRIVER SCOPE 8bb953004416 bridge bridge local 2c18234cad82 host host local 67860e823c36 none null local [root@centos01 ~]# docker network create -d bridge liyanxin <!--創建網絡名稱空間--> 0c69de4672ec173dc4c60b19e0bf93b361f45a804859f7bc2105d85ca83b1169 [root@centos01 ~]# docker network create -d bridge gongsunli <!--創建網絡名稱空間--> 35687468c9034262173a96e9c23e045cbb8b7ffa6648fc84e015504740815001 [root@centos01 ~]# ifconfig <!--查看docker宿主機網卡信息--> br-0c69de4672ec: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.18.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 br-35687468c903: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.19.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 [root@centos01 ~]# docker run -it -d --name centos6.701 --network=liyanxin hub.c.163.com/public/centos:6.7-tools <!--創建運行的容器添加到liyanxin網絡名稱空間中隔離--> b85a2d8419a98756369ddc3b78247d3d42c178e8e563a936fe973f2f6611f951 [root@centos01 ~]# docker exec -it centos6.701 /bin/bash <!--登錄centos6.701容器--> [root@b85a2d8419a9 /]# ifconfig <!--查看IP地址--> eth0 Link encap:Ethernet HWaddr 02:42:AC:12:00:02 inet addr:172.18.0.2 Bcast:0.0.0.0 Mask:255.255.0.0 [root@centos01 ~]# docker run -it -d --name centos6.702 --network=gongsunli hub.c.163.com/public/centos:6.7-tools <!--創建運行的容器添加到gongsunli網絡名稱空間中隔離--> 9af0fb7b85af3270f3c7c44b62438f436b22289ac0a7604d6ed522604b7b185f [root@centos01 ~]# docker exec -it centos6.702 /bin/bash <!--登錄centos6.702容器--> [root@9af0fb7b85af /]# ifconfig <!--查看IP地址--> eth0 Link encap:Ethernet HWaddr 02:42:AC:13:00:02 inet addr:172.19.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
三、配置網橋實現網絡隔離
1、配置網橋實現網絡隔離的目的
實現Docker宿主機的容器跨Docker宿主機的容器通信使用。
2、配置網橋實現網絡隔離原理
將物理網卡橋接到創建的網橋網卡上;給網橋網卡配置IP地址;創建容器加載網橋網卡實現;docker宿主機容器跨docker宿主機容器通信;管理員管理docker宿主機通過網橋網卡進行遠程管理
3、配置docker網橋實現網絡隔離
[root@centos01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32 <!--修改docker宿主機物理網卡橋接到網橋網卡br0--> TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes NAME=ens32 DEVICE=ens32 ONBOOT=yes BRIDGE=br0 <!--添加此行--> [root@centos01 ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens32 /etc/sysconfig/network-scripts/ifcfg-br0 <!--創建并生成br0網橋--> [root@centos01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-br0 <!--編輯br0網卡配置文件--> TYPE=Bridge <!--修改此行--> PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes NAME=br0 <!--修改名字--> DEVICE=br0 <!--修改名字--> ONBOOT=yes IPADDR=192.168.100.10 <!--添加宿主機IP地址--> NETMASK=255.255.255.0 [root@centos01 ~]# systemctl restart network <!--重新啟動docker宿主機網卡服務--> [root@centos01 ~]# ifconfig <!--查看docker宿主機網卡信息--> br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.10 netmask 255.255.255.0 broadcast 192.168.100.255 br-0c69de4672ec: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.18.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 br-35687468c903: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.19.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:18:d3:26 txqueuelen 1000 (Ethernet) ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::4ad2:dd37:4341:5d8e prefixlen 64 scopeid 0x20<link> lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 veth7b0bb5f: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::ccd3:86ff:fee6:5725 prefixlen 64 scopeid 0x20<link> veth7e0f471: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::684c:fdff:fe13:b436 prefixlen 64 scopeid 0x20<link> virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 [root@centos01 ~]# yum -y install git <!--docker宿主機安裝git--> [root@centos01 ~]# git clone https://github.com/jpetazzo/pipework <!--下載docker容器網絡管理工具pipework--> [root@centos01 ~]# cp pipework/pipework /usr/local/bin/ <!--優化管理命令--> [root@centos01 ~]# chmod +x /usr/local/bin/pipework <!--添加執行權限--> [root@centos01 ~]# docker run -d --name centos6.703 --network=none hub.c.163.com/public/centos:6.7-tools <!--通過鏡像運行容器--> adea0ad48bdde947ec595382d96cba06eb6522ec046e9b3c7bfcb1edb5c84545 [root@centos01 ~]# pipework br0 centos6.703 192.168.100.101/24 <!--給centos6.703容器配置IP地址--> [root@centos01 ~]# docker exec -it centos6.703 /bin/bash <!--登錄centos6.703容器--> [root@adea0ad48bdd /]# ifconfig <!--查看IP地址--> eth2 Link encap:Ethernet HWaddr FA:3A:9D:ED:C0:FF inet addr:192.168.100.101 Bcast:192.168.100.255 Mask:255.255.255.0 [root@adea0ad48bdd /]# ping 192.168.100.10 PING 192.168.100.10 (192.168.100.10) 56(84) bytes of data. 64 bytes from 192.168.100.10: icmp_seq=1 ttl=64 time=0.100 ms 64 bytes from 192.168.100.10: icmp_seq=2 ttl=64 time=0.097 ms 64 bytes from 192.168.100.10: icmp_seq=3 ttl=64 time=0.039 ms
4、配置docker宿主機容器和docker宿主機容器通信
[root@centos02 ~]# ping www.baidu.com <!--再新開一臺服務器,連接公網,安裝docker--> PING www.a.shifen.com (39.156.66.18) 56(84) bytes of data. 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=1 ttl=51 time=19.5 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=2 ttl=51 time=17.3 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=3 ttl=51 time=18.1 ms [root@centos02 ~]# cd /etc/yum.repos.d/ [root@centos02 yum.repos.d]# ls local.repo [root@centos02 yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo <!--下載centos7源--> [root@centos02 ~]# yum install docker -y <!--安裝docker--> [root@centos02 ~]# systemctl start docker <!--啟動docker--> [root@centos02 ~]# systemctl enable docker <!--設置開機自動啟動--> [root@centos02 ~]# docker pull hub.c.163.com/public/centos:6.7-tools <!--下載鏡像--> [root@centos02 ~]# docker images <!--查看鏡像--> REPOSITORY TAG IMAGE ID CREATED SIZE hub.c.163.com/public/centos 6.7-tools b2ab0ed558bb 3 years ago 602 MB [root@centos02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32 <!--修改docker宿主機網卡配置信息橋接到br0網卡 --> TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes NAME=ens32 DEVICE=ens32 ONBOOT=yes BRIDGE=br0 <!--添加此行--> [root@centos02 ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens32 /etc/sysconfig/network-scripts/ifcfg-br0 <!--創建并生成br0網橋--> [root@centos02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-br0 <!--編輯br0網卡配置文件--> TYPE=Bridge <!--修改為Bridge--> PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes NAME=br0 <!--修改名字--> DEVICE=br0 <!--修改為br0--> ONBOOT=yes IPADDR=192.168.100.20 <!--添加宿主機IP地址--> NETMASK=255.255.255.0 [root@centos02 ~]# systemctl restart network <!--重新啟動docker宿主機網卡服務--> [root@centos02 ~]# ifconfig <!--查看docker宿主機網卡信息--> br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.20 netmask 255.255.255.0 broadcast 192.168.100.255 docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:97:5c:9f txqueuelen 1000 (Ethernet) ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.104 netmask 255.255.255.0 broadcast 192.168.0.255 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 [root@centos02 ~]# yum -y install git <!--安裝git--> [root@centos02 ~]# git clone https://github.com/jpetazzo/pipework <!--下載docker容器網絡管理工具pipework--> [root@centos02 ~]# cp pipework/pipework /usr/local/bin/ <!--優化管理命令--> [root@centos02 ~]# chmod +x /usr/local/bin/pipework <!--添加執行權限--> [root@centos02 ~]# docker run -d --name centos6.7 --network=none hub.c.163.com/public/centos:6.7-tools <!--通過進行運行容器--> abec0a6bd3822a2fd702dc44d1cf3043648aadd1a661e577c23701e30ee9df7a [root@centos02 ~]# pipework br0 centos6.7 192.168.100.102/24 <!--給centos6.7容器配置IP地址--> [root@centos02 ~]# docker exec -it centos6.7 /bin/bash <!--登錄centos6.7容器--> [root@abec0a6bd382 /]# ifconfig <!--查看IP地址--> eth2 Link encap:Ethernet HWaddr EE:01:B7:99:90:1C inet addr:192.168.100.102 Bcast:192.168.100.255 Mask:255.255.255.0 [root@abec0a6bd382 /]# ping 192.168.100.101 <!----> PING 192.168.100.101 (192.168.100.101) 56(84) bytes of data. 64 bytes from 192.168.100.101: icmp_seq=1 ttl=64 time=0.660 ms 64 bytes from 192.168.100.101: icmp_seq=2 ttl=64 time=0.865 ms 64 bytes from 192.168.100.101: icmp_seq=3 ttl=64 time=0.382 ms [root@abec0a6bd382 /]# ping 192.168.100.10 <!----> PING 192.168.100.10 (192.168.100.10) 56(84) bytes of data. 64 bytes from 192.168.100.10: icmp_seq=1 ttl=64 time=0.632 ms 64 bytes from 192.168.100.10: icmp_seq=2 ttl=64 time=0.732 ms 64 bytes from 192.168.100.10: icmp_seq=3 ttl=64 time=0.796 ms [root@abec0a6bd382 /]# ping 192.168.100.20 <!----> PING 192.168.100.20 (192.168.100.20) 56(84) bytes of data. 64 bytes from 192.168.100.20: icmp_seq=1 ttl=64 time=0.144 ms 64 bytes from 192.168.100.20: icmp_seq=2 ttl=64 time=0.094 ms 64 bytes from 192.168.100.20: icmp_seq=3 ttl=64 time=0.043 ms
看完這篇關于Docker容器如何實現網絡管理和網絡隔離的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。