使用网络“my-network”
在群组中设置两个泊坞窗虚拟机docker-machine create --driver virtualbox --virtualbox-hostonly-cidr "10.10.11.1/24" --virtualbox-host-dns-resolver node-1
docker-machine create --driver virtualbox --virtualbox-hostonly-cidr "10.10.12.1/24" --virtualbox-host-dns-resolver node-2
ManagerIP=`docker-machine ip node-1`
docker-machine ssh node-1 docker swarm init --advertise-addr ${ManagerIP}
WorkerToken=`docker-machine ssh node-1 docker swarm join-token worker | grep token | awk '{ print $5 }'`
docker-machine ssh node-2 "docker swarm join --token ${WorkerToken} ${ManagerIP}:2377 --advertise-addr $(docker-machine ip node-2)"
docker-machine ssh node-1 "docker network create --attachable --driver overlay --subnet 10.0.9.0/24 my-network"
使用复制服务创建docker-compose文件(test.docker-compose.yml)
version: '3.1'
networks:
my-network:
external: true
services:
test:
image: busybox
networks:
- my-network
command: "tail -f /dev/null"
deploy:
replicas: 2
部署为堆栈“teststack”
docker stack deploy --compose-file test.docker-compose.yml teststack
检查
eval $(docker-machine env node-1)
docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
k0ynsbcldrws857ioes7lvj9l * node-1 Ready Active Leader
kdbpaopbh2arzvcp0jwygw1lm node-2 Ready Active
docker stack ps teststack
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ydfu68b163bu teststack_test.1 busybox:latest node-2 Running Running about a minute ago
cl0glr0emk9r teststack_test.2 busybox:latest node-1 Running Running about a minute ago
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ca90fbfb8b3 busybox:latest "tail -f /dev/null" 3 minutes ago Up 3 minutes teststack_test.2.cl0glr0emk9rejuf25eync1cz
eval $(docker-machine env node-2)
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
701f527d8327 busybox:latest "tail -f /dev/null" 4 minutes ago Up 4 minutes teststack_test.1.ydfu68b163bu1h1lq0107l6f8
我希望能够从另一个实例访问该服务的一个实例/容器。让我们说我们想从node-2上的容器teststack_test.1.ydfu68b163bu1h1lq0107l6f8访问node-1上的容器teststack_test.2.cl0glr0emk9rejuf25eync1cz
docker exec -it teststack_test.1.ydfu68b163bu1h1lq0107l6f8 /bin/sh
ping teststack_test.2.cl0glr0emk9rejuf25eync1cz
PING teststack_test.2.cl0glr0emk9rejuf25eync1cz (10.0.9.6): 56 data bytes
^C
--- teststack_test.2.cl0glr0emk9rejuf25eync1cz ping statistics ---
12 packets transmitted, 0 packets received, 100% packet loss
它可以使用类似名称
访问“本身”ping teststack_test.1.ydfu68b163bu1h1lq0107l6f8
PING teststack_test.1.ydfu68b163bu1h1lq0107l6f8 (10.0.9.5): 56 data bytes
64 bytes from 10.0.9.5: seq=0 ttl=64 time=0.046 ms
64 bytes from 10.0.9.5: seq=1 ttl=64 time=0.083 ms
^C
--- teststack_test.1.ydfu68b163bu1h1lq0107l6f8 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.046/0.064/0.083 ms
请注意,它确实为内部网络上的teststack_test.2.cl0glr0emk9rejuf25eync1cz获取了正确的IP(10.0.9.6)
eval $(docker-machine env node-1)
docker exec -it teststack_test.2.cl0glr0emk9rejuf25eync1cz /bin/sh
ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:0A:00:09:06
inet addr:10.0.9.6 Bcast:10.0.9.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1450 Metric:1
RX packets:13 errors:0 dropped:0 overruns:0 frame:0
TX packets:13 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1218 (1.1 KiB) TX bytes:1218 (1.1 KiB)
...
如何从node-2上的容器teststack_test.1.ydfu68b163bu1h1lq0107l6f8访问node-1上的teststack_test.2.cl0glr0emk9rejuf25eync1cz?反之亦然
有没有办法可以访问teststack_test的1号实例,即使实例移动到另一个节点也能正常工作(我希望这个名称不再是teststack_test.1.ydfu68b163bu1h1lq0107l6f8,而是teststack_test.1。< ;一些,别的>)? ping teststack_test.1似乎不起作用。