Centos 6 Docker配置桥接网络

客官°小女子只卖身不卖艺 2022-07-11 12:12 285阅读 0赞

为了使本地网络中的机器和Docker容器更方便的通信,我们经常会有将Docker容器配置到和主机同一网段的需求。这个需求其实很容易实现,我们只要将Docker容器和宿主机的网卡桥接起来,再给Docker容器配上IP就可以了。

Centos 6按照下面的方法操作(6与7的操作有所不同)

  1. [root@localhost ~]# cd /etc/sysconfig/network-scripts/
  2. [root@localhost network-scripts]# ls
  3. ifcfg-eth0 ifdown-ib ifdown-ppp ifup-aliases ifup-ipv6 ifup-ppp init.ipv6-global
  4. ifcfg-lo ifdown-ippp ifdown-routes ifup-bnep ifup-isdn ifup-routes net.hotplug
  5. ifdown ifdown-ipv6 ifdown-sit ifup-eth ifup-plip ifup-sit network-functions
  6. ifdown-bnep ifdown-isdn ifdown-tunnel ifup-ib ifup-plusb ifup-tunnel network-functions-ipv6
  7. ifdown-eth ifdown-post ifup ifup-ippp ifup-post ifup-wireless
  8. [root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-br0
  9. 1
  10. 2
  11. 3
  12. 4
  13. 5
  14. 6
  15. 7
  16. 8
  17. 9
  18. 1
  19. 2
  20. 3
  21. 4
  22. 5
  23. 6
  24. 7
  25. 8
  26. 9
  27. [root@localhost network-scripts]# vi ifcfg-eth0
  28. DEVICE=eth0
  29. HWADDR=00:0C:29:DB:B2:28
  30. TYPE=Ethernet
  31. UUID=b2268aab-fa2e-49e9-bd67-2572f29e5790
  32. ONBOOT=yes
  33. NM_CONTROLLED=yes
  34. BOOTPROTO=static
  35. IPADDR=192.168.1.179
  36. NETMASK=255.255.255.0
  37. GATEWAY=192.168.1.1
  38. DNS1=114.114.114.114
  39. DNS2=8.8.4.4
  40. 1
  41. 2
  42. 3
  43. 4
  44. 5
  45. 6
  46. 7
  47. 8
  48. 9
  49. 10
  50. 11
  51. 12
  52. 13
  53. 14
  54. 1
  55. 2
  56. 3
  57. 4
  58. 5
  59. 6
  60. 7
  61. 8
  62. 9
  63. 10
  64. 11
  65. 12
  66. 13
  67. 14

增加BRIDGE=br0,删除IPADDR、NETMASK、GATEWAY、DNS

  1. DEVICE=eth0
  2. HWADDR=00:0C:29:DB:B2:28
  3. TYPE=Ethernet
  4. UUID=b2268aab-fa2e-49e9-bd67-2572f29e5790
  5. ONBOOT=yes
  6. BRIDGE=br0
  7. BOOTPROTO=none
  8. #IPADDR=192.168.1.179
  9. #NETMASK=255.255.255.0
  10. #GATEWAY=192.168.1.1
  11. #DNS1=114.114.114.114
  12. #DNS2=8.8.4.4
  13. 1
  14. 2
  15. 3
  16. 4
  17. 5
  18. 6
  19. 7
  20. 8
  21. 9
  22. 10
  23. 11
  24. 12
  25. 13
  26. 1
  27. 2
  28. 3
  29. 4
  30. 5
  31. 6
  32. 7
  33. 8
  34. 9
  35. 10
  36. 11
  37. 12
  38. 13

保存退出

  1. [root@localhost network-scripts]# vi ifcfg-br0
  2. DEVICE=eth0
  3. HWADDR=00:0C:29:DB:B2:28
  4. TYPE=Ethernet
  5. UUID=b2268aab-fa2e-49e9-bd67-2572f29e5790
  6. ONBOOT=yes
  7. NM_CONTROLLED=yes
  8. BOOTPROTO=static
  9. IPADDR=192.168.1.179
  10. NETMASK=255.255.255.0
  11. GATEWAY=192.168.1.1
  12. DNS1=114.114.114.114
  13. DNS2=8.8.4.4
  14. 1
  15. 2
  16. 3
  17. 4
  18. 5
  19. 6
  20. 7
  21. 8
  22. 9
  23. 10
  24. 11
  25. 12
  26. 13
  27. 14
  28. 1
  29. 2
  30. 3
  31. 4
  32. 5
  33. 6
  34. 7
  35. 8
  36. 9
  37. 10
  38. 11
  39. 12
  40. 13
  41. 14

修改DEVICE为br0,Type为Bridge,把eth0的网络设置设置到这里来

  1. DEVICE=br0
  2. TYPE=Bridge
  3. ONBOOT=yes
  4. BOOTPROTO=static
  5. IPADDR=192.168.1.179
  6. NETMASK=255.255.255.0
  7. GATEWAY=192.168.1.1
  8. DNS1=114.114.114.114
  9. DNS2=8.8.4.4
  10. 1
  11. 2
  12. 3
  13. 4
  14. 5
  15. 6
  16. 7
  17. 8
  18. 9
  19. 10
  20. 1
  21. 2
  22. 3
  23. 4
  24. 5
  25. 6
  26. 7
  27. 8
  28. 9
  29. 10

保存退出

  1. [root@localhost network-scripts]# service network restart
  2. 1
  3. 1

这一步我出现了问题,不过重启宿主机后就OK了。

  1. [root@localhost ~]# ifconfig
  2. br0 Link encap:Ethernet HWaddr 00:0C:29:DB:B2:28
  3. inet addr:192.168.1.179 Bcast:192.168.1.255 Mask:255.255.255.0
  4. inet6 addr: fe80::20c:29ff:fedb:b228/64 Scope:Link
  5. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  6. RX packets:137 errors:0 dropped:0 overruns:0 frame:0
  7. TX packets:93 errors:0 dropped:0 overruns:0 carrier:0
  8. collisions:0 txqueuelen:0
  9. RX bytes:13745 (13.4 KiB) TX bytes:11095 (10.8 KiB)
  10. docker0 Link encap:Ethernet HWaddr C6:01:70:AF:C6:E4
  11. inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0
  12. inet6 addr: fe80::c401:70ff:feaf:c6e4/64 Scope:Link
  13. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  14. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  15. TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
  16. collisions:0 txqueuelen:0
  17. RX bytes:0 (0.0 b) TX bytes:468 (468.0 b)
  18. eth0 Link encap:Ethernet HWaddr 00:0C:29:DB:B2:28
  19. inet6 addr: fe80::20c:29ff:fedb:b228/64 Scope:Link
  20. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  21. RX packets:137 errors:0 dropped:0 overruns:0 frame:0
  22. TX packets:101 errors:0 dropped:0 overruns:0 carrier:0
  23. collisions:0 txqueuelen:1000
  24. RX bytes:15663 (15.2 KiB) TX bytes:11959 (11.6 KiB)
  25. lo Link encap:Local Loopback
  26. inet addr:127.0.0.1 Mask:255.0.0.0
  27. inet6 addr: ::1/128 Scope:Host
  28. UP LOOPBACK RUNNING MTU:65536 Metric:1
  29. RX packets:16 errors:0 dropped:0 overruns:0 frame:0
  30. TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
  31. collisions:0 txqueuelen:0
  32. RX bytes:960 (960.0 b) TX bytes:960 (960.0 b)
  33. 1
  34. 2
  35. 3
  36. 4
  37. 5
  38. 6
  39. 7
  40. 8
  41. 9
  42. 10
  43. 11
  44. 12
  45. 13
  46. 14
  47. 15
  48. 16
  49. 17
  50. 18
  51. 19
  52. 20
  53. 21
  54. 22
  55. 23
  56. 24
  57. 25
  58. 26
  59. 27
  60. 28
  61. 29
  62. 30
  63. 31
  64. 32
  65. 33
  66. 34
  67. 35
  68. 36
  69. 1
  70. 2
  71. 3
  72. 4
  73. 5
  74. 6
  75. 7
  76. 8
  77. 9
  78. 10
  79. 11
  80. 12
  81. 13
  82. 14
  83. 15
  84. 16
  85. 17
  86. 18
  87. 19
  88. 20
  89. 21
  90. 22
  91. 23
  92. 24
  93. 25
  94. 26
  95. 27
  96. 28
  97. 29
  98. 30
  99. 31
  100. 32
  101. 33
  102. 34
  103. 35
  104. 36
  105. [root@localhost ~]# git
  106. -bash: git: command not found
  107. [root@localhost ~]# yum install -y git
  108. [root@localhost ~]# cd /etc/sysconfig/network-scripts/
  109. [root@localhost network-scripts]# git clone https://github.com/jpetazzo/pipework
  110. 1
  111. 2
  112. 3
  113. 4
  114. 5
  115. 6
  116. 1
  117. 2
  118. 3
  119. 4
  120. 5
  121. 6

pipework是由Docker的工程师Jérôme Petazzoni开发的一个Docker网络配置工具

  1. [root@localhost network-scripts]# ls
  2. ifcfg-br0 ifdown-eth ifdown-post ifup ifup-ippp ifup-post ifup-wireless pipework
  3. ifcfg-eth0 ifdown-ib ifdown-ppp ifup-aliases ifup-ipv6 ifup-ppp init.ipv6-global
  4. ifcfg-lo ifdown-ippp ifdown-routes ifup-bnep ifup-isdn ifup-routes net.hotplug
  5. ifdown ifdown-ipv6 ifdown-sit ifup-eth ifup-plip ifup-sit network-functions
  6. ifdown-bnep ifdown-isdn ifdown-tunnel ifup-ib ifup-plusb ifup-tunnel network-functions-ipv6
  7. [root@localhost network-scripts]# cp pipework/pipework /usr/local/bin/
  8. [root@localhost network-scripts]# cd
  9. [root@localhost ~]# docker run -itd --net=none --name docker_bridge centos_with_httpd:genesis bash 5417847d3edf9381acf4bcc94c5345002521f4500415d835f081b022f6d48723
  10. [root@localhost ~]# docker ps
  11. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  12. 5417847d3edf centos_with_httpd:genesis "bash" 8 seconds ago Up 6 seconds docker_bridge
  13. [root@localhost ~]# docker exec -it docker_bridge bash
  14. [root@5417847d3edf /]# ifconfig
  15. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  16. inet 127.0.0.1 netmask 255.0.0.0
  17. inet6 ::1 prefixlen 128 scopeid 0x10<host>
  18. loop txqueuelen 0 (Local Loopback)
  19. RX packets 0 bytes 0 (0.0 B)
  20. RX errors 0 dropped 0 overruns 0 frame 0
  21. TX packets 0 bytes 0 (0.0 B)
  22. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  23. [root@5417847d3edf /]# exit
  24. 1
  25. 2
  26. 3
  27. 4
  28. 5
  29. 6
  30. 7
  31. 8
  32. 9
  33. 10
  34. 11
  35. 12
  36. 13
  37. 14
  38. 15
  39. 16
  40. 17
  41. 18
  42. 19
  43. 20
  44. 21
  45. 22
  46. 23
  47. 24
  48. 25
  49. 26
  50. 1
  51. 2
  52. 3
  53. 4
  54. 5
  55. 6
  56. 7
  57. 8
  58. 9
  59. 10
  60. 11
  61. 12
  62. 13
  63. 14
  64. 15
  65. 16
  66. 17
  67. 18
  68. 19
  69. 20
  70. 21
  71. 22
  72. 23
  73. 24
  74. 25
  75. 26
  76. [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
  77. 1
  78. 1

不升级会报错Object “netns” is unknown, try “ip help”

  1. [root@localhost ~]# pipework br0 docker_bridge 192.168.1.187/24
  2. [root@localhost ~]# docker exec -it docker_bridge bash
  3. [root@5417847d3edf /]# ifconfig
  4. eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  5. inet 192.168.1.187 netmask 255.255.255.0 broadcast 192.168.1.255
  6. inet6 fe80::bcf2:9cff:fe1e:4796 prefixlen 64 scopeid 0x20<link>
  7. ether be:f2:9c:1e:47:96 txqueuelen 1000 (Ethernet)
  8. RX packets 7 bytes 528 (528.0 B)
  9. RX errors 0 dropped 0 overruns 0 frame 0
  10. TX packets 7 bytes 510 (510.0 B)
  11. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  12. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  13. inet 127.0.0.1 netmask 255.0.0.0
  14. inet6 ::1 prefixlen 128 scopeid 0x10<host>
  15. loop txqueuelen 0 (Local Loopback)
  16. RX packets 0 bytes 0 (0.0 B)
  17. RX errors 0 dropped 0 overruns 0 frame 0
  18. TX packets 0 bytes 0 (0.0 B)
  19. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  20. 1
  21. 2
  22. 3
  23. 4
  24. 5
  25. 6
  26. 7
  27. 8
  28. 9
  29. 10
  30. 11
  31. 12
  32. 13
  33. 14
  34. 15
  35. 16
  36. 17
  37. 18
  38. 19
  39. 20
  40. 21
  41. 1
  42. 2
  43. 3
  44. 4
  45. 5
  46. 6
  47. 7
  48. 8
  49. 9
  50. 10
  51. 11
  52. 12
  53. 13
  54. 14
  55. 15
  56. 16
  57. 17
  58. 18
  59. 19
  60. 20
  61. 21

ping一下网关

  1. [root@5417847d3edf /]# ping 192.168.1.1
  2. PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
  3. 64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=63.1 ms
  4. 64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=2.12 ms
  5. ^C
  6. --- 192.168.1.1 ping statistics ---
  7. 2 packets transmitted, 2 received, 0% packet loss, time 1001ms
  8. rtt min/avg/max/mdev = 2.125/32.637/63.150/30.513 ms

转自: http://blog.csdn.net/noob_f/article/details/52895026

发表评论

表情:
评论列表 (有 0 条评论,285人围观)

还没有评论,来说两句吧...

相关阅读