Docker swarm – Load balancing asp.net core Ubuntu 20.04 – Part 1

Focus

Today, we’ll learn how to host your asp.net core site for load balancing inside a Docker swarm on Ubuntu 20.04. Docker swarm, at a simplest level, is a group (cluster) of machines. The machines can be physical or virtual. Each machine is called a node. Each node can have a role of a leader or a worker. Docker swarm is a container orchestration tool just like Kubernetes. It helps you manage your containers hosted across machines.

This is Part 1 of the article. I wanted to focus on native docker capabilities to orchestrate/manage containers. Consequently, I picked up Docker swarm. I’ll be covering Kubernetes in upcoming blogs.

Note: You can promote all nodes into a leader role. But for the sake of simplicity, we’ll keep one leader and others as worker nodes. Here, I’ve created three virtual machines. First one as leader and other two as worker nodes. I use Bitvise SSH client to login into my Ubuntu Servers. You can use putty if you like. Both support great copy pasting of commands. Since I use virtual machines, Bitvise easily helps me transfer files from host to guest machines via a GUI.

The IP addresses of my machines are:

NodeNode detailsIP AddressMachine Name
1Leader node192.168.26.128jupiter
2Worker node 1192.168.26.129saturn
3Worker node 2192.168.26.130neptune

This is a first step for load balancing your site. I’ll be using the same asp.net core hello world docker container which I created in my previous article.

Pre-requisites

  1. Run sudo apt-get update on both the worker nodes.
  2. Ensure OpenSSH server is installed on all machines while OS installation.
  3. Ensure that the docker service is installed on both, Worker node 1 and 2 machines. You can refer this for installation.
  4. You don’t need to install any other software tools or packages on Worker node 1 and 2. Just docker is enough. Don’t install dotnet core sdk or any thing else for this article. We’ll keep Worker node 1 and 2 as clean as possible.
  5. Remember to append sudo to all your commands or just type sudo -s in the beginning to skip typing sudoevery time.
  6. The Leader node has the asp.net core hello world web application installed. The container image originally created also resides here.

Goal

  1. Host an asp.net core inside a docker container on a leader node.
  2. Leader node will replicate your container to all worker nodes automatically.

Prepare the leader node

We’ll quickly setup Docker swarm. It just boils down to once single command to initialize the swarm. When you run docker system info, it shows that the swarm is inactive.

Assuming this is the first time you are creating the swarm, run: docker swarm init

Swarm initialized: current node (hmvgx5st2rgqw0ybz6frw7bew) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-289ioi145whp5tg80zpb4sd9ilc57axa3a56epra3li321a3xy-e6sj9l1zlgjdlhht8qex1hh52 192.168.26.128:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

Your output will not be the same but similar. Now, you have promoted your first machine to a manager. Importantly, in the output itself you will see the command (docker swarm join) you need to run on Worker node 1 and 2. This will make the worker nodes join the swarm.

On leader node, run: docker node ls. You will see:

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
ofbzz2nk2o29y0epytdvaky6m *   jupiter             Ready               Active              Leader              19.03.8

As a next step, SSH into your Worker node 1 and 2 machines. Run individually:

sudo docker swarm join --token SWMTKN-1-289ioi145whp5tg80zpb4sd9ilc57axa3a56epra3li321a3xy-e6sj9l1zlgjdlhht8qex1hh52 192.168.26.128:2377

Important note: All subsequent commands should be run on the Leader node. They will not run on the Worker nodes.

It will give you a message that the node has joined the swarm. Once you have ensured that both the machines have joined the swarm, you can confirm by running:

docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
ofbzz2nk2o29y0epytdvaky6m *   jupiter             Ready               Active              Leader              19.03.8
29gi2dej0bk66yehy9jqm1if3     neptune             Ready               Active                                  19.03.8
gs2piolqn814eurpcj5f6qq3r     saturn              Ready               Active                                  19.03.8

Now when you run, docker system info, it shows that the swarm is active. We’ll now prepare to host the container into the swarm.

Install Docker compose

First run,
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Then give execute permissions to Docker compose,
sudo chmod +x /usr/local/bin/docker-compose

Check if docker compose is correctly installed:
docker-compose --version
Output will be something like: docker-compose version 1.25.5, build 8a1c60f6

Create registry to replicate containers

You need to create a registry to be able to replicate containers on all worker nodes

So run: docker service create --name registry --publish published=5000,target=5000 registry:2

wzx4cntqs5cavhd326z5w4pux
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged

Run docker node ls and check the output:

ID                  NAME                MODE                REPLICAS            IMAGE                  PORTS
wzx4cntqs5ca        registry            replicated          1/1                 registry:2             *:5000->5000/tcp

