共计 3298 个字符,预计需要花费 9 分钟才能阅读完成。
创立 overlay 网络
docker network create -d overlay swarm-overlay
创立 whoami 容器
jwilder/whoami 镜像反对通过 http 申请来获取主机的 hostname
docker service create --name whoami -p 8000:8000 --network swarm-overlay -d jwilder/whoami
创立 busybox 容器
docker service create --name busybox --network swarm-overlay -d busybox /bin/sh -c "while true;do sleep 3600;done"
查看服务
# 服务列表
[vagrant@swarm-manager ~]$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
zq7ulpxk83nq busybox replicated 1/1 busybox:latest
q1j2ddophtom whoami replicated 1/1 jwilder/whoami:latest *:8000->8000/tcp
# busybox 节点散布
[vagrant@swarm-manager ~]$ docker service ps busybox
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
yzquayg07i2a busybox.1 busybox:latest swarm-work2 Running Running 23 seconds ago
# whoami 节点散布
[vagrant@swarm-manager ~]$ docker service ps whoami
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
1diq1k8h38o5 whoami.1 jwilder/whoami:latest swarm-work1 Running Running 43 seconds ago
发现 busybox 部署在了 swarm-work2(192.168.205.12)节点上,whoami 部署在了 swarm-work1(192.168.205.11)节点上
网络测试
在 swarm-work2
节点上测试,发现是能够 ping 通 swarm-work1 节点的上 whoami 容器的
[vagrant@swarm-work2 ~]$ docker exec c2f9bbbea76c ping whoami
PING whoami (10.0.1.10): 56 data bytes
64 bytes from 10.0.1.10: seq=0 ttl=64 time=0.058 ms
64 bytes from 10.0.1.10: seq=1 ttl=64 time=0.198 ms
64 bytes from 10.0.1.10: seq=2 ttl=64 time=0.332 ms
然而有一个问题,10.0.1.10
并不是 whoami 容器的对外 IP,因为咱们将 whoami 容器扩大到 3 台之后,返回的 ip 也都是10.0.1.10
docker service scale whoami=3
而后对其中一台 whoami 容器上执行如下命令,发现 10.0.1.10
只是 lo 网卡上的一个 ip,的确不是对外 ip。
[vagrant@swarm-manager ~]$ docker exec ac13768a6699 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 10.255.0.6/32 brd 10.255.0.6 scope global lo
valid_lft forever preferred_lft forever
inet 10.0.1.10/32 brd 10.0.1.10 scope global lo
valid_lft forever preferred_lft forever
23: eth0@if24: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue state UP
link/ether 02:42:0a:ff:00:08 brd ff:ff:ff:ff:ff:ff
inet 10.255.0.8/16 brd 10.255.255.255 scope global eth0
valid_lft forever preferred_lft forever
25: eth1@if26: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:12:00:03 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.3/16 brd 172.18.255.255 scope global eth1
valid_lft forever preferred_lft forever
28: eth2@if29: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue state UP
link/ether 02:42:0a:00:01:0e brd ff:ff:ff:ff:ff:ff
inet 10.0.1.14/24 brd 10.0.1.255 scope global eth2
valid_lft forever preferred_lft forever
如果咱们应用 nslookup 命令,也能证实 10.0.1.10
并不是实在 IP
[vagrant@swarm-work2 ~]$ docker exec -it c2f9bbbea76c /bin/sh
/ # nslookup tasks.whoami
Server: 127.0.0.11
Address 1: 127.0.0.11
Name: tasks.whoami
Address 1: 10.0.1.14 ac13768a6699.swarm-overlay
Address 2: 10.0.1.15 whoami.3.n249t5179xlde8tpymb0r5zwm.swarm-overlay
Address 3: 10.0.1.11 whoami.1.1diq1k8h38o5ase5pmuaodr0u.swarm-overlay
事实上,10.0.1.10
是 service 思考到容器的扩大,为多个雷同的容器调配的一个对立的对外 IP、虚构 IP,即 service ip、VIP
负载平衡
通过 VIP,一个域名解析到多个容器,咱们也可能看到负载平衡的成果了
# 第一次拜访
wget whoami:8000
Connecting to whoami:8000 (10.0.1.10:8000)
index.html 100% |*****************************************************************************************************************| 17 0:00:00 ETA
cat index.html
I'm ac13768a6699
# 第二次拜访
rm -rf index.html
wget whoami:8000
Connecting to whoami:8000 (10.0.1.10:8000)
index.html 100% |*****************************************************************************************************************| 17 0:00:00 ETA
cat index.html
I'm 409eef9c93da
正文完