Docker Host创建swarm overlay网络

Docker Host创建swarm overlay网络

  • 依赖
  • 修改系统参数
  • 关闭防火墙
  • 连接Swarm
  • 更改hostname
  • 创建overlay网络
  • 测试

依赖

  • 准备三台虚拟机vm1,vm2,vm3。三台机器在同一个eth中。
  • 操作系统:CentOS 7.6
  • 安装Docker(点击)

修改系统参数

IPv6会影响到overlay网络的运行,需要关掉。
IPv4 Forward可以开启IP路由功能,当数据包被发往本机错误的网卡时,可以转发到正确网卡中。开启会影响运行速度。overlay网络依赖此功能。
关闭IPv6,开启IPv4 Forward 参考这里

关闭防火墙

service iptables stop
service firewalld stop

连接Swarm

vm1 作为主节点(manager node),其他节点要加入overlay网络只需要获取这个节点的token然后加入即可(worker node)。
主节点上执行:

docker swarm init

启动成功显示如下:

Swarm initialized: current node (brjg2ltw1sv4dihlxjr6tkojp) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-4hj1cuiz76i7jkw5uwpiomi4kulkrfulcan51v6mz5y88orv8n-65o0wega86chw0nreghmtofw1 172.169.18.28:2377
    
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

根据vm1的提示命令,在vm2和vm3上加入overlay网络

 docker swarm join --token SWMTKN-1-4hj1cuiz76i7jkw5uwpiomi4kulkrfulcan51v6mz5y88orv8n-65o0wega86chw0nreghmtofw1 172.169.18.28:2377

显示

This node joined a swarm as a worker.

查看node信息

docker node ls

结果如下 Id 就是node名称

ID                            HOSTNAME                STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
7eawdhzcvb1vx2gj7nr3fuge5     localhost.localdomain   Ready               Active                                  19.03.1
brjg2ltw1sv4dihlxjr6tkojp *   localhost.localdomain   Ready               Active              Leader              19.03.1
sp8taco3e5qivv26dy8a0jm57     localhost.localdomain   Ready               Active                                  19.03.1

更改hostname

#vm1
hostnamectl set-hostname vm1
service docker restart
#vm2
hostnamectl set-hostname vm2
service docker restart
#vm3
hostnamectl set-hostname vm3
service docker restart

查看hostname

service node ls

结果

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
brjg2ltw1sv4dihlxjr6tkojp *   vm1                 Ready               Active              Leader              19.03.1
7eawdhzcvb1vx2gj7nr3fuge5     vm2                 Ready               Active                                  19.03.1
sp8taco3e5qivv26dy8a0jm57     vm3                 Ready               Active                                  19.03.1

创建overlay网络

在vm1上创建overlay网络,vm2和vm3上面都可以访问。attachable参数开始container访问权限,默认只可被service使用。

docker network create -d overlay --attachable overlay

测试

#vm1
docker run --network overlay --rm --name deb1 -itd debian bash
#vm2
docker run  --network overlay --rm --name deb2 -it debian ping deb1

vm2 结果

Unable to find image 'debian:latest' locally
latest: Pulling from library/debian
5ae19949497e: Pull complete 
Digest: sha256:903779f30a7ee46937bfb21406f125d5fdace4178074e1cc71c49039ebf7f48f
Status: Downloaded newer image for debian:latest
PING deb1 (10.0.0.87) 56(84) bytes of data.
64 bytes from deb1.overlay (10.0.0.87): icmp_seq=1 ttl=64 time=2.43 ms
64 bytes from deb1.overlay (10.0.0.87): icmp_seq=2 ttl=64 time=0.428 ms
64 bytes from deb1.overlay (10.0.0.87): icmp_seq=3 ttl=64 time=0.342 ms
64 bytes from deb1.overlay (10.0.0.87): icmp_seq=4 ttl=64 time=0.432 ms
64 bytes from deb1.overlay (10.0.0.87): icmp_seq=5 ttl=64 time=0.387 ms
64 bytes from deb1.overlay (10.0.0.87): icmp_seq=6 ttl=64 time=0.410 ms
64 bytes from deb1.overlay (10.0.0.87): icmp_seq=7 ttl=64 time=0.555 ms
64 bytes from deb1.overlay (10.0.0.87): icmp_seq=8 ttl=64 time=0.343 ms
64 bytes from deb1.overlay (10.0.0.87): icmp_seq=9 ttl=64 time=0.430 ms
64 bytes from deb1.overlay (10.0.0.87): icmp_seq=10 ttl=64 time=0.392 ms
^C
--- deb1 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 12ms
rtt min/avg/max/mdev = 0.342/0.614/2.430/0.608 ms

你可能感兴趣的:(Docker)