Docker implement cross-host container Instance network communication (1)--build multi-host Docker network with Linuxbridge __linux

Source: Internet
Author: User
Tags docker run

Preface

We have mentioned earlier that if we build the Docker cluster, there will be the need for a Docker instance network connection across the host, and in order to save the IP resources of the host network, we try to use DOCKER0 own network connection, add Host network IP to the main node (container instance), This should be a more desirable business requirement.

--------------------------------------------------------------------------------------

Blog:http://blog.csdn.net/chinagissoft

QQ Group: 16403743

Purpose: Focus on the "gis+" cutting-edge technology research and exchange, the cloud computing technology, large data technology, container technology, IoT and GIS in-depth integration, explore the "gis+" technology and industry solutions

Reprint Note: The article is allowed to reprint, but must be linked to the source address, otherwise held legal responsibility!

--------------------------------------------------------------------------------------

In order to make full use of IP resources of DOCKER0 Network Bridge, we specify different--FIXED-CIDR parameters for Docker daemon on each host, and restrict the address of Docker container on different hosts to different network segments.



As shown in the figure above, I have two hosts, including two network card eth0,eth1, my eth1 as a management card, IP as shown above.

By default, each machine will create a DOCKER0 network Bridge, IP is 172.17.0.1, but in order to avoid IP conflict, the first cloud host of the DOCKER0 Network Bridge IP modified to 172.17.0.2, to ensure connectivity between each other.


1. The first host, modify the network information of DOCKER0 Network Bridge

sm@controller:~$ cat/etc/network/interfaces
# This file describes the network interfaces available on your system
  
   # and how to activate them. For more information, interfaces (5).

# The Loopback network interface
Auto lo
iface lo inet loopback

auto Docker0
iface Docker0 inet static
   
    address 172.17.0.1 netmask 255.255.0.0 bridge_ports eth0 bridge_stp off bridge_fd 0 Auto eth1 iface eth1 inet DHCP 
   
  

It is recommended that you reboot the server after the modification is complete

2, for the first host to add the container host IP segment settings, add docker_opts= "--fixed-cidr=172.17.1.1/24"

sm@controller:~$ Cat/etc/default/docker
# docker upstart and sysvinit configuration file

# Customize of Docker binary (especially for development testing).
#DOCKER = "/usr/local/bin/docker"

# Use Docker_opts to modify the daemon startup options.
docker_opts= "--fixed-cidr=172.17.1.1/24"
# If you are need Docker to use a HTTP proxy, it can also be specified here.
  
    #export http_proxy= "http://127.0.0.1:3128/"

# This are also a handy place to tweak where Docker ' s temporary files go.< c8/> #export tmpdir= "/mnt/bigdrive/docker-tmp"
#DOCKER_OPTS = "-b=br-docker"
  

Restart the Docker service after the addition is complete


3, the same method, repeat the above operation in the second machine, pay attention to change IP to 172.17.0.2

In addition, add docker_opts= "--fixed-cidr=172.17.2.1/24" to restart the Docker service


Briefly, we see on the first machine that Docker0 's IP is the 172.17.0.1/16,docker container that gets the IP from Docker0 's network, and on the first machine, the Docker container's IP range is limited to 172.17.1.1/ 24 Network segment.

Similarly, we see on the second machine Docker0 IP is the 172.17.0.2/16,docker container that obtains IP from Docker0 's network, and on the second machine, the Docker container's IP range is limited to 172.17.2.1/24 network segment.


Test host1

View IP to ensure connectivity to Host2 DOCKER0IP

root@controller:~# ifconfig docker0
docker0   Link encap:ethernet  hwaddr 00:0c:29:d3:5a:fe inet addr
          : 172.17.0.1  bcast:172.17.255.255  mask:255.255.0.0
          inet6 addr:fe80::20c:29ff:fed3:5afe/64 scope:link Up
          broadcast RUNNING multicast  mtu:1500  metric:1
          RX packets:85 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:6599 (6.5 KB)  TX bytes:648 (648.0 B)

root@controller:~# Ping 172.17.0.2
ping 172.17.0.2 (172.17.0.2) bytes of data.
Bytes from 172.17.0.2:icmp_seq=1 ttl=64 time=0.561 ms-
bytes from 172.17.0.2:icmp_seq=2 ttl=64 time=0.700 ms

HOST1 Create container instance, IP is 172.17.1.2
root@controller:~# Docker run-it--name test1      ubuntu:14.04/bin/bash root@54fd72ea7832:/# ifconfig eth0 Link encap:ethernet  hwaddr 02:42:ac:11:01:02
          inet addr:172.17.1.2  bcast:0.0.0.0  mask:255.255.0.0
          Inet6 addr:fe80::42:acff:fe11:102/64 Scope:link
          Up broadcast RUNNING multicast  mtu:1500  metric:1
          RX packets:7 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0
          RX bytes:568 (568.0 B) C15/>tx bytes:508 (508.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)

Host2 View DOCKER0 IP information

root@docker2:~# ifconfig docker0
docker0   Link encap:ethernet  hwaddr 00:0c:29:c0:73:8c inet addr
          : 172.17.0.2  bcast:172.17.255.255  mask:255.255.0.0
          inet6 addr:fe80::20c:29ff:fec0:738c/64 scope:link Up
          broadcast RUNNING multicast  mtu:1500  metric:1
          RX packets:3697 errors:0 dropped:0 overruns:0 frame:0
          TX packets:38 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:327320 (327.3 KB)  TX bytes:3420 (3.4 kb)



Host2 creates the container instance and obtains the ip:172.17.2.1

root@docker2:~# Docker run-it--name test2 ubuntu:14.04/bin/bash root@4d049a6397b3:/# ifconfig eth0      Link Encap:ethernet  hwaddr 02:42:ac:11:02:01
          inet addr:172.17.2.1  bcast:0.0.0.0
          Inet6 addr:fe80::42:acff:fe11:201/64 scope:link up
          broadcast RUNNING
          multicast mtu:1500 metric:1 RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0
          dropped:0 overruns:0 collisions:0 txqueuelen:0
          RX bytes:920 (920.0 b)  TX bytes:508 (508.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)

Host2 test the container for connecting host1 172.17.1.2

root@docker2:~# Docker run-it--name test2 ubuntu:14.04/bin/bash root@4d049a6397b3:/# ifconfig eth0 Link encap:ethe Rnet hwaddr 02:42:ac:11:02:01 inet addr:172.17.2.1 bcast:0.0.0.0 the mask:255.255.0.0 inet6: : 42:ACFF:FE11:201/64 scope:link up broadcast RUNNING multicast mtu:1500 metric:1 RX packets:10 Error
          s:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 RX bytes:920 (920.0 b) TX bytes:508 (508.0 b) Lo Link encap:local loopback inet addr:127.0.0.1 mask:25  5.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 RX bytes:0  (0.0 B) TX bytes:0 (0.0 B) root@4d049a6397b3:/# ping 172.17.1.2 ping 172.17.1.2 (172.17.1.2) bytes of data. Bytes from 172.17.1.2:icmp_seq=1 ttl=64 time=0.500 ms-bytes from 172.17.1.2:icmp_seq=2 ttl=64 time=0.669 Ms Byt Es from 172.17.1.2:icmp_seq=3 ttl=64 time=0.599 ms ^c---172.17.1.2 ping statistics---3 packets transmitted, 3 receive
 D, 0% packet loss, time 1998ms rtt Min/avg/max/mdev = 0.500/0.589/0.669/0.072 ms


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.