docker compose 水平扩展

本节课的源码下载 本节源码

环境清理

删除所有容器和镜像

  1. $ docker container rm -f $(docker container ps -aq)
  2. $ docker system prune -a -f

启动

下载源码,进入源码目录

  1. $ docker-compose pull
  2. $ docker-compose build
  3. $ docker-compose up -d
  4. Creating network "compose-scale-example_default" with the default driver
  5. Creating compose-scale-example_flask_1 ... done
  6. Creating compose-scale-example_client_1 ... done
  7. Creating compose-scale-example_redis-server_1 ... done
  8. $ docker-compose ps
  9. Name Command State Ports
  10. ----------------------------------------------------------------------------------------
  11. compose-scale-example_client_1 sh -c while true; do sleep ... Up
  12. compose-scale-example_flask_1 flask run -h 0.0.0.0 Up 5000/tcp
  13. compose-scale-example_redis-server_1 docker-entrypoint.sh redis ... Up 6379/tcp

水平扩展 scale

  1. $ docker-compose up -d --scale flask=3
  2. compose-scale-example_client_1 is up-to-date
  3. compose-scale-example_redis-server_1 is up-to-date
  4. Creating compose-scale-example_flask_2 ... done
  5. Creating compose-scale-example_flask_3 ... done
  6. $ docker-compose ps
  7. Name Command State Ports
  8. ----------------------------------------------------------------------------------------
  9. compose-scale-example_client_1 sh -c while true; do sleep ... Up
  10. compose-scale-example_flask_1 flask run -h 0.0.0.0 Up 5000/tcp
  11. compose-scale-example_flask_2 flask run -h 0.0.0.0 Up 5000/tcp
  12. compose-scale-example_flask_3 flask run -h 0.0.0.0 Up 5000/tcp
  13. compose-scale-example_redis-server_1 docker-entrypoint.sh redis ... Up 6379/tcp

添加 nginx

源码下载 源码更新

docker-compose-flask-nginx