如果要在生产和测试环境大规模采用docker技术,首先就需要解决不同物理机建的docker容器互联问题。本文介绍在centos7环境下采用open vswitch实现不同物理服务器上的docker容器互联的配置。
环境介绍:
Server1: 192.168.115.5/24
Server2: 192.168.115.6/24
一:在Server1和Server2上分别用rpm方式安装docker并启动服务
# yum list *docker*
# yum -y install docker
# service docker start
Redirecting to /bin/systemctl start docker.service
二: 在Server1和Server2上分别安装open vswitch
# yum -y install wget openssl-devel kernel-devel
# yum groupinstall "Development Tools"
$ wget http://openvswitch.org/releases/openvswitch-2.3.0.tar.gz
$ tar -zxvpf openvswitch-2.3.0.tar.gz
$ mkdir -p ~/rpmbuild/SOURCES
$ sed 's/openvswitch-kmod, //g' openvswitch-2.3.0/rhel/openvswitch.spec > openvswitch-2.3.0/rhel/openvswitch_no_kmod.spec
$ cp openvswitch-2.3.0.tar.gz rpmbuild/SOURCES/
$ rpmbuild -bb --without check ~/openvswitch-2.3.0/rhel/openvswitch_no_kmod.spec
$ exit
# yum localinstall ~/rpmbuild/RPMS/x86_64/openvswitch-2.3.0-1.x86_64.rpm
# mkdir /etc/openvswitch
# setenforce 0
# systemctl start openvswitch.service
# systemctl status openvswitch.service -l
三:在Server1和Server2上建立OVS Bridge并配置路由
修改文件 /etc/docker/daemon.json 添加内容 "bip": "ip/netmask" [ 切勿与宿主机同网段 ] 为172.17.1.1/24
重启docker服务
Server1: docker容器内网ip网段172.17.1.0/24
# cat /proc/sys/net/ipv4/ip_forward
1
# ovs-vsctl add-br obr0
# ovs-vsctl add-port obr0 gre0 -- set Interface gre0 type=gre options:remote_ip=192.168.115.5
# brctl addbr kbr0
# brctl addif kbr0 obr0
# 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
# cat /etc/sysconfig/network-scripts/route-ens32
172.17.2.0/24 via 192.168.115.6 dev ens32
# systemctl restart network.service
修改文件 /etc/docker/daemon.json 添加内容 "bip": "ip/netmask" [ 切勿与宿主机同网段 ] 为172.17.2.1/24
重启docker服务
Server2: docker容器内网ip网段172.17.2.0/24
# cat /proc/sys/net/ipv4/ip_forward
1
# ovs-vsctl add-br obr0
# ovs-vsctl add-port obr0 gre0 -- set Interface gre0 type=gre options:remote_ip=192.168.115.6
# brctl addbr kbr0
# brctl addif kbr0 obr0
# 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
# cat /etc/sysconfig/network-scripts/route-ens32
172.17.1.0/24 via 192.168.115.5 dev ens32
# systemctl restart network.service
四:启动容器测试
Server1和Server2上修改docker启动的虚拟网卡绑定为kbr0,重启docker进程
Server1和Server2上修改docker启动的虚拟网卡绑定为kbr0,重启docker进程
Server2:
# docker run -idt --name test2 daocloud.io/centos /bin/bash
如果ping不同查看宿主机的路由情况route –n是否有对端主机的路由,如果没有手动添加route add –net 172.17.2.0/24 gw 192.168.115.6 即可
本文转自 转身撞墙角 51CTO博客,原文链接:http://blog.51cto.com/chentianwang/1754374