This article teaches how to deploy the Voting Application using Docker Stack for improved scalability and management across multiple nodes.
Welcome to this comprehensive lesson on Docker Stacks. Previously, you learned how to run the Voting Application using Docker run and Docker Compose. In this lesson, we will enhance your deployment strategy by using Docker Stack to deploy the Voting Application across multiple nodes in a cluster.
Begin by modifying your Docker Compose file to be compatible with Docker Stack. Update the file format to version 3 by specifying the version at the top and organizing the configuration under a services section. Below is the updated Compose file:
Create a new directory (e.g., “voting-app-stack”) on your Docker host.
Copy the updated Compose file into this directory and save it as docker-stack.yaml.
Once you have the file ready, deploy the stack with the following command, ensuring you use the same stack name (“voting-app-stack”):
Copy
Ask AI
root@docker-master:/root/voting-app-stack # docker stack deploy voting-app-stack --compose-file docker-stack.ymlCreating network voting-app-stack_defaultCreating service voting-app-stack_workerCreating service voting-app-stack_resultCreating service voting-app-stack_redisCreating service voting-app-stack_dbCreating service voting-app-stack_voteroot@docker-master:/root/voting-app-stack # docker service lsID NAME MODE REPLICAS IMAGE PORTS3jkedpmwa9vu voting-app-stack_worker replicated 0/1 dockersamples/examplevotingapp_worker:latestdxiwtn4ersh7 voting-app-stack_db replicated 1/1 postgres:9.4pc268n3july4 voting-app-stack_result replicated 1/1 dockersamples/examplevotingapp_result:latest *:5001->80/tcpqwhi4xn67d7 voting-app-stack_redis replicated 1/1 redis:latestz7jywvrse2lr voting-app-stack_vote replicated 0/1 dockersamples/examplevotingapp_vote:latest *:5000->80/tcproot@docker-master:/root/voting-app-stack #root@docker-node2:/root #
When you deploy the stack, Docker creates a default network named voting-app-stack_default where all containers connect. Use the following command to verify the status of your services:
Copy
Ask AI
root@docker-master:/root/voting-app-stack # docker service ls
Initially, you may notice that the worker and vote services are missing one instance. This is normal as the deployment process scales up based on resource availability.
To inspect the status of a particular service, such as the vote service, run:
Copy
Ask AI
docker service ps voting-app-stack_vote
Once deployed, open your web browser and navigate to port 5000 to view the voting interface. Each vote is captured and processed, with results displayed through the result service.
By default, each service starts with a single instance. For increased availability, you can scale the voting interface by deploying multiple replicas. To scale the vote service, update the Compose file to include a deploy parameter with the replicas setting under the vote service:
The Voting Application is now successfully deployed using Docker Stack. This configuration not only simplifies deployment across multiple nodes but also provides an easy pathway for scaling services. Keep experimenting with Docker Stack to enhance your container orchestration skills.Happy Dockering!