docker第三章-网络管理

[root@Centos-node4 dockerfile]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
c5091b86a2de        bridge              bridge              local
832b9c357202        host                host                local
20dfc847adb8        none                null                local

第一种网络NULL

只有Lo接口,封闭式容器

[root@Centos-node4 ~]# docker run -it --rm --name t1 --network none  busybox:latest
/ # ifconfig -a
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

第二种网络bridged,跟宿主机docker0相关联

[root@Centos-node4 ~]# docker run -it --rm --name t1 busybox:latest
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1016 (1016.0 B)  TX bytes:0 (0.0 B)
#指定DNS ,DNS搜索域 host
[root@Centos-node4 ~]# docker run -it --rm --name t1 --network none -h t1 --dns 114.114.114.114 --dns-search test  --add-host www.baidu.com:1.1.1.1 busybox:latest
#共享网络空间
[root@Centos-node4 ~]# docker run -it --name b3 --network container:b2 --rm busybox

#修改docker0桥的网络信息
[root@localhost /]# vi /etc/docker/daemon.json
 
{"bip":"10.20.1.1/24",
 "fixed-cidr": "10.20.0.0/16",
 "fixed-cidr_v6": "2001:db8::/64",
 "mtu": 1500,
 "default-gateway": "10.20.1.1",
 "default-gateway-v6": "2001:db8:abcd::89",
 "dns": ["10.20.1.2","10.20.1.3"]
}
#修改监听地址
[root@localhost /]# vi /etc/docker/daemon.json
{"hosts": ["tcp://0.0.0.0:2375"],"unix:///var/run/docker.sock"}
#远程查看主机
docker -H 192.168.10.10:2375 ps
#创建bridge网卡
[root@Centos-node4 ~]# docker network create -d bridge --subnet "192.168.56.0/24" --gateway "192.168.56.1" mybr0
1730edb366f7c72949437cc55b0a470e0be481d09c43f8bc16c85c2caa970d3c
[root@Centos-node4 ~]# docker network ls

第三种 host共享主机网络

[root@Centos-node4 ~]# docker run -it --name b3 --network host --rm busybox

五 扩展 多台物理主机docker容器通信

root@Centos-node2 ~]# docker run -d -p 8500:8500 --name consul progrium/consul -server -bootstrap

#node3 node5主机添加以下配置
[root@Centos-node3 ~]# cat /etc/docker/daemon.json 
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"cluster-store":"consul://192.168.10.138:8500",      #consul地址
"cluster-advertise":"192.168.10.139:2375"             #本机地址

}
复制代码

验证:

 

 

创建自定义全局网络

复制代码
[root@Centos-node3 ~]# docker network create -d overlay ov_net1
1ce3d31db5dc8865bcfb0d8c06c3c9b6c7a62d8518ce7802263e9acad3ab8b80
[root@Centos-node3 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
af13f853071f        bridge              bridge              local
fec447af9823        host                host                local
aec79181307e        my_net              bridge              local
5ad00d340d16        my_net2             bridge              local
8c6172952391        none                null                local
1ce3d31db5dc        ov_net1             overlay             global

#node3创建node4查看
[root@Centos-node4 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
2035b2339f4d        bridge              bridge              local
832b9c357202        host                host                local
20dfc847adb8        none                null                local
1ce3d31db5dc        ov_net1             overlay             global
复制代码

 node3,node4分别启动容器指定全局网络测试互通

复制代码
[root@Centos-node3 ~]# docker run -it --rm --network=ov_net1 busybox
/ # ping 10.0.0.2
PING 10.0.0.2 (10.0.0.2): 56 data bytes
64 bytes from 10.0.0.2: seq=0 ttl=64 time=1.322 ms
64 bytes from 10.0.0.2: seq=1 ttl=64 time=0.395 ms
--- 10.0.0.2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.395/0.858/1.322 ms

[root@Centos-node3 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
af13f853071f        bridge              bridge              local
5fd6e9efce57        docker_gwbridge     bridge              local  #随机 为了让容器与外界通信
fec447af9823        host                host                local
aec79181307e        my_net              bridge              local
5ad00d340d16        my_net2             bridge              local
8c6172952391        none                null                local
1ce3d31db5dc        ov_net1             overlay             global

 

posted @ 2018-09-02 21:32  闫世成  阅读(193)  评论(0编辑  收藏  举报