赞
踩
在docker-compose文件中添加networks
参数即可,可同时创建多个
- networks:
- mynetwork1:
- mynetwork2:
networks的参数ipam
即网络管理,其下还有driver
参数即指定使用的网络模式,以及config
配置参数(比如配置子网等)
- Docker Compose中的 default network 是根据环境来决定的,
- 在当前的单节点环境中默认为 bridge 模式,在swarm中为 overlay。
-
- networks:
- mynetwork1:
- ipam:
- driver: bridge
- mynetwork2:
- ipam:
- driver: default
- config:
- - subnet: "172.16.238.0/24"
在service
参数中加上参数即可,可指定多个网络,但是必须是上一步中已创建的网络,如果没有创建就会使用默认的网络即default nework
- services:
- flask-demo:
- image: flask-demo:latest
- environment:
- - REDIS_HOST=redis-server
- networks:
- - mynetwork1
- - mynetwork2
- ports:
- - 8080:5000
删除所有容器和镜像
- docker container rm -f $(docker container ps -aq)
- docker system prune -a -f
下载源码,进入源码目录
- docker-compose pull
- docker-compose build
- docker-compose up -d
-
- docker-compose ps
- 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
- docker-compose up -d --scale flask=3
- compose-scale- is up-to-date
- compose-scale--server_1 is up-to-date
- Creating compose-scale-example_flask_2 ... done
- Creating compose-scale-example_flask_3 ... done
- $ docker-compose ps
- 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- flask run -h 0.0.0.0 Up 5000/tcp
- compose-scale- flask run -h 0.0.0.0 Up 5000/tcp
- compose-scale--server_1 docker-entrypoint.sh redis ... Up 6379/tcp
进入其中一个容器ping flask会进行DNS解析的负载均衡,每次取第一个实例(ip)返回
访问5000端口运行程序也有负载均衡
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。