Stay Hungry,Stay Foolish!

docker-compose scale load balance

实验

基于此代码,部署两个web容器, 一个gateway容器。

https://github.com/fanqingsong/microservice-demo

 

song@song-VirtualBox:~/workspace/microservice-demo$ docker network inspect microservice-demo_microservices 
[
    {
        "Name": "microservice-demo_microservices",
        "Id": "d273c4321a6d7d016e4032075b7b5d0558cb7662ada5b9389b4e9f42e4809311",
        "Created": "2024-06-28T19:18:06.634784192+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "043ae385c7bbe981b11a5867aa444b72df02d6e340de4dab742a7f3e01c9d08b": {
                "Name": "microservice-demo-web-2",
                "EndpointID": "dae9a580f33ce01e75bc55a87759e6e5fa7e3c7f4b46660396107897228ec2c8",
                "MacAddress": "02:42:ac:12:00:07",
                "IPv4Address": "172.18.0.7/16",
                "IPv6Address": ""
            },
            "07029b18f487f4bc7c92009f72b3e46172c080b88af88d45a3ab3a42dd8d0712": {
                "Name": "microservice-demo-product-1",
                "EndpointID": "3ef39376fc26a50472924071bd7e61aecdacc856e3081a027d31024be53545ff",
                "MacAddress": "02:42:ac:12:00:05",
                "IPv4Address": "172.18.0.5/16",
                "IPv6Address": ""
            },
            "1888ff08b8599fe2c36f503decd7fd69265b78f411fda135f923f58d0a5c4a75": {
                "Name": "microservice-demo-web-1",
                "EndpointID": "ccf81c411e70b0af7f39f376b7e9f2d1c9283b0ebef30ff362336f7d0a46c60d",
                "MacAddress": "02:42:ac:12:00:08",
                "IPv4Address": "172.18.0.8/16",
                "IPv6Address": ""
            },
            "3e83f0edc3e536b4ea433ff1ef927ef2df63b752c7e3896c3900032af424866a": {
                "Name": "microservice-demo-user-1",
                "EndpointID": "606db720255222b616ae4532b781cb7e610a91af91e03f1889a2c22da0c8623a",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "7e56ba05b850caa06addaf4b90f931ec4c6260f421a24b2b71ee41f8f7affb61": {
                "Name": "microservice-demo-cart-1",
                "EndpointID": "6a57470ecfee254e983e6347f9a0a2da4272cd5a591636f4f5f61f3f6b35e6e2",
                "MacAddress": "02:42:ac:12:00:04",
                "IPv4Address": "172.18.0.4/16",
                "IPv6Address": ""
            },
            "8547903abc8f1cb4f7409d961b32eee321f8c8f9daac7dbdaeb442046daa0cc6": {
                "Name": "microservice-demo-event-1",
                "EndpointID": "ca827b11d7edeb172e286ed7302fddd8913e4af1c84a0c77553af9782497ecc9",
                "MacAddress": "02:42:ac:12:00:06",
                "IPv4Address": "172.18.0.6/16",
                "IPv6Address": ""
            },
            "93ed44f184c7d540bbab032f30fd90677287badc9846852431f2b244c9077df5": {
                "Name": "microservice-demo-gateway-1",
                "EndpointID": "0d5e3838fe4998011f1553af58b4b58a500fd9852c26eecbbf1731b2564e57a9",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "microservices",
            "com.docker.compose.project": "microservice-demo",
            "com.docker.compose.version": "2.20.3"
        }
    }
]
song@song-VirtualBox:~/workspace/microservice-demo$ 

 

登录 gateway容器, 连续ping web,

发现响应的容器地址有变化,在web1和web2之间好像按照固定时间间隔切换。

可以看出是dns负载均衡的实现, 在可选的web容器ips之间做时间片轮转。

