准备overlay网络实验环境文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
以容器的方式运行consul文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
sudo docker run -d -p 8500:8500 -h consul --name consul progrium/consul -server -bootstrap
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
容器启动后,可以通过来访问consul文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
接下来修改两个主机的docker daemon配置文件,其配置文件的路径如下文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
/ect/stsytmd/system/
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
-H来允许远程主机连接文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
--cluster-store指定consul的地址文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
--cluster-advertise告知consul自己的连接地址文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
修改完配置文件后重启docker daemon文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
sudo systemctl daemon-reload sudo systemctl restart docker.service
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
两个主机将自动注册到consul数据库中文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
创建overlay网络文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
在第一台主机上创建overlay网络overlay_net1文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
-d overlay指定driver为overlay文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
用docker network ls来查看网络文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
overlay_net1的SCOPE为global,而其他网络为local,在第二台主机上查看存在的网络文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
结果表明,在第二台主机上也能看到overlay_net1。这是因为创建overlay_net1时第一台主机将网络信息存入了consul,第二台主机从consul中读取到了新的网络数据,之后overlay_net1的任何变化都会同步到两台主机文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
用docker network inspect overlay_net1来查看创建的overlay的详细信息文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
overlay中运行容器文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
用前面创建的overlay网络创建一个容器,并查看容器的网络配置信息文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
test15容器有两个网络接口eth0和eth1。eth0的IP为,连接的是overlay网络。eth1的IP为,容器默认路由是走eth1。docker会创建一个bridge网络“docker_gwbridge”,为所有连接到overlay网络的容器提供访问外网的能力文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
可以用docker network inspect来查看它的详细信息文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
这样容器就可以通过docker_gwbridge访问外网文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
如果外网要访问容器,可以通过主机端口映射,例如文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
sudo docker run -p 80:80 -d --net overlay_net1 --name web1 httpd
overlay实现跨主机通信文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
前面在一台主机上运行了容器test15,现在在另一台主机上运行容器test16,并查看容器的网络信息文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
test16的IP为,在test16容器里面直接ping容器test15,由结果可以看出这两个容器间可以通信,同时docker也实现了DNS服务文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
可以用以下方法来查看overlay网络的namespace,可以看出两个主机上有相同的namespace文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
overlay的隔离文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
不同的overlay网络是相互隔离的。这里创建第二个overlay网络,并运行容器test17文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
ping失败,可见不同overlay网络之间是隔离的,即便是通过docker_gwbridge也不能通信文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
要实现test15与test17通信,可以将test17也连接到overlay_net1中文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wangluo/11907.html