Scaling Asp.Net 6 Application using Docker Swarm

On 11/27/21 I worked on finding a way to scale Docker Containers with Docker Swarm, practiced on the Staging Server, and proved that when I use the IP_Has algorithm in Nginx Docker file default.conf the IP sticks to one instance.

However, I wasn't satisfied yet. I needed to know if Routing indeed worked when you scale the replication to multiple apps, this lead to putting this project on shelf until January 16, 2022, when I verified that Docker Swarm indeed manages Routing to Scaled Nodes inside the Docker Swarm Service.

There was an issue if not using IP_Hash where If you log in, you will be logged out due to the round-robin load-balancing algorithm in Nginx.

1. When you are ready to scale or use Docker Swarm use the docker-compose-swarm.yml file to create containers.

Steps to creating a Docker Stack and Services
1. docker swarm init #this will initialize the machine into a Docker Swarm Manager
   - note the token, copy it, and store it for other nodes from different machines to join.
2. Deploy docker-compose-swarm.yml file to the Docker Swarm Start
    - before you deploy first
   docker-compose -f docker-compose-swarm.yml build #to build your images
    docker stack deploy -c docker-compose-swarm.yml NameOfMyStack
 - This will create networks, containers and start managing pods
3. After deploying a stack you can then query services like so
    docker service ls #to list all the services running within a stack
4. You can then scale the deployment
    docker service scale MyFirstStack_webmvc2 = 4 #this will scale the name of the service
   - In order to verify that you are hitting different app every time you request header of a hostname
     to verify that it actually hit a different app.
4. Delete the Stack
   docker stack rm MyFirstStack #this will delete a stack and its services
5. Leave the machine as a DockerSwarm
    docker swarm leave -f #You will have to force it to leave, the machine won't be the Docker Swarm Manager

Does the Docker Swarm Service Scale automatically route to nodes?

Login to See the Rest of the Answer

Yes, I tested scaling the application nodes to 4 and then hitting the port for one single application e.g. 5004, and printing the hostname on each instance of the application. Every time I started a session from another computer or different browser the hash for the Host Name changed, this indicated that the Docker Service Scaled nodes was routing based on the number of connections the node had. In other words, it was routing using a Round-Robin Algorithm.

© 2024 - ErnesTech - Privacy
E-Commerce Return Policy