ITPub博客

首页 > 云计算 > > docker 跨主机互联

docker 跨主机互联

作者:浪花小雨滴 时间:2020-05-09 11:48:11 0 删除 编辑

docker容器使用weave实现跨主机互联


主机A:192.168.1.9


容器1:192.168.2.1

容器2:192.168.3.1

···


主机B:192.168.1.10


容器1:192.168.2.2

···

实现主机A中容器1与主机B中容器1的网络互联

主机A步骤:

①下载复制weave二进制执行文件(需要internet)

[root@192 ~]#git clone github.com/weaveworks/w #如没有git,yum install git

[root@192 ~]#cp -p /root/weave/weave /usr/local/bin/

[root@192 ~]#weave version

weave version会检测weave运行容器状态,所以会先从注册服务器下载weaveexec容器

②在第一次weave launch会报各种错误,根据提示先排除错误在进行启动。

[root@192 ~]# iptables -L FORWARD --line-numbers

找到规则REJECT all anywhere anywhere reject-with icmp-host-prohibited的编号将它删除

[root@192 ~]# iptables -D FORWARD 19

打开本地tcp和udp对weave的端口

[root@192 ~]# iptables -I INPUT -m state --state NEW,ESTABLISHED -p tcp --dport 6783 -j ACCEPT

[root@192 ~]# iptables -I INPUT -m state --state NEW,ESTABLISHED -p udp --dport 6783 -j ACCEPT

weave launch会自动进行tls检测,把它关掉启动

[root@192 ~]# weave launch --no-detect-tls

等待下载weave剩下的支持容器

③启动docker容器,使用weave网桥给容器分配沟通地址

[root@192 ~]# docker run -dit -P centos/httpd:v1

[root@192 ~]# weave attach 192.168.2.1/24 容器ID

192.168.2.1

主机B步骤:

①下载复制weave二进制执行文件(需要internet)

[root@192 ~]#git clone github.com/weaveworks/w

[root@192 ~]#cp -p /root/weave/weave /usr/local/bin/

[root@192 ~]#weave version

weave version会检测weave运行容器状态,所以会先从注册服务器下载weaveexec容器

②在第一次weave launch会报各种错误,根据提示先排除错误在进行启动。

[root@192 ~]# iptables -L FORWARD --line-numbers

找到规则REJECT all anywhere anywhere reject-with icmp-host-prohibited的编号将它删除

[root@192 ~]# iptables -D FORWARD 19

打开本地tcp和udp对weave的端口

[root@192 ~]# iptables -I INPUT -m state --state NEW,ESTABLISHED -p tcp --dport 6783 -j ACCEPT

[root@192 ~]# iptables -I INPUT -m state --state NEW,ESTABLISHED -p udp --dport 6783 -j ACCEPT

weave launch会自动进行tls检测,把它关掉启动

[root@192 ~]# weave launch 192.168.1.9 --no-detect-tls #启动并连接主机A,注意主机A不要去连接主机B

等待下载weave剩下的支持容器

③启动docker容器,使用weave网桥给容器分配沟通地址

[root@192 ~]# docker run -dit -P centos/httpd:v1

[root@192 ~]# weave attach 192.168.2.2/24 容器ID

192.168.2.2

测试:

[root@192 ~]# docker exec -it 容器ID /bin/bash

[root@192 ··]# ping 192.168.2.2

PING 192.168.2.2 (192.168.2.2) 56(84) bytes of data.

64 bytes from 192.168.2.2: icmp_seq=1 ttl=64 time=3.06 ms

64 bytes from 192.168.2.2: icmp_seq=2 ttl=64 time=2.21 ms

跨主机通信成功

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3132584/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-05-16

  • 博文量
    4259
  • 访问量
    40166