当前位置:   article > 正文

Docker Compose Scale网络、水平扩展与负载均衡_docker compose headscale

docker compose headscale

使用docker-compose创建网络

  • 在docker-compose文件中添加networks参数即可,可同时创建多个

    1. networks:
    2. mynetwork1:
    3. mynetwork2:

  • networks的参数ipam即网络管理,其下还有driver参数即指定使用的网络模式,以及config配置参数(比如配置子网等)

    1. Docker Compose中的 default network 是根据环境来决定的,
    2. 在当前的单节点环境中默认为 bridge 模式,在swarm中为 overlay
    3. networks:
    1. mynetwork1:
    2. ipam:
    3. driver: bridge
    4. mynetwork2:
    5. ipam:
    6. driver: default
    7. config:
    8. - subnet: "172.16.238.0/24"

    在service中指定使用的network

  • service参数中加上参数即可,可指定多个网络,但是必须是上一步中已创建的网络,如果没有创建就会使用默认的网络即default nework

    1. services:
    2. flask-demo:
    3. image: flask-demo:latest
    4. environment:
    5. - REDIS_HOST=redis-server
    6. networks:
    7. - mynetwork1
    8. - mynetwork2
    9. ports:
    10. - 8080:5000

    环境清理

    删除所有容器和镜像

    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. docker-compose ps
  5. docker-compose down

 

Creating network "compose-scale-example_default" with the default driver
Creating compose-scale-example_flask_1 ... done
Creating compose-scale-example_client_1 ... done
Creating compose-scale-example_redis-server_1 ... done


Name Command State Ports
----------------------------------------------------------------------------------------
compose-scale- sh -c while true; do sleep ... Up
compose-scale- flask run -h 0.0.0.0 Up 5000/tcp
compose-scale--server_1 docker-entrypoint.sh redis ... Up 6379/tcp

水平扩展 scale

  1. docker-compose up -d --scale flask=3
  2. compose-scale- is up-to-date
  3. compose-scale--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. compose-scale- sh -c while true; do sleep ... Up
  9. compose-scale- flask run -h 0.0.0.0 Up 5000/tcp
  10. compose-scale- flask run -h 0.0.0.0 Up 5000/tcp
  11. compose-scale- flask run -h 0.0.0.0 Up 5000/tcp
  12. compose-scale--server_1 docker-entrypoint.sh redis ... Up 6379/tcp

负载均衡

  • 进入其中一个容器ping flask会进行DNS解析的负载均衡,每次取第一个实例(ip)返回

  • 访问5000端口运行程序也有负载均衡

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/365235
推荐阅读
相关标签
  

闽ICP备14008679号