我觉得您的环境肯定有问题,我刚才又完全从头开始创建了一遍,完全没有问题,我使用了两台vagrant host (使用的vagrantfile是https://github.com/xiaopeng163/docker-k8s-lab/blob/master/lab/docker/multi-node/vagrant/Vagrantfile )
第一台地址192.168.205.10,第二台192.168.205.11
第一台上docker swarm init --advertise-addr 192.168.205.10 初始化
第二台docker swarm join --token SWMTKN-1-3brmkxoh41vm2tmecwe2dsxgzcsxb2sj4vlfj4u7utbe3jkjzu-82ri7cpzw1wm3td0vt397b6ms 192.168.205.10:2377
组成swarm集群
然后在manager节点上创建demo网络,创建whoami和client service
docker network create -d overlay demo
docker service create --name whoami -p 8000:8000 --network demo -d jwilder/whoami
docker service create --name client -d --network demo busybox sh -c "while true; do sleep 3600; done"
然后client的container被创建在node上,whoami在manager上
在node1上进入client,ping whoami
vagrant@docker-node2:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fcd4665013fc busybox:latest "sh -c 'while true; …" 38 seconds ago Up 38 seconds client.1.fe44b5uxtj593379imhfcpcd5
vagrant@docker-node2:~$ docker exec -it fc sh
/ #
/ #
/ #
/ # ping whoami
PING whoami (10.0.1.2): 56 data bytes
64 bytes from 10.0.1.2: seq=0 ttl=64 time=0.169 ms
64 bytes from 10.0.1.2: seq=1 ttl=64 time=0.119 ms
64 bytes from 10.0.1.2: seq=2 ttl=64 time=0.065 ms
^C
--- whoami ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.065/0.117/0.169 ms
/ # exit