Simulate a docker cluster on a single machine using multipass

Simulate a docker cluster on a single machine using multipass

Microsoft Certified Data Engineer Associate

Published on: 12 November 2023

Author: Ramesh Kanjinghat

Introduction

Have you ever needed to deploy multiple Docker nodes on a single machine, especially for tasks like container orchestration or local development environment testing? If you have, you're not alone. Many developers face this challenge, and that's where Multipass steps in as an elegant solution.

What is Multipass?

Imagine you're running a Windows 10 machine, and you want to create multiple Docker nodes that you can manage as a single cluster using Docker Swarm. These virtual machines (VMs) should be set up quickly, managed effortlessly, and come pre-equipped with Docker. Moreover, they should seamlessly communicate with each other. While it's possible to manually set up these VMs, it's a time-consuming and complex process. What you need is a tool that streamlines the entire procedure, and that's where Multipass comes into play.
In the words of its creators, Multipass is a tool designed to rapidly generate cloud-style Ubuntu VMs on Linux, macOS, and Windows.
What distinguishes Multipass is its user-friendly nature and the fact that it provides pre-configured VM images, including one with Docker pre-installed.

Getting Started with Multipass

For the purpose of this blog, I have used Windows 10 machine and we will only powershell to setup everything.
First, we have to install Multipass on the Windows 10 machine. You can find the step-by-step instructions in the official documentation here: https://multipass.run/docs/how-to-guides
We can check if multipass is running by running below command in powershell
1 multipass version
Check multipass version.
Check multipass version.

Creating Docker Nodes

Now that we Multipass is installed let's go ahead a create the nodes reqiored for our docker swarm cluster. Here I will create 3 manager nodes and 2 worker nodes. You can create as many as your machine supports or you need. Minimum 2 nodes are recommended when running clusters.
  • Check available multipass images
1multipass find
List of available multipass images. Docker image is highlighted above.
List of available multipass images. Docker image is highlighted above.
  • Now create our first manager node, manager1.
1multipass launch docker --name manager1
First node, manager1 created.
First node, manager1 created.
  • Repeat the command to create two more Managers, e.g., manager2 and manager3.
When it comes to multipass, it treats all nodes as equivalent, without distinguishing between manager nodes and worker nodes; each node is essentially a virtual machine (VM) with Docker installed.
  • Repeate above command to create worker nodes, worker1 and worker2.
  • Validate all the VMs are created as we need.
1multipass list
List out the created VMs.
List out the created VMs.
Great! we have all the five nodes created.

Creating a Docker Swarm

As this blog on multipass, I won't cover the details of crearting a Docker swarm.
  • You will have to shell in each node to create the docker swarm with all those nodes, you can shell into a node using the command multipass shell <node name>. For instance, to shell into the node manager1
1multipass shell manager1
The shell interface once you shell into a VM.
The shell interface once you shell into a VM.
  • To create a Docker Swarm and add Manager and Worker nodes to it, you'll need to shell into each node individually and run swarm commands. Follow the instructions here https://docs.docker.com/engine/swarm/swarm-mode/ to create a swarm and add nodes to it.
  • Once the swarm is created and all the nodes are added shell into one of the managers and run the command, docker node ls to list our all nodes in the swarm.
1docker node ls
List out all the docker swarm nodes.
List out all the docker swarm nodes.
And we can see that there are 3 manager nodes and 2 worker nodes in the swarm. Now you can run all docker swarm commands, setup networking, load balancing etc. on this swarm cluster.

Cleanup

So far, we have created the vms but we also want to clean-up the vms once we don’t need them anymore. Clean-up is a 2-step process,
  • delete: Makes the VMs non-usable but they are not permanently removed. You can bring them back, if you want them later.
  • purge: Permanently deletes those Vms. You can recreate them if you want them later.
  • Delete all nodes
1multipass delete --all
List out the VMs after they are deleted.
List out the VMs after they are deleted.
  • Purge all deleted nodes
1multipass purge
Check if there are deleted VMs after they are purged.
Check if there are deleted VMs after they are purged.
I ran delete and purge separately but if you want you can do it in a single command,
1multipass delete --purge --all
I have deleted all the nodes but if you want to delete only couple then you can run below command for all those nodes
1multipass delete --purge <node name>
You can find more details here, https://multipass.run/docs/remove-an-instance.
I hope that this information will assist you in getting started with the multipass tool.