Reference: https://github.com/docker/labs/blob/master/beginner/chapters/votingapp.md
First, create a Swarm.
$ docker swarm init
Swarm initialized: current node (o9grw63b1jc3kjvxehln2vush) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3bxzfkmh9gz4l7ap3qx395br876l6p2uwupeuqhtdws83k8i0y-clyn1usfm5x0jqianoc8j8j7e 192.168.65.3:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
Second, Create a Docker Compose file. names docker-stack.yml
version: "3"
services:
redis:
image: redis:alpine
ports:
- "6379"
networks:
- frontend
deploy:
replicas: 1 # deploy only 1 node
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
db:
image: postgres:9.4
volumes:
- db-data:/var/lib/postgresql/data
networks:
- backend
deploy:
placement:
constraints: [node.role == manager]
vote:
image: dockersamples/examplevotingapp_vote:before
ports:
- 5000:80
networks:
- frontend
depends_on:
- redis
deploy:
replicas: 2 # deploy 2 nodes for balance.
update_config:
parallelism: 2
restart_policy:
condition: on-failure
result:
image: dockersamples/examplevotingapp_result:before
ports:
- 5001:80
networks:
- backend
depends_on:
- db
deploy:
replicas: 1 # deploy only 1 node
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
worker:
image: dockersamples/examplevotingapp_worker
networks:
- frontend
- backend
deploy:
mode: replicated
replicas: 1 # deploy only 1 node
labels: [APP=VOTING]
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 3
window: 120s
placement:
constraints: [node.role == manager]
visualizer:
image: dockersamples/visualizer
ports:
- "8080:8080"
stop_grace_period: 1m30s
volumes:
- /var/run/docker.sock:/var/run/docker.sock
deploy:
placement:
constraints: [node.role == manager]
networks:
frontend:
backend:
volumes:
db-data:
Third, deploy it, and then we will look more deeply into the details:
docker stack deploy --compose-file docker-stack.yml vote
Creating network vote_frontend
Creating network vote_backend
Creating network vote_default
Creating service vote_vote
Creating service vote_result
Creating service vote_worker
Creating service vote_redis
Creating service vote_db
Fourth, to verify your stack has deployed, use docker stack services vote
$ docker stack services vote
ID NAME MODE REPLICAS IMAGE
25wo6p7fltyn vote_db replicated 1/1 postgres:9.4
2ot4sz0cgvw3 vote_worker replicated 1/1 dockersamples/examplevotingapp_worker:latest
9faz4wbvxpck vote_redis replicated 2/2 redis:alpine
ocm8x2ijtt88 vote_vote replicated 2/2 dockersamples/examplevotingapp_vote:before
p1dcwi0fkcbb vote_result replicated 2/2 dockersamples/examplevotingapp_result:before
If you take a look at docker-stack.yml
, you will see that the file defines
- vote container based on a Python image
- result container based on a Node.js image
- redis container based on a redis image, to temporarily store the data.
- .NET based worker app based on a .NET image
- Postgres container based on a postgres image
check vote result by http://localhost:5001/
$ docker stack deploy --compose-file docker-stack.yml vote
Updating service vote_db (id: xtj2cfti451f0m2o8c512l47b)
Updating service vote_vote (id: ur903zugvgqqfqtbk7icf66me)
Updating service vote_result (id: wkvh4w7ajcj1chup9ip6fmf97)
Updating service vote_worker (id: nmwilsr6r8f0re9005faaz5yu)
Updating service vote_visualizer (id: zl6q0johuc1bar77cumywfl3p)
Updating service vote_redis (id: 9p1nzisw7d7ostpf79e76qiu9)
then
do vote by http://localhost:5000/
check vote result by http://localhost:5001/
Sixth, Remove the stack
$ docker stack rm vote
Removing service vote_db
Removing service vote_redis
Removing service vote_result
Removing service vote_visualizer
Removing service vote_vote
Removing service vote_worker
Removing network vote_frontend
Removing network vote_backend
Removing network vote_default
没有评论:
发表评论