2 單主機Docker容器VLAN劃分
pipework不僅可以使用Linux bridge連接配接Docker容器,還可以與OpenVswitch結合,實作Docker容器的VLAN劃分。下面,就來簡單示範一下,在單機環境下,如何實作Docker容器間的二層隔離。
為了示範隔離效果,我們将4個容器放在了同一個IP網段中。但實際他們是二層隔離的兩個網絡,有不同的廣播域。
安裝openvswitch
安裝基礎環境
<code>yum </code><code>install</code> <code>gcc </code><code>make</code> <code>python-devel openssl-devel kernel-devel graphviz \</code>
<code>kernel-debug-devel autoconf automake rpm-build redhat-rpm-config \</code>
<code>libtool</code>
下載下傳openvswitch的包
解壓與打包
<code>[root@localhost src]</code><code># tar zxf openvswitch-2.3.1.tar.gz </code>
<code>[root@localhost src]</code><code># cd /root/</code>
<code>[root@localhost ~]</code><code># mkdir -p ~/rpmbuild/SOURCES</code>
<code>[root@localhost src]</code><code># cp openvswitch-2.3.1.tar.gz ~/rpmbuild/SOURCES/</code>
<code>[root@localhost src]</code><code># sed 's/openvswitch-kmod, //g' openvswitch-2.3.1/rhel/openvswitch.spec > openvswitch-2.3.1/rhel/openvswitch_no_kmod.spec</code>
之後會在~/rpmbuild/RPMS/x86_64/裡有2個檔案
<code>[root@localhost src]</code><code># ls ~/rpmbuild/RPMS/x86_64/</code>
<code>openvswitch-2.3.1-1.x86_64.rpm openvswitch-debuginfo-2.3.1-1.x86_64.rpm</code>
安裝第一個就行
<code>[root@localhostsrc]</code><code>#yum localinstall ~/rpmbuild/RPMS/x86_64/openvswitch-2.3.1-1.x86_64.rpm</code>
或
systemctl start openvswitch
啟動
<code>root@localhost src]</code><code># /sbin/chkconfig openvswitch on</code>
<code>[root@localhost src]</code><code># /sbin/service openvswitch start</code>
<code>Starting openvswitch (via systemctl): [ OK ]</code>
<code>檢視狀态</code>
<code>[root@localhost src]</code><code># /sbin/service openvswitch status</code>
<code>ovsdb-server is not running</code>
<code>ovs-vswitchd is not running</code>
[root@localhost src]# systemctl status openvswitch
安裝pipework過程略,參考前面的操作
建立交換機,把實體網卡加入ovs1
<code>[root@localhost ~]</code><code># ovs-vsctl add-br ovs1</code>
<code>[root@localhost ~]</code><code># ovs-vsctl add-port ovs1 eno16777736</code>
<code>[root@localhost ~]</code><code># ip link set ovs1 up</code>
<code>[root@localhost ~]</code><code># ifconfig eno16777736</code>
Ifconfig 檢視
<a href="https://s5.51cto.com/wyfs02/M02/03/CC/wKiom1mgOBbxqwEvAABqsP2gmLQ852.png-wh_500x0-wm_3-wmp_4-s_1355321913.png" target="_blank"></a>
在主機A上建立4個Docker容器,test1、test2、test3、test4
<code>[root@localhost ~]</code><code># docker run -itd --name test1 centos7:http /bin/bash</code>
<code>0ef176613e34b045112f84f860643a071b903c10d23ffc1b1a233ff1367bc312</code>
<code>[root@localhost ~]</code><code># docker run -itd --name test2 centos7:http /bin/bash</code>
<code>cf99eb3b2558d87226f5624f2cb3144e5e34425b73f3da15a1ea5b86027dbb5d</code>
<code>[root@localhost ~]</code><code># docker run -itd --name test3 centos7:http /bin/bash</code>
<code>3abd9472a1075241d900c9d2e4ed6b6860e2e140efb104036ae7cfbbd27fd570</code>
<code>[root@localhost ~]</code><code># docker run -itd --name test4 centos7:http /bin/bash</code>
<code>09a73f1a6284e4380c1b4e271756a422caa10f384fd6a532d8522e7e9de5ca37</code>
将test1,test2劃分到一個vlan中,vlan在mac位址後加@指定,此處mac位址省略。
[root@localhost ~]# pipework ovs1 test1 192.168.1.1/24@100
[root@localhost ~]# pipework ovs1 test2 192.168.1.2/24@100
[root@localhost ~]# pipework ovs1 test3 192.168.1.3/24@200
[root@localhost ~]# pipework ovs1 test4 192.168.1.4/24@200
[root@0ef176613e34 /]# ping 192.168.1.3
測試ping
PING 192.168.1.3 (192.168.1.3) 56(84) bytes of data.
64 bytes from 192.168.1.3: icmp_seq=1 ttl=64 time=0.564 ms
64 bytes from 192.168.1.3: icmp_seq=2 ttl=64 time=0.053 ms
完成上述操作後,使用docker attach連到容器中,然後用ping指令測試連通性,發現test1和test2可以互相通信,但與test3和test4隔離。這樣,一個簡單的VLAN隔離容器網絡就已經完成。
由于OpenVswitch本身支援VLAN功能,是以這裡pipework所做的工作和之前介紹的基本一樣,隻不過将Linux bridge替換成了OpenVswitch,在将veth pair的一端加入ovs0網橋時,指定了tag。底層操作如下:
ovs-vsctl add-port ovs0 veth* tag=100
本文轉自柴鑫旺 51CTO部落格,原文連結:http://blog.51cto.com/chaixinwang/1959620,如需轉載請自行聯系原作者