Docker Swarm 创建overlay网络

Docker Swarm 创建overlay网络

环境:

  • 系统:Centos 7.4 x64
  • 应用版本:Docker 18.09.0
  • 管理节点:192.168.1.79
  • 工作节点:192.168.1.78
  • 工作节点:192.168.1.77

一、创建网络与服务

1、管理节点:创建overlay网络名字为my-network

docker network create --driver overlay my-network
命令:docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
3d1ut7rm89tv        my-network          overlay             swarm
查看网络创建

2、管理节点:创建服务并使用overlay网络

docker service create \
--replicas 3 \
--network my-network \
--name hello \
busybox ping www.baidu.com
# 创建服务
docker service create \
# 副本数
--replicas 3 \
# 添加网络
--network my-network \
# 服务名
--name hello \
# 镜像
busybox 
# 容器执行指令
ping www.baidu.com
命令解析
命令:docker service ps my-web
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
lb9djycc131z        my-web.1            nginx:latest        slave02             Running             Running 1 second ago                         
4wjvue79bdfc        my-web.2            nginx:latest        slave02             Running             Running 1 second ago                         
r1rzm3tmq456        my-web.3            nginx:latest        slave01             Running             Running 17 seconds ago
查看创建服务

二、测试网络连通

1、工作节点1与2:进入容器测试连通情况

# 进入容器
docker exec -it ID名称 sh
# 工作节点1
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:0A:00:00:07  
          inet addr:10.0.0.7  Bcast:0.0.0.0  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

# 工作节点2
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:0A:00:00:08  
          inet addr:10.0.0.8  Bcast:0.0.0.0  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

2、测试通信

# 工作节点1:容器间测试通信
/ # ping 10.0.0.8
PING 10.0.0.8 (10.0.0.8): 56 data bytes
64 bytes from 10.0.0.8: seq=0 ttl=64 time=0.737 ms
64 bytes from 10.0.0.8: seq=1 ttl=64 time=0.443 ms
64 bytes from 10.0.0.8: seq=2 ttl=64 time=0.450 ms

三、其他动作

将现有服务连接到overlay网络

docker service update --network-add my-network hello

删除正在运行的网络连接

docker service update --network-rm my-network hello

猜你喜欢

转载自www.cnblogs.com/xiangsikai/p/9938374.html