7.5 routing mesh for communication between cluster services

Time:2021-10-21

Create overlay network

docker network create -d overlay swarm-overlay

Create whoamI container

Jwilder / whoamI image supports obtaining the host’s hostname through HTTP request

docker service create --name whoami -p 8000:8000 --network swarm-overlay -d jwilder/whoami

Create busybox container

docker service create --name busybox --network swarm-overlay -d busybox /bin/sh -c "while true;do sleep 3600;done"

View services

#Service list
[[email protected] ~]$ 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 node distribution
[[email protected] ~]$ 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 node distribution
[[email protected] ~]$ 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

It is found that busybox is deployed on the swarm-work2 (192.168.205.12) node and whoamI is deployed on the swarm-work1 (192.168.205.11) node

Network test

stayswarm-work2After testing on the node, it is found that you can ping the whoamI container on the swarm-work1 node

[[email protected] ~]$ 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

But there’s a problem,10.0.1.10It is not the external IP of the whoamI container, because after we expand the whoamI container to three, the returned IP is also the same10.0.1.10

docker service scale whoami=3

Then execute the following command on one of the whoamI containers and find10.0.1.10It’s just an IP on the lo network card. It’s really not an external IP.

[[email protected] ~]$ 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: [email protected]: <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: [email protected]: <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: [email protected]: <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

If we use the NSLOOKUP command, it can also be confirmed10.0.1.10Not real IP

[[email protected] ~]$ 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

in fact,10.0.1.10It refers to a unified external IP and virtual IP, i.e. service IP and VIP, allocated to multiple identical containers in consideration of container expansion

load balancing

Through VIP, a domain name can be resolved to multiple containers, and we can also see the effect of load balancing

#First visit
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

#Second visit
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

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]