文章目录
创建一个overlay的网络
docker network create -d overlay demo
创建whoami service
docker service create --name whoami -p 8080:8080 --network demo -d jwilder/whoami
创建client service
docker service create --name client -d --network demo busybox sh -c "while true;do sleep 3600;done"
进入client service所在的节点,ping whoami 是可以ping同的
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
489c59e8436d busybox:latest "sh -c 'while true;d…" 3 minutes ago Up 3 minutes client.1.zmld0iwgetb7p5mab761ge8zu
[node1] (local) root@192.168.0.13 ~
$ docker exec -it 489c59e8436d sh
/ #
/ # ping whoami
PING whoami (10.0.0.8): 56 data bytes
64 bytes from 10.0.0.8: seq=0 ttl=64 time=0.499 ms
64 bytes from 10.0.0.8: seq=1 ttl=64 time=0.106 ms
64 bytes from 10.0.0.8: seq=2 ttl=64 time=0.089 ms
64 bytes from 10.0.0.8: seq=3 ttl=64 time=0.110 ms
扩展whoami service
docker service scale whoami=2
在client service中再次ping whoami 发现ip是不变的
/ # ping whoami
PING whoami (10.0.0.8): 56 data bytes
64 bytes from 10.0.0.8: seq=0 ttl=64 time=0.106 ms
64 bytes from 10.0.0.8: seq=1 ttl=64 time=0.073 ms
64 bytes from 10.0.0.8: seq=2 ttl=64 time=0.104 ms
64 bytes from 10.0.0.8: seq=3 ttl=64 time=0.071 ms
64 bytes from 10.0.0.8: seq=4 ttl=64 time=0.099 ms
nslookup 命令
localhost:~ xuzhongjie$ nslookup www.imooc.com
Server: 192.168.1.1
Address: 192.168.1.1#53
Non-authoritative answer:
Name: www.imooc.com
Address: 117.121.101.41
Name: www.imooc.com
Address: 115.182.41.180
Name: www.imooc.com
Address: 117.121.101.40
进入client service 容器执行
nslookup whoami
nslookup tasks.whoami
swarm 会给每个service分配虚拟ip
每次访问whoami 返回的id不一样,说明负载均衡。
/ # wget whoami:8000
Connecting to whoami:8000 (10.0.0.8:8000)
saving to 'index.html'
index.html 100% |*************************************************************************************| 17 0:00:00 ETA
'index.html' saved
/ # more index.html
I'm fc2f8a79b2d5
/ # rm -f index.html
/ # wget whoami:8000
Connecting to whoami:8000 (10.0.0.8:8000)
saving to 'index.html'
index.html 100% |*************************************************************************************| 17 0:00:00 ETA
'index.html' saved
/ # more index.html
I'm 99c1aa83eb38