IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Docker配置桥接网络

    村里来的扫地僧发表于 2016-03-24 05:00:37
    love 0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    
    安装pipwork
    git clone https://github.com/jpetazzo/pipework
     
    设置主机为桥接状态
    cd /etc/sysconfig/network-scripts/; cp ifcfg-eth0  ifcfg-br0
    vi ifcfg-eth0 //增加BRIDGE=br0,删除IPADDR,NETMASK,GATEWAY,DNS1
    vi ifcfg-br0//修改DEVICE为br0,Type为Bridge,把eth0的网络设置设置到这里来
     
    配置如下:
    [root@localhost ~]# ifconfig
    docker0   Link encap:Ethernet  HWaddr 32:9F:EC:A9:08:EA  
              inet addr:172.17.42.1  Bcast:0.0.0.0  Mask:255.255.0.0
              inet6 addr: fe80::8032:1fff:febc:c437/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:109051 errors:0 dropped:0 overruns:0 frame:0
              TX packets:153465 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:5745966 (5.4 MiB)  TX bytes:497759555 (474.7 MiB)
     
    eth0      Link encap:Ethernet  HWaddr 00:0C:29:84:63:8C  
              inet addr:192.168.32.120  Bcast:192.168.32.255  Mask:255.255.255.0
              inet6 addr: fe80::20c:29ff:fe84:638c/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:597578 errors:0 dropped:0 overruns:0 frame:0
              TX packets:59150 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:275465297 (262.7 MiB)  TX bytes:5089601 (4.8 MiB)
     
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:65536  Metric:1
              RX packets:123811 errors:0 dropped:0 overruns:0 frame:0
              TX packets:123811 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:456345433 (435.2 MiB)  TX bytes:456345433 (435.2 MiB)
     
    vethb911bc4 Link encap:Ethernet  HWaddr D2:F2:E4:07:80:32  
              inet6 addr: fe80::d0f2:e4ff:fe07:8032/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:13 errors:0 dropped:0 overruns:0 frame:0
              TX packets:18 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:979 (979.0 b)  TX bytes:1655 (1.6 KiB)
     
    veth03a7489 Link encap:Ethernet  HWaddr 32:9F:EC:A9:08:EA  
              inet6 addr: fe80::309f:ecff:fea9:8ea/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:9171 errors:0 dropped:0 overruns:0 frame:0
              TX packets:11304 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:619469 (604.9 KiB)  TX bytes:38522521 (36.7 MiB)
     
    veth36c5211 Link encap:Ethernet  HWaddr 3A:4C:A6:F3:99:1B  
              inet6 addr: fe80::384c:a6ff:fef3:991b/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:7 errors:0 dropped:0 overruns:0 frame:0
              TX packets:23 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:558 (558.0 b)  TX bytes:1746 (1.7 KiB)
     
    veth4678dc2 Link encap:Ethernet  HWaddr FE:D3:AA:2F:62:69  
              inet6 addr: fe80::fcd3:aaff:fe2f:6269/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:10 errors:0 dropped:0 overruns:0 frame:0
              TX packets:52 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:696 (696.0 b)  TX bytes:3884 (3.7 KiB)
     
    vethc1cb675 Link encap:Ethernet  HWaddr 3A:67:6F:60:65:56  
              inet6 addr: fe80::3867:6fff:fe60:6556/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:99814 errors:0 dropped:0 overruns:0 frame:0
              TX packets:142231 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:6648110 (6.3 MiB)  TX bytes:459241897 (437.9 MiB)
     
     
    拷贝ifcfg-eth0 至ifcfg-br0
    [root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-br0  
     
    配置ifcfg-eth0
     
    [root@localhost network-scripts]# cat ifcfg-eth0 
    DEVICE=eth0
    TYPE=Ethernet
    UUID=c3e80ed0-5019-48ba-a4cc-f395d342847c
    ONBOOT=yes
    NM_CONTROLLED=yes
    BOOTPROTO=none
    HWADDR=00:0C:29:84:63:8C
    BRIDGE=br0
    [root@localhost network-scripts]# 
     
    配置ifcfg-br0
    [root@localhost network-scripts]# cat ifcfg-br0 
    DEVICE=br0
    TYPE=Bridge
    ONBOOT=yes
    NM_CONTROLLED=yes
    BOOTPROTO=static
    HWADDR=00:0C:29:84:63:8C
    IPADDR=192.168.32.120
    NETMASK=255.255.255.0
    GATEWAY=192.168.32.254
    [root@localhost network-scripts]# 
     
    重启网卡服务
    [root@localhost network-scripts]# /etc/init.d/network restart
     
    安装pipwork
    [root@localhost /]#  git clone https://github.com/jpetazzo/pipework 
     
    [root@localhost /]# cp pipework/pipework /usr/local/bin/
     
    开启一个容器:
    [root@localhost /]# docker run -itd --net=none --name centos-web centos /bin/bash
    f9be7c6c1a52dbb65e22766eeabe64f0012e7281212b4f40b84507c5f2de3136
     
    升级iproute
     
    [root@localhost /]# rpm -Uvh https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm 
    Retrieving https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm
    warning: /var/tmp/rpm-tmp.2Cha4E: Header V4 RSA/SHA1 Signature, key ID d97b3247: NOKEY
    Preparing...                ########################################### [100%]
       1:iproute                ########################################### [100%]
    [root@localhost /]# 
     
     
    配置网络
    [root@localhost /]# pipework br0 f9be7c6c1a52dbb65e22766eeabe64f0012e7281212b4f40b84507c5f2de3136 192.168.32.121/24@192.168.32.254
     
    进入容器
    [root@localhost /]# docker exec -it f9be /bin/bash
     
    查看地址
    [root@f9be7c6c1a52 /]# ifconfig
    eth1      Link encap:Ethernet  HWaddr C6:FE:5E:40:94:4A  
              inet addr:192.168.32.121  Bcast:192.168.32.255  Mask:255.255.255.0
              inet6 addr: fe80::c4fe:5eff:fe40:944a/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:887 errors:0 dropped:0 overruns:0 frame:0
              TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:75567 (73.7 KiB)  TX bytes:510 (510.0 b)
     
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              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:0 
              RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
     
    [root@f9be7c6c1a52 /]# ping www.baidu.com
    PING www.a.shifen.com (115.239.210.27) 56(84) bytes of data.
    64 bytes from 115.239.210.27: icmp_seq=1 ttl=57 time=5.46 ms
    64 bytes from 115.239.210.27: icmp_seq=2 ttl=57 time=2.73 ms
    ^C
    --- www.a.shifen.com ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1089ms
    rtt min/avg/max/mdev = 2.738/4.100/5.462/1.362 ms
    [root@f9be7c6c1a52 /]#


沪ICP备19023445号-2号
友情链接