Docker container interconnection (Openvswitch) in different physical machines
This article uses vultr virtual host to achieve
Invitation registration link: http://www.vultr.com/?ref=6940267
Reference: http://www.cnblogs.com/openxxs/p/4690478.html
Buy two consoles first, very cheap, the one for $5 a month is enough
Install docker and related software
yum upgrade -y
yum install -y docker tree git wget p7zip bridge-utils net-tools vim unzip
systemctl start docker.service
systemctl enable docker
Install openvswitch 2.5.0
For specific installation methods, please refer to: http://crabdave.iteye.com/blog/2363896
Download the openvswitch in the attachment directly
wget http://dl2.iteye.com/upload/attachment/0123/7844/8da95736-54ae-3fc7-938c-b990af0b24c9.zip
unzip 8da95736-54ae-3fc7-938c-b990af0b24c9.zip
Install locally and start the service
yum localinstall -y openvswitch-2.5.0-1.x86_64.rpm
systemctl start openvswitch.service
Enable ip_forward on both hosts
echo net.ipv4.ip_forward=1 >> /etc/sysctl.conf
systemctl restart network
sysctl net.ipv4.ip_forward
Configure the intranet IP on the cloud host
(I have been unable to connect to ip route add RTNETLINK answers: Network is unreachable before using the external network IP)
Refer to Sample Network Configuration on www.vultr.com page
Example:
/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=IP address
NETMASK=255.255.254.0
GATEWAY=Gateway
DNS1=
IPV6INIT=yes
IPV6ADDR="2001:19f0:4400:4827:5400:00ff:fe5d:302c/64"
IPV6_AUTOCONF="yes"
DNS2=2001:19f0:300:1704::6
/etc/sysconfig/network-scripts/route-eth0
169.254.0.0/16 dev eth0
Configure the internal network card
/etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.99.0.11 (intranet IP)
NETMASK=255.255.0.0
MTU = 1450
First configure the first machine 10.99.0.11:
If you installed it wrong before, you can delete the previously bound port through the ovs-vsctl del-port ob0 gre0 command
ovs-vsctl creates the ob0 bridge and binds the IP:
ovs-vsctl add-br ob0
ovs-vsctl add-port ob0 gre0 -- set Interface gre0 type=gre options:remote_ip=10.99.0.11
brctl addbr kbr0
brctl addif kbr0 ob0
ovs-vsctl show
Delete the docker default bridge docker0
ip link set dev docker0 down
ip link del dev docker0
vi /etc/sysconfig/network-scripts/ifcfg-kbr0
DEVICE=kbr0
ONBOOT=yes
BOOTPROTO=static
IPADDR=172.17.2.1
NETMASK=255.255.255.0
GATEWAY=172.17.2.0
USERCTL=no
TYPE=Bridge
IPV6INIT=no
Use the intranet network card to connect to eth1 and configure the other party's IP
echo "172.17.1.0/24 via 10.99.0.10 dev eth1" > /etc/sysconfig/network-scripts/route-eth1
After the configuration is complete, restart the network service:
systemctl restart network.service
View route:
ip route show|column -t
172.17.1.0/24 via 10.99.0.10 dev eth1
172.17.2.0/24 dev kbr0 proto kernel scope link src 172.17.2.1
Then configure the second machine 10.99.0.10:
ovs-vsctl add-br ob0
ovs-vsctl add-port ob0 gre0 -- set Interface gre0 type=gre options:remote_ip=10.99.0.10
brctl addbr kbr0
brctl addif kbr0 ob0
ovs-vsctl show
Delete the docker default bridge docker0
ip link set dev docker0 down
ip link del dev docker0
vi /etc/sysconfig/network-scripts/ifcfg-kbr0
DEVICE=kbr0
ONBOOT=yes
BOOTPROTO=static
IPADDR=172.17.1.1
NETMASK=255.255.255.0
GATEWAY=172.17.1.0
USERCTL=no
TYPE=Bridge
IPV6INIT=no
Use the intranet network card to connect to eth1 and configure the other party's IP
echo "172.17.2.0/24 via 10.99.0.11 dev eth1" > /etc/sysconfig/network-scripts/route-eth1
After the configuration is complete, restart the network service:
systemctl restart network.service
View route:
ip route show|column -t
172.17.1.0/24 dev kbr0 proto kernel scope link src 172.17.1.1
172.17.2.0/24 via 10.99.0.11 dev eth1
Configure docker for two machines
vim /etc/sysconfig/docker
Add new bridge in OPTIONS= -b=kbr0
restart docker service
systemctl restart docker.service
Write a Dockerfile that installs only one net-tools
vi Dockerfile
From centos
RUN yum -y install net-tools
build image
docker build -t test .
run the container
docker run -it --rm=true test
Then check the IP
ifconfig
ping peer IP
ping 172.17.2.2
PING 172.17.2.2 (172.17.2.2) 56(84) bytes of data.
64 bytes from 172.17.2.2: icmp_seq=1 ttl=62 time=1.18 ms
64 bytes from 172.17.2.2: icmp_seq=2 ttl=62 time=0.804 ms
ping 172.17.1.2
PING 172.17.1.2 (172.17.1.2) 56(84) bytes of data.
64 bytes from 172.17.1.2: icmp_seq=1 ttl=62 time=0.904 ms
64 bytes from 172.17.1.2: icmp_seq=2 ttl=62 time=0.802 ms
If the two sides can spell it, it will be a success!
If you don't want to build the image file, you can also use the following command to install net-tools immediately after creating the centos container
docker run -it --rm=true centos sh -c "yum -y install net-tools; bash"