docker-compose scale
配置
https://github.com/docker/compose/issues/5586
version: '2.2' services:template:image: tutum/hello-worldscale: 2
version: "3" services:pg:image: postgres:latestnetworks:default:aliases:- rdb.test.mepgadmin:image: dpage/pgadmin4:latestdeploy:replicas: 3environment:HOSTNAME: rdba.test.mePGADMIN_DEFAULT_EMAIL: user@test.mePGADMIN_DEFAULT_PASSWORD: SuperSecret
命令
https://noobtomaster.com/docker/running-and-scaling-applications-using-compose/#:~:text=Scaling%20your%20application%20becomes%20effortless%20with%20Docker%20Compose.,it%20to%20three%20containers%3A%20docker-compose%20up%20--scale%20web%3D3
version: '3' services:web:build: .ports:- "8080:80"volumes:- ./app:/usr/share/nginx/htmldb:image: mysqlenvironment:- MYSQL_ROOT_PASSWORD=secret
docker-compose up
docker-compose up --scale web=3
microservice-demo -- scale config
https://github.com/fanqingsong/microservice-demo
version: '2'services:gateway:build: traefikcommand: --api --providers.docker --tracing.instana.logLevel="DEBUG" --accesslog # Enables the web UI and tells Traefik to listen to dockernetworks:- microservicesports:- "80:80"# - "443:443"- "9090:8080"volumes:- /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker eventsuser:build: usernetworks:- microservices# ports:# - "8081:80"cart:build: cartnetworks:- microservices# ports:# - "8082:80"product:build: productnetworks:- microservices# ports:# - "8083:80"event:build: eventnetworks:- microservices# ports:# - "8084:80"web:build: webnetworks:- microservices scale: 2# ports:# - "8085:80"networks:microservices:driver: bridge
启动
测试
traefic demo
scale动态扩展, 扩展容器会自动注册到 traefic
https://github.com/vegasbrianc/docker-compose-demo/tree/master
version: '3' services:whoami:image: containous/whoami # A container that exposes an API to show its IP address labels:- "traefik.frontend.rule=Host:whoami.docker.localhost"redis:image: redisnetworks:- back-tierreverse-proxy:image: traefik command: --api --providers.docker --tracing.instana.logLevel="DEBUG" --accesslog # Enables the web UI and tells Traefik to listen to dockerports:- "80:80" # The HTTP port- "8080:8080" # The Web UI (enabled by --api)volumes:- /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker eventsnetworks:front-tier:driver: bridgeback-tier:driver: bridge
Now comes the fun part of compose which is scaling. Let's scale our web service from 1 instance to 5 instances. This will now scale our web service container. We now should run an update on our stack so the Loadbalancer is informed about the new web service containers.
docker-compose scale whoami=5
Now run our curl command again on our web services and we will now see the hostname change. To get a deeper understanding tail the logs of the stack to watch what happens each time you access your web services.
``` docker-compose logs whoami whoami_5 | Starting up on port 80 whoami_4 | Starting up on port 80 whoami_3 | Starting up on port 80 whoami_2 | Starting up on port 80 whoami_1 | Starting up on port 80 ```
Here's the output from my docker-compose logs after I curled the
whoami
application so it is clear that the round-robin is sent to all 5 web service containers.``` reverse-proxy_1 | 172.26.0.1 - - [01/May/2019:19:16:34 +0000] "GET /favicon.ico HTTP/1.1" 200 647 "http://whoami.docker.localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36" 10 "Host-whoami-docker-localhost-1" "http://172.26.0.2:80" 1ms ```