Now switch to ./apps/aspnet-hw/aspnetcoreapp/ folder

We need to create a docker-compose.yml file. Type nano docker-compose.yml and paste the following contents inside it.

version: '3'

services:
web:
image: 127.0.0.1:5000/counter-image
  build: .
  ports:
     - "8000:5000"

Note: The image for the web app is built using the Dockerfile defined above. It’s also tagged with 127.0.0.1:5000 – the address of the registry created earlier. This is important when distributing the app to the swarm.

Push docker image to swarm

To distribute the web app’s image across the swarm, it needs to be pushed to the registry you set up earlier. With the help of Compose, this is very simple, run:

docker-compose push
Output:
Pushing web (127.0.0.1:5000/counter-image:latest)...
The push refers to repository [127.0.0.1:5000/counter-image]
2089e92f307d: Pushed
d28d93422a14: Pushed
98b87267e8cd: Pushed
731d9ef100ab: Pushed
097cf5cb984a: Pushed
4c1434a6c15b: Pushed
c2adabaecedb: Pushed
latest: digest: sha256:cdb85f4a62aaaddbb6f32ce900fd03e8c5d10997715f0c70fcc162c3ae5b8051 size: 1792

The stack is now ready to be deployed:

docker stack deploy --compose-file docker-compose.yml counter-image
Ignoring unsupported options: build

Creating network counter-image_default
Creating service counter-image_web

Run docker service ls

ID            NAME                MODE                REPLICAS            IMAGE                                 PORTS
kngu8zmvnxbt  counter-image_web   replicated          1/1                 127.0.0.1:5000/counter-image:latest   *:8000->5000/tcp
kug3cnpctqad  registry            replicated          1/1                 registry:2                            *:5000->5000/tcp

Replication complete

Done! Now your container will be replicated to Worker nodes 1 and 2 seamlessly. It might take a while say a minute or two. But since our container size is small, the replication would be very quick (under a minute).

Now you need to test your replication:

On Leader machine type: curl http://localhost:8000. It will show HTML output.
On Leader machine type: curl http://192.168.26.129:8000. It will show HTML output.
On Leader machine type: curl http://192.168.26.130:8000. It will show HTML output.
As an alternative, on the Worker node 1 and 2 machines, you can also type: curl http://localhost:8000 and it will still show HTML output.

To check the status of your container, run: docker stack services counter-image

ID                  NAME                MODE                REPLICAS            IMAGE                                 PORTS
kngu8zmvnxbt        counter-image_web   replicated          1/1                 127.0.0.1:5000/counter-image:latest   *:8000->5000/tcp

Conclusion

In the first part, we covered how to host your asp.net core site for load balancing inside a Docker swarm on Ubuntu 20.04. In the second part of this article, we will see implementing and testing load balancing.

Clean up

Remove a node from a swarm: docker node rm <machinename> --force

Leave a swarm, run this from the Worker node: sudo docker swarm leave --force

Bring down the stack, run: docker stack rm counter-image

Tear down registry, run: docker service rm registry

Destroy the swarm, run:
First, on Worker nodes: docker swarm leave --force
Last, on Leader node: docker swarm leave --force

Troubleshooting

Sometimes, when you reboot Worker nodes, they remain in “Down” status and not reachable by the swarm. In such cases, after rebooting the Worker node, just restart docker service on the worker node and things will be fine.

systemctl restart docker

If you forget the swarm joining token, just run on the Leader node:

docker swarm join-token manager

You may also like...

