当前位置 > it书童 > docker > 正文

docker scale 实现水平扩展和负载均衡

docker it书童 2019-10-04 17:40:04 0赞 0踩 411阅读 0评论

scale 基本使用

scale 可以将容器进行扩展,极速实现负载均衡

源码

启动三个应用:

$ docker-compose up --scale web=3 -d
Starting flask-redis_redis_1 ...
Starting flask-redis_redis_1 ... done
Starting flask-redis_web_1   ... done
Creating flask-redis_web_2   ... error
Creating flask-redis_web_3   ... error

ERROR: for flask-redis_web_3  Cannot start service web: driver failed programming external connectivity on endpoint flask-redis_web_3 (74eade16e6045684b30c37d13731e797724388792839cc9c2119b7368214f939): Bind for 0.0.0.0:8080 failed: port is already allocated

ERROR: for flask-redis_web_2  Cannot start service web: driver failed programming external connectivity on endpoint flask-redis_web_2 (32732c543ebf1050f8e704a3fe491ddb642dcc97a6d8f4db0d9751ff0f7bebdc): Bind for 0.0.0.0:8080 failed: port is already allocated

ERROR: for web  Cannot start service web: driver failed programming external connectivity on endpoint flask-redis_web_3 (74eade16e6045684b30c37d13731e797724388792839cc9c2119b7368214f939): Bind for 0.0.0.0:8080 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.

启用多个 web 时,不能在配置中指定端口,会报端口占用错误

docker-compose.yml 的端口绑定去掉

version: "3"

services:

  redis:
    image: redis

  web:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      REDIS_HOST: redis

再次执行

$ docker-compose up --scale web=3 -d
Recreating flask-redis_web_1 ...
Recreating flask-redis_web_1 ... done
Recreating flask-redis_web_2 ... done
Recreating flask-redis_web_3 ... done

查看状态

$ docker-compose ps
       Name                      Command               State    Ports
-----------------------------------------------------------------------
flask-redis_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp
flask-redis_web_1     python app.py                    Up      5000/tcp
flask-redis_web_2     python app.py                    Up      5000/tcp
flask-redis_web_3     python app.py                    Up      5000/tcp

由此可见,三个 web 都使用了容器内部的 5000 端口

由于此时并我们的主机并没有与 docker 容器进行端口映射,因此不能访问该应用

再建立10个

$ docker-compose up --scale web=10 -d
Starting flask-redis_web_1 ...
Starting flask-redis_web_1 ... done
Starting flask-redis_web_2 ... done
Starting flask-redis_web_3 ... done
Creating flask-redis_web_4  ... done
Creating flask-redis_web_5  ... done
Creating flask-redis_web_6  ... done
Creating flask-redis_web_7  ... done
Creating flask-redis_web_8  ... done
Creating flask-redis_web_9  ... done
Creating flask-redis_web_10 ... done

总数是 10个,而不是 13 个

$ docker-compose ps
       Name                      Command               State    Ports
-----------------------------------------------------------------------
flask-redis_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp
flask-redis_web_1     python app.py                    Up      5000/tcp
flask-redis_web_10    python app.py                    Up      5000/tcp
flask-redis_web_2     python app.py                    Up      5000/tcp
flask-redis_web_3     python app.py                    Up      5000/tcp
flask-redis_web_4     python app.py                    Up      5000/tcp
flask-redis_web_5     python app.py                    Up      5000/tcp
flask-redis_web_6     python app.py                    Up      5000/tcp
flask-redis_web_7     python app.py                    Up      5000/tcp
flask-redis_web_8     python app.py                    Up      5000/tcp
flask-redis_web_9     python app.py                    Up      5000/tcp

关闭容器

$ docker-compose down
Stopping flask-redis_web_5   ... done
Stopping flask-redis_web_9   ... done
Stopping flask-redis_web_7   ... done
Stopping flask-redis_web_8   ... done
Stopping flask-redis_web_6   ... done
Stopping flask-redis_web_4   ... done
Stopping flask-redis_web_10  ... done
Stopping flask-redis_web_1   ... done
Stopping flask-redis_web_2   ... done
Stopping flask-redis_web_3   ... done
Stopping flask-redis_redis_1 ... done
Removing flask-redis_web_5   ... done
Removing flask-redis_web_9   ... done
Removing flask-redis_web_7   ... done
Removing flask-redis_web_8   ... done
Removing flask-redis_web_6   ... done
Removing flask-redis_web_4   ... done
Removing flask-redis_web_10  ... done
Removing flask-redis_web_1   ... done
Removing flask-redis_web_2   ... done
Removing flask-redis_web_3   ... done
Removing flask-redis_redis_1 ... done
Removing network flask-redis_default

用 scale 实现负载均衡

源码

$ docker-compose up -d
$ docker-compose ps
      Name                    Command               State                    Ports
---------------------------------------------------------------------------------------------------
lb_scale_lb_1      /sbin/tini -- dockercloud- ...   Up      1936/tcp, 443/tcp, 0.0.0.0:8000->80/tcp
lb_scale_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp
lb_scale_web_1     python app.py                    Up      80/tcp, 0.0.0.0:32768->8080/tcp

发出请求,由于只有一个 web 容器,因此每次响应的容器名称是一致的

$ curl 127.0.0.1:8000
Hello Container World! I have been seen 1 times and my hostname is 3265aaffe8f8.
$ curl 127.0.0.1:8000
Hello Container World! I have been seen 2 times and my hostname is 3265aaffe8f8.

构建三个 web 容器

$ docker-compose up --scale web=3 -d
lb_scale_redis_1 is up-to-date
Starting lb_scale_web_1 ... done
Creating lb_scale_web_2 ... done
Creating lb_scale_web_3 ... done
lb_scale_lb_1 is up-to-date

再次发出请求,可看到三个 web 容器采用轮询策略进行响应

$ curl 127.0.0.1:8000
Hello Container World! I have been seen 3 times and my hostname is 3265aaffe8f8.
$ curl 127.0.0.1:8000
Hello Container World! I have been seen 4 times and my hostname is f22a8b798666.
$ curl 127.0.0.1:8000
Hello Container World! I have been seen 5 times and my hostname is 246410c8c2f7.
$ curl 127.0.0.1:8000
Hello Container World! I have been seen 6 times and my hostname is 3265aaffe8f8.
$ curl 127.0.0.1:8000
Hello Container World! I have been seen 7 times and my hostname is f22a8b798666.
$ curl 127.0.0.1:8000
Hello Container World! I have been seen 8 times and my hostname is 246410c8c2f7.

当然,单机上的 scale 只是模拟,不具备生产意义,因为物理资源是不变的

转载须注明出处:https://www.itshutong.com/articles/197/docker-scale-to-realize-horizontal-expansion-and-load-balancing
关于我
一个文科出身的程序员,追求做个有趣的人,传播有价值的知识,微信公众号主要分享读书思考心得,不会有代码类文章,非程序员的同学请放心订阅
发表评论
我有句话,不知当讲不当讲?
要讲之前请先 登录