本文共 3086 字,大约阅读时间需要 10 分钟。
2 单主机Docker容器VLAN划分
pipework不仅可以使用Linux bridge连接Docker容器,还可以与OpenVswitch结合,实现Docker容器的VLAN划分。下面,就来简单演示一下,在单机环境下,如何实现Docker容器间的二层隔离。
为了演示隔离效果,我们将4个容器放在了同一个IP网段中。但实际他们是二层隔离的两个网络,有不同的广播域。
安装openvswitch
安装基础环境
1 2 3 | yum install gcc make python-devel openssl-devel kernel-devel graphviz \ kernel-debug-devel autoconf automake rpm-build redhat-rpm-config \ libtool |
下载openvswitch的包
wget
解压与打包
1 2 3 4 5 | [root@localhost src] # tar zxf openvswitch-2.3.1.tar.gz [root@localhost src] # cd /root/ [root@localhost ~] # mkdir -p ~/rpmbuild/SOURCES [root@localhost src] # cp openvswitch-2.3.1.tar.gz ~/rpmbuild/SOURCES/ [root@localhost src] # sed 's/openvswitch-kmod, //g' openvswitch-2.3.1/rhel/openvswitch.spec > openvswitch-2.3.1/rhel/openvswitch_no_kmod.spec |
之后会在~/rpmbuild/RPMS/x86_64/里有2个文件
1 2 | [root@localhost src] # ls ~/rpmbuild/RPMS/x86_64/ openvswitch-2.3.1-1.x86_64.rpm openvswitch-debuginfo-2.3.1-1.x86_64.rpm |
安装第一个就行
1 | [root@localhostsrc] #yum localinstall ~/rpmbuild/RPMS/x86_64/openvswitch-2.3.1-1.x86_64.rpm |
或
systemctl start openvswitch
启动
1 2 3 4 | root@localhost src] # /sbin/chkconfig openvswitch on [root@localhost src] # /sbin/service openvswitch start Starting openvswitch (via systemctl): [ OK ] 查看状态 |
1 2 3 | [root@localhost src] # /sbin/service openvswitch status ovsdb-server is not running ovs-vswitchd is not running |
或
[root@localhost src]# systemctl status openvswitch
安装pipework过程略,参考前面的操作
创建交换机,把物理网卡加入ovs1
1 2 3 4 | [root@localhost ~] # ovs-vsctl add-br ovs1 [root@localhost ~] # ovs-vsctl add-port ovs1 eno16777736 [root@localhost ~] # ip link set ovs1 up [root@localhost ~] # ifconfig eno16777736 |
Ifconfig 查看
在主机A上创建4个Docker容器,test1、test2、test3、test4
1 2 3 4 5 6 7 8 | [root@localhost ~] # docker run -itd --name test1 centos7:http /bin/bash 0ef176613e34b045112f84f860643a071b903c10d23ffc1b1a233ff1367bc312 [root@localhost ~] # docker run -itd --name test2 centos7:http /bin/bash cf99eb3b2558d87226f5624f2cb3144e5e34425b73f3da15a1ea5b86027dbb5d [root@localhost ~] # docker run -itd --name test3 centos7:http /bin/bash 3abd9472a1075241d900c9d2e4ed6b6860e2e140efb104036ae7cfbbd27fd570 [root@localhost ~] # docker run -itd --name test4 centos7:http /bin/bash 09a73f1a6284e4380c1b4e271756a422caa10f384fd6a532d8522e7e9de5ca37 |
将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
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