song-VirtualBox:~/workspace/microservice-demo$ docker exec -it microservice-demo-gateway-1 ping web
PING web (172.18.0.8): 56 data bytes
64 bytes from 172.18.0.8: seq=0 ttl=64 time=0.091 ms
64 bytes from 172.18.0.8: seq=1 ttl=64 time=0.093 ms
64 bytes from 172.18.0.8: seq=2 ttl=64 time=0.083 ms
64 bytes from 172.18.0.8: seq=3 ttl=64 time=0.080 ms
^C
--- web ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.080/0.086/0.093 ms
song@song-VirtualBox:~/workspace/microservice-demo$ 
song@song-VirtualBox:~/workspace/microservice-demo$ 
song@song-VirtualBox:~/workspace/microservice-demo$ docker exec -it microservice-demo-gateway-1 ping web
PING web (172.18.0.8): 56 data bytes
64 bytes from 172.18.0.8: seq=0 ttl=64 time=0.322 ms
64 bytes from 172.18.0.8: seq=1 ttl=64 time=0.089 ms
64 bytes from 172.18.0.8: seq=2 ttl=64 time=0.093 ms
^C
--- web ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.089/0.168/0.322 ms
song@song-VirtualBox:~/workspace/microservice-demo$ 
song@song-VirtualBox:~/workspace/microservice-demo$ docker exec -it microservice-demo-gateway-1 ping web
PING web (172.18.0.8): 56 data bytes
64 bytes from 172.18.0.8: seq=0 ttl=64 time=0.048 ms
64 bytes from 172.18.0.8: seq=1 ttl=64 time=0.075 ms
64 bytes from 172.18.0.8: seq=2 ttl=64 time=0.076 ms
^C
--- web ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.048/0.066/0.076 ms
song@song-VirtualBox:~/workspace/microservice-demo$ 
song@song-VirtualBox:~/workspace/microservice-demo$ 
song@song-VirtualBox:~/workspace/microservice-demo$ 
song@song-VirtualBox:~/workspace/microservice-demo$ docker exec -it microservice-demo-gateway-1 ping web
PING web (172.18.0.8): 56 data bytes
64 bytes from 172.18.0.8: seq=0 ttl=64 time=0.095 ms
64 bytes from 172.18.0.8: seq=1 ttl=64 time=0.082 ms
^C
--- web ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.082/0.088/0.095 ms
song@song-VirtualBox:~/workspace/microservice-demo$ 
song@song-VirtualBox:~/workspace/microservice-demo$ docker exec -it microservice-demo-gateway-1 ping web
PING web (172.18.0.8): 56 data bytes
64 bytes from 172.18.0.8: seq=0 ttl=64 time=0.050 ms
64 bytes from 172.18.0.8: seq=1 ttl=64 time=0.105 ms
64 bytes from 172.18.0.8: seq=2 ttl=64 time=0.130 ms
^C
--- web ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.050/0.095/0.130 ms
song@song-VirtualBox:~/workspace/microservice-demo$ 
song@song-VirtualBox:~/workspace/microservice-demo$ docker exec -it microservice-demo-gateway-1 ping web
PING web (172.18.0.7): 56 data bytes
64 bytes from 172.18.0.7: seq=0 ttl=64 time=0.086 ms
64 bytes from 172.18.0.7: seq=1 ttl=64 time=0.104 ms
64 bytes from 172.18.0.7: seq=2 ttl=64 time=0.122 ms
^C
--- web ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.086/0.104/0.122 ms
song@song-VirtualBox:~/workspace/microservice-demo$ 
song@song-VirtualBox:~/workspace/microservice-demo$ 
song@song-VirtualBox:~/workspace/microservice-demo$ docker exec -it microservice-demo-gateway-1 ping web
PING web (172.18.0.7): 56 data bytes
64 bytes from 172.18.0.7: seq=0 ttl=64 time=0.055 ms
64 bytes from 172.18.0.7: seq=1 ttl=64 time=0.139 ms
^C
--- web ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.055/0.097/0.139 ms
song@song-VirtualBox:~/workspace/microservice-demo$ 

 

posted @ 2024-06-28 20:53  lightsong  阅读(2)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel