实现docker跨主机通信 – 基于Swarm集群的overlay网络

环境配置

节点名 节点IP 节点服务
dnode0 192.168.0.11 安装 Docker,并初始化Swarm集群
dnode1 192.168.0.12 安装 Docker
dnode2 192.168.0.13 安装 Docker

配置三台机器,请注意修改hostname为不同的值,防火墙放行如下端口:

  • TCP port 2376 for secure Docker client communication. This port is required for Docker Machine to work. Docker Machine is used to orchestrate Docker hosts.
  • TCP port 2377 is used for communication between the nodes of a Docker Swarm or cluster. It only needs to be opened on manager nodes.
  • UDP port 4789 for overlay network traffic (container ingress networking).
  • TCP and UDP port 7946 for communication among nodes (container network discovery).

初始化Swarm集群服务

docker swarm init

如果没有记住加入集群的token,以下可以重新获取

docker swarm join-token worker

其他节点分别加入Swarm集群

docker swarm join --token SWMTKN-1-tokenxxxxxxx 192.168.0.11:2377

在节点上创建网络

docker network create --driver overlay --subnet=192.168.2.0/24 --gateway=192.168.2.1 --attachable vnet2