关闭

create own virtual bridge 与本机物理接口通信

标签: docker linux ubuntu
134人阅读 评论(0) 收藏 举报
分类:

适用于centos环境:

脚本:   sh  startbr.sh

#! /bin/sh

 

IP_ADDR=`/sbin/ifconfigeth1 | grep "inet addr" | tr -s ' ' | cut -d ' ' -f3 | cut -d":" -f2`    //获取Ip address,设置变量方便下文调用

 

NETMASK=`/sbin/ifconfigeth1 | grep "inet addr" | tr -s ' ' | cut -d ":" -f4`

 

/usr/sbin/brctl addbr br0                   //添加bridgebr0

/usr/sbin/brctl addif br0eth1              //添加以太网网物理接口,附加到网桥接口br0

ifconfig eth1 0.0.0.0promisc            //设置网卡为混杂模式

ifconfig br0 ${IP_ADDR}netmask ${NETMASK} up            // 启用网桥

 

适用于ubuntu环境:

root@ubuntu-daisy:~/bamboo#cat startbr.sh

#! /bin/sh

 

IP_ADDR=`/sbin/ifconfigenp3s0f3 | grep "inet addr" | tr -s ' ' | cut -d ' ' -f3 | cut -d":" -f2`

NETMASK=`/sbin/ifconfigenp3s0f3 | grep "inet addr" | tr -s ' ' | cut -d ":" -f4`

 

/sbin/brctladdbr bri0

/sbin/brctl addif bri0enp3s0f3

ifconfig enp3s0f30.0.0.0 promisc

ifconfig bri0${IP_ADDR} netmask ${NETMASK} up

 

root@ubuntu-daisy:~# ifconfig docker0

docker0   Linkencap:Ethernet  HWaddr 02:42:30:37:7c:78

          inetaddr:172.18.0.1  Bcast:0.0.0.0  Mask:255.255.0.0

          inet6 addr:fe80::42:30ff:fe37:7c78/64 Scope:Link

          UP BROADCASTMULTICAST  MTU:1500  Metric:1

          RXpackets:230265 errors:0 dropped:0 overruns:0 frame:0

          TXpackets:292164 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0txqueuelen:0

          RXbytes:12271942 (12.2 MB)  TXbytes:559848432 (559.8 MB)

root@ubuntu-daisy:~# /sbin/ifconfig docker0 | grep"inet addr" | tr -s ' ' | cut -d ' ' -f3 | cut -d ":" -f2

172.18.0.1

root@ubuntu-daisy:~# which ifconfig

/sbin/ifconfig

root@ubuntu-daisy:~# myip=`/sbin/ifconfig docker0 | grep"inet addr" | tr -s ' ' | cut -d ' ' -f3 | cut -d ":" -f2`

root@ubuntu-daisy:~# echo $myip

172.18.0.1

建立通信步骤:

第一步:在本机上sh startbr.sh ,此时本机物理接口已经与建立的虚拟网桥接上,本机通信断掉,需要从其他机器登录跳转到本机进行下面操作

第二步:建立container,设定IP

第三步:添加网关,允许外网访问本机内部container IP

For example

root@ubuntu-daisy:~/bamboo# ls

master.zip mydockerbuild pipework-master  startbr.sh  web

root@ubuntu-daisy:~/bamboo#

root@ubuntu-daisy:~/bamboo#

root@ubuntu-daisy:~/bamboo# cat startbr.sh

#! /bin/sh

 

IP_ADDR=`/sbin/ifconfig enp3s0f3 | grep "inetaddr" | tr -s ' ' | cut -d ' ' -f3 | cut -d ":" -f2`

NETMASK=`/sbin/ifconfig enp3s0f3 | grep "inetaddr" | tr -s ' ' | cut -d ":" -f4`

 

/sbin/brctl addbr bri0

/sbin/brctl addif bri0 enp3s0f3

ifconfig enp3s0f3 0.0.0.0 promisc

ifconfig bri0 ${IP_ADDR} netmask ${NETMASK} up

root@ubuntu-daisy:~/bamboo#

root@ubuntu-daisy:~/bamboo# sh startbr.sh

登录其他host:10.239.128.245

  ssh root@10.239.129.89

建立container前检查所设IP可以使用

root@ubuntu-daisy:~# ping 10.239.129.14

PING 10.239.129.14 (10.239.129.14) 56(84) bytes of data.

64 bytes from 10.239.129.14: icmp_seq=1 ttl=64 time=0.317 ms

64 bytes from 10.239.129.14: icmp_seq=2 ttl=64 time=0.183 ms

64 bytes from 10.239.129.14: icmp_seq=3 ttl=64 time=0.183 ms

^C

--- 10.239.129.14 ping statistics ---

3 packets transmitted, 3 received, 0% packet loss, time2000ms

rtt min/avg/max/mdev = 0.183/0.227/0.317/0.065 ms

root@ubuntu-daisy:~#  pipework bri0$(docker run -d -it --net=none  --nametestduliip001  busybox ) 10.239.129.14/23@10.239.128.1

root@ubuntu-daisy:~# ping 10.239.128.12

PING 10.239.128.12 (10.239.128.12) 56(84) bytes of data.

64 bytes from 10.239.128.12: icmp_seq=1 ttl=64 time=0.430 ms

64 bytes from 10.239.128.12: icmp_seq=2 ttl=64 time=0.146 ms

64 bytes from 10.239.128.12: icmp_seq=3 ttl=64 time=0.172 ms

64 bytes from 10.239.128.12: icmp_seq=4 ttl=64 time=0.126 ms

^C

--- 10.239.128.12 ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time2999ms

rtt min/avg/max/mdev = 0.126/0.218/0.430/0.124 ms

root@ubuntu-daisy:~#  pipework bri0$(docker run -d -it --net=none  --nametestduliip002  busybox )10.239.128.12/23@10.239.128.1

Warning: arping not found; interface may not be immediatelyreachable

root@ubuntu-daisy:~# brctl show

bridge name     bridgeid               STP enabled     interfaces

bri0           8000.001e67e6c02a       no              enp3s0f3

                                                       veth1pl6754

                                                        veth1pl6933

docker0        8000.02420270a92d       no

virbr0         8000.525400f5b743       yes             virbr0-nic

建立网关,开始ping

root@ubuntu-daisy:~# route add default gw 10.239.128.1

root@ubuntu-daisy:~# ping 10.239.128.12

PING 10.239.128.12 (10.239.128.12) 56(84) bytes of data.

64 bytes from 10.239.128.12: icmp_seq=1 ttl=64 time=0.189 ms

64 bytes from 10.239.128.12: icmp_seq=2 ttl=64 time=0.116 ms

64 bytes from 10.239.128.12: icmp_seq=3 ttl=64 time=0.165 ms

64 bytes from 10.239.128.12: icmp_seq=4 ttl=64 time=0.125 ms

^C

--- 10.239.128.12 ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time2997ms

rtt min/avg/max/mdev = 0.116/0.148/0.189/0.033 ms

ping成功,外网可以访问虚拟网桥连接的container,实现container与外部通信。

 

###/etc/default/docker .io     create own virtual bridge

###将#DOCKER_OPTS="--dns 8.8.8.8 --dns8.8.4.4" 改成 #DOCKER_OPTS=”-b=bridgeName”

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8334次
    • 积分:390
    • 等级:
    • 排名:千里之外
    • 原创:32篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类