Docker跨主机容器通信overlay的实现方法

发布时间:2025-01-24 点击:70
服务器
这篇文章主要讲解了docker跨主机容器通信overlay的实现方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
同样是两台服务器:
准备工作: 
设置容器的主机名
consul:kv类型的存储数据库(key:value)docker01、02上:vim /etc/docker/daemon.json{ "hosts":["tcp://0.0.0.0:2376","unix:///var/run/docker.sock"], 这行改了要修改自己的docker配置文件 "cluster-store": "consul://10.0.0.11:8500", "cluster-advertise": "10.0.0.11:2376" 另外一台写自己的ip即可}vim /etc/docker/daemon.jsonvim /usr/lib/systemd/system/docker.serviceexecstart=/usr/bin/dockerd --containerd=/run/containerd/containerd.sock 这行改成这样,两台的都要改systemctl daemon-reloadsystemctl restart docker docker01上:
docker run -d -p 8500:8500 -h consul --name consul progrium/consul -server -bootstrap 这条命令在设置容器的主机名之后做[root@docker1 centos_zabbix]# docker imagesrepository tag image id created size10.0.0.11:5000/kode v2 6914fda5fd44 4 hours ago 502mbkode v2 6914fda5fd44 4 hours ago 502mb10.0.0.11:5000/centos6.9_nginx_ssh v3 92b1087df3f5 13 hours ago 431mbcentos6.9_nginx_ssh v3 92b1087df3f5 13 hours ago 431mbcentos6.9_nginx_ssh v2 6307e23ee16d 13 hours ago 431mb<none> <none> b94c822488f9 13 hours ago 431mbcentos6.9_nginx_ssh v1 f01eeebf918a 13 hours ago 431mb<none> <none> 54167f1d659b 13 hours ago 431mb<none> <none> 71474e616504 13 hours ago 430mbcentos6.9_ssh v2 03387f99f022 14 hours ago 307mb<none> <none> 82d0e9ce844b 14 hours ago 195mbkode v1 c0f486ac58fe 15 hours ago 536mbprogrium/consul latest 09ea64205e55 4 years ago 69.4mb 这里有了[root@docker1 centos_zabbix]# docker ps -acontainer id image command created status ports names1016753ee64d progrium/consul "/bin/start -server …" 4 minutes ago exited (255) 3 minutes ago 53/tcp, 53/udp, 8300-8302/tcp, 8400/tcp, 8301-8302/udp, 0.0.0.0:8500->8500/tcp consul最后在页面上访问:
2)创建overlay网络
docker network create -d overlay –subnet 172.16.1.0/24 –gateway 172.16.1.254 ol1 这个网段不能和宿主机上相同,并且随便在一台上执行这个命令即可
查看容器的网络,有没有出现overlay
[root@docker1 shell]# docker network lsnetwork id name driver scopee480a4fe5f4b bridge bridge local0a3cbfe2473f host host local4e5ccdc657e1 macvlan_1 macvlan localaab77f02a0b1 none null local8ae111afded7 ol1 overlay global3)启动容器测试
一台上:docker run -it --network ol1 --name test01 centos6.9_ssh:v2 /bin/bash另外一台上:docker run -it --network ol1 --name test02 centos6.9_ssh:v2 /bin/bash 之后可以通过test01和test02之间联通之后可以通过test01和test02之间联通
真实原理:
每个容器有两块网卡,eth0实现容器间的通讯,eth2实现容器访问外网 
看完上述内容,是不是对docker跨主机容器通信overlay的实现方法有进一步的了解,如果还想学习更多内容,欢迎关注资讯频道。

正规的云服务器产品怎么续费
腾讯云服务器续费学生
网站域名是什么东西?如何选择网站域名?
阿里云服务器购买时考虑的因素
为什么要租用服务器
查询域名丰寸公司还未实名
怎么测试ping服务器
阿里云服务器低价活动