使用ovs構建docker網絡,實現跨主機通訊與網絡隔離

初始化環境

升級內核:
      rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
      rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
      yum --enablerepo=elrepo-kernel install  kernel-ml-devel kernel-ml
      awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
      grub2-set-default 0
      reboot
      uname -a

安裝docker:
     yum install -y yum-utils
     yum-config-manager     --add-repo     https://download.docker.com/linux/centos/docker-ce.repo
     yum-config-manager --disable docker-ce-edge
     yum makecache fast
     yum install docker-ce
     service docker start


安裝open vswitch:
     yum -y install wget openssl-devel gcc make python-devel openssl-devel kernel-devel graphviz kernel-debug-devel autoconf automake rpm-build redhat-rpm-config libtool python-twisted-core python-zope-interface PyQt4 desktop-file-utils libcap-ng-devel groff checkpolicy selinux-policy-devel
     adduser ovs
     su - ovs
     yum localinstall /home/ovs/rpmbuild/RPMS/x86_64/openvswitch-2.5.0-1.el7.centos.x86_64.rpm -y
     systemctl start openvswitch.service
     systemctl is-active openvswitch
     ovs-vsctl -V
     systemctl enable openvswitch

安裝pipework:
     yum install git
     git clone https://github.com/jpetazzo/pipework
     cp pipework/pipework /bin

一些工具安裝:
yum install bridge-utils # 若是brctl不能用python

yum install net-tools # 若是route命令不能用linux

單節點上使用ovs vlan劃分網絡

啓動四個容器:git

docker run -itd --name con1 ubuntu:14.04 /bin/bash
docker run -itd --name con2 ubuntu:14.04 /bin/bash
docker run -itd --name con3 ubuntu:14.04 /bin/bash
docker run -itd --name con4 ubuntu:14.04 /bin/bash

建立ovs網橋並綁定端口github

pipework ovs0 con1 192.168.0.1/24 @100
pipework ovs0 con2 192.168.0.2/24 @100

pipework ovs0 con3 192.168.0.3/24 @200
pipework ovs0 con4 192.168.0.4/24 @200

這樣con1 和 con2是通的,con3和con4是通的,這個比較簡單。pipework乾的具體的事是:docker

ovs-vsctl add-port ovs0 [容器的虛擬網卡設備] tag=100

ovs劃分vlan處理的原理也很是簡單,包進入到switch時打上tag,發出去時去掉tag,發出去的端口與包的tag不匹配時不處理,這便實現了二層隔離。ubuntu

access端口與trunk端口的區別是,trunk端口可接受多個tag。centos

跨主機vlan

準備兩個主機,在host1上:bash

docker run -itd --name con1 ubuntu:14.04 /bin/bash
docker run -itd --name con2 ubuntu:14.04 /bin/bash
pipework ovs0 con1 192.168.0.1/24 @100
pipework ovs0 con2 192.168.0.2/24 @200

若是是單張網卡的話,把eth0橋接到switch上時會形成網絡中斷,因此如下幾步不要經過ssh操做:
若是非得ssh去操做的話把如下命令放在一條命令中執行(用&&鏈接各個命令)網絡

ovs-vsctl add-port ovs0 eth0
ifconfig ovs0 10.1.86.201 netmask 255.255.255.0   # 這裏地址和掩碼與eth0的配置一致
ifconfig ovs0 up
ifconfig eth0 0.0.0.0
route add default gw 10.1.86.1  # 執行以前看看eth0的gw是什麼,保持一致,這樣eth0就橋接到ovs0上去了。

查看switch端口:ssh

[root@dev-86-204 ~]# ovs-vsctl show
c5ddf9e8-daac-4ed2-80f5-16e6365425fa
    Bridge "ovs0"
        Port "veth1pl41885"
            tag: 100
            Interface "veth1pl41885"
        Port "ovs0"
            Interface "ovs0"
                type: internal
        Port "eth0"
            Interface "eth0"
        Port "veth1pl41805"
            tag: 200
            Interface "veth1pl41805"
    ovs_version: "2.5.1"

在host2上:

docker run -itd --name con3 ubuntu:14.04 /bin/bash
docker run -itd --name con4 ubuntu:14.04 /bin/bash
pipework ovs0 con3 192.168.0.3/24 @100
pipework ovs0 con4 192.168.0.4/24 @200

一樣要橋接eth0到ovs0上,同host1的操做,而後con1與con3可通,con2與con4可通.

GRE實現overlay網絡

linux內核須要3.11以上,本尊在3.10內核上實踐失敗,在虛擬機中升級內核時虛擬機啓動不了,CPU飆到100%,之後再試。

發現把容器直接掛ovs網橋上是能夠通的

gre與下面的vxlan很是相似,只須要在添加往外連的端口時改爲下面命令便可:

ovs-vsctl add-port ovs0 gre0 -- set interface gre0 type=gre options:remote_ip=172.31.244.185

Vxlan實現跨主機通訊

host1:10.1.86.203 
   ovs0
    |
    |-veth1 <-------> eth1 192.168.0.3  con3
    |
    |-vxlan1-------------+
    |                    |
                         |
    host2:10.1.86.204    |
    ovs0                 |
     |                   |
     |-vxlan1------------+
     |
     |-veth1 <--------> eth1 192.168.0.4 con4
     |

能夠看到con3和con4在搭建vxlan以前是沒法通訊的。

在host1上:

[root@dev-86-203 ~]# docker run --name con3 -itd ubuntu:14.04 /bin/bash
[root@dev-86-203 ~]# ovs-vsctl add-br ovs0
[root@dev-86-203 ~]# pipework ovs0 con3 192.168.0.3/24   # 給容器分配地址並掛到ovs0上

[root@dev-86-203 ~]# ovs-vsctl add-port ovs0 vxlan1 -- set interface vxlan1 type=vxlan options:remote_ip=10.1.86.204 options:key=flow # 建立vxlan

[root@dev-86-203 ~]# ovs-vsctl show
5e371797-db70-451c-a0f2-d70c6d00cd05
    Bridge "ovs0"
        Port "veth1pl3342"
            Interface "veth1pl3342"
        Port "ovs0"
            Interface "ovs0"
                type: internal
        Port "vxlan1"
            Interface "vxlan1"
                type: vxlan
                options: {key=flow, remote_ip="10.1.86.204"}
    ovs_version: "2.5.1"

host2上同理:

[root@dev-86-204 ~]# docker run --name con4 -itd ubuntu:14.04 /bin/bash
[root@dev-86-204 ~]# ovs-vsctl add-br ovs0
[root@dev-86-204 ~]# pipework ovs0 con4 192.168.0.4/24   # 給容器分配地址並掛到ovs0上

[root@dev-86-204 ~]# ovs-vsctl add-port ovs0 vxlan1 -- set interface vxlan1 type=vxlan options:remote_ip=10.1.86.203 options:key=flow # 建立vxlan

[root@dev-86-204 ~]# ovs-vsctl show
c5ddf9e8-daac-4ed2-80f5-16e6365425fa
    Bridge "ovs0"
        Port "ovs0"
            Interface "ovs0"
                type: internal
        Port "veth1pl52846"
            Interface "veth1pl52846"
        Port "vxlan1"
            Interface "vxlan1"
                type: vxlan
                options: {key=flow, remote_ip="10.1.86.203"}
    ovs_version: "2.5.1"

驗證:

[root@dev-86-204 ~]# docker exec con4 ping 192.168.0.3  # con4容器中ping con3的地址,可通
PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.251 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.170 ms

更多問題歡迎聯繫我: https://github.com/fanux

相關文章
相關標籤/搜索