81 Responses

  1. Aw, this was a really nice post. In idea I would like to put in writing like this additionally ? taking time and actual effort to make a very good article? but what can I say? I procrastinate alot and by no means seem to get something done.

  2. There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game. Both boys and girls feel the impact of just a moment?s pleasure, for the rest of their lives.

  3. WONDERFUL Post.thanks for share..more wait .. 😉 ?

  4. Hi, i think that i saw you visited my web site so i came to “return the favor”.I am attempting to find things to enhance my website!I suppose its ok to use a few of your ideas!!

  5. Wow, amazing blog format! How long have you been running a blog for? you make running a blog look easy. The whole glance of your website is great, let alone the content!!

  6. Wow, superb blog structure! How long have you been blogging for? you made running a blog glance easy. The entire glance of your site is fantastic, as well as the content hitman

  7. I really appreciate this post. I have been looking everywhere for something similar to this! Thank goodness I found it on Bing. You’ve made my day! Thx again! soyos

  8. I write often and I genuinely admire your material. This fantastic piece has absolutely peaked my attention. I am mosting likely to book mark your internet site and also keep looking for all new facts about when a week. I opted in for your RSS feed in addition.

  9. I truly wanted to construct a simple word to be able to say thanks to you for the great tactics you are posting on this website. My rather long internet research has finally been honored with reasonable content to write about with my friends and family. I ‘d point out that we website visitors are unequivocally blessed to be in a magnificent community with many outstanding professionals with useful methods. I feel rather privileged to have seen your entire web pages and look forward to really more fabulous minutes reading here. Thank you again for everything.

  10. Please let me know if you’re looking for a writer for your blog. You have some really great posts and I think I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some material for your blog in exchange for a link back to mine. Please shoot me an e-mail if interested. Thanks!

  11. Wonderful blog! I found it while browsing on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thanks

  12. Have you ever considered creating an ebook or guest authoring on other sites? I have a blog based on the same topics you discuss and would love to have you share some stories/information. I know my visitors would value your work. If you are even remotely interested, feel free to shoot me an e mail.

  13. Great site you have here but I was wondering if you knew of any community forums that cover the same topics talked about in this article? I’d really love to be a part of group where I can get opinions from other experienced people that share the same interest. If you have any recommendations, please let me know. Bless you!

  14. Thanks for your personal marvelous posting! I seriously enjoyed reading it, you are a great author.I will make certain to bookmark your blog and will often come back down the road. I want to encourage one to continue your great writing, have a nice holiday weekend!

  15. Does your website have a contact page? I’m having a tough time locating it but, I’d like to shoot you an email. I’ve got some creative ideas for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it develop over time.

  16. Thanks for the marvelous posting! I definitely enjoyed reading it, you will be a great author.I will make sure to bookmark your blog and may come back down the road. I want to encourage yourself to continue your great job, have a nice morning!

  17. Mytjx.com says:

    Pretty nice post. I just stumbled upon your blog and wished to say that I have really enjoyed surfing around your blog posts. In any case I will be subscribing to your feed and I hope you write again soon!

  18. Nice blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple tweeks would really make my blog stand out. Please let me know where you got your theme. Thank you

  19. click here says:

    I just could not depart your web site before suggesting that I actually enjoyed the standard info a person provide for your visitors? Is gonna be back often in order to check up on new posts

  20. Thanks – Enjoyed this post, how can I make is so that I receive an alert email every time you write a fresh update?

  21. Read this says:

    Today, while I was at work, my cousin stole my apple ipad and tested to see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views. I know this is completely off topic but I had to share it with someone!

  22. I together with my guys ended up studying the good guidelines on your web page while all of a sudden I had a terrible feeling I never expressed respect to you for those techniques. All the young boys had been for this reason very interested to learn all of them and have honestly been taking advantage of those things. Appreciate your simply being really kind and also for utilizing these kinds of extraordinary topics millions of individuals are really desirous to be informed on. My honest apologies for not saying thanks to sooner.

  23. Visit says:

    Hi there! This is my first comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading your posts. Can you recommend any other blogs/websites/forums that cover the same subjects? Many thanks!

  24. Thanks a lot for giving everyone an extremely wonderful chance to read in detail from this site. It really is very pleasurable and stuffed with a lot of fun for me and my office peers to visit your website at a minimum thrice a week to read through the latest guides you will have. Not to mention, I am certainly happy with the breathtaking ideas you give. Certain 1 ideas in this article are essentially the most effective I’ve ever had.

  25. I absolutely love your blog and find a lot of your post’s to be precisely what I’m looking for. Do you offer guest writers to write content for yourself? I wouldn’t mind producing a post or elaborating on some of the subjects you write about here. Again, awesome web log!

  26. I’m not sure why but this web site is loading extremely slow for me. Is anyone else having this problem or is it a problem on my end? I’ll check back later on and see if the problem still exists.

  27. I wanted to construct a brief comment so as to appreciate you for those pleasant pointers you are sharing on this site. My incredibly long internet lookup has at the end of the day been honored with wonderful facts and techniques to exchange with my company. I ‘d believe that most of us readers are quite lucky to live in a notable community with so many outstanding individuals with interesting things. I feel somewhat lucky to have discovered the webpage and look forward to plenty of more cool minutes reading here. Thanks once more for everything.

  28. Hi, i think that i saw you visited my site so i came to return the favor.I’m trying to find things to improve my website!I suppose its ok to use some of your ideas!!

  29. Best View i have ever seen !

  30. Hey would you mind sharing which blog platform you’re using? I’m going to start my own blog soon but I’m having a hard time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something completely unique. P.S Sorry for getting off-topic but I had to ask!

  31. I’m really impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you customize it yourself? Anyway keep up the nice quality writing, it is rare to see a nice blog like this one these days..

Leave a Reply

Your email address will not be published. Required fields are marked *

Enter Captcha Here : *

Reload Image