May 29, 2017
2 min read

Building a Swarm Cluster in Local Machine with CoreOS

So last week I built a cluster on my local machine with docker swarm to get a taste of clustering. I used CoreOS as operating system on all the nodes as CoreOS is a container specific os. CoreOS also has several tools themselves for cluster orchestration. In this post I’ll show you how I made the cluster on vagrant machines.

First of all you should have VirtualBox or VMware to run virtual machine on your host machine and you’ll also need Vagrant for provisioning vm. VirtualBox should be running before starting our cluster. First we’ll pull CoreOS vagrant image, enter into a directory and clone the image

$ git clone [https://github.com/coreos/coreos-vagrant.git](https://github.com/coreos/coreos-vagrant.git) coreos
$ cd coreos

Our cluster will have 1 master node and 3 worker node, so we need to run 4 vm. Open the Vagrantfile and find $num_instances and makes it value 4. All the machine is configured to have 1GB of memory, so if you want to increase or decrease the memory change the value of vm_memory variables.

Now start all the vm

$ vagrant up

After sometimes all the vm will be started, check all the machine are running by below command

$ vagrant status

All 4 machine are running

Now we’ll ssh into the first machine and start our swarm cluster

$ vagrant ssh core-01 -- -A

To initialize the cluster we need the ip address of our machine. Run following command within the vm

$ ip addr show

It’ll show a bunch of network related information. The information we’re interested are the ip address of eth1 interface.

Finding IP of our machine

Now we’ll start the cluster, run the following command

$ docker swarm init --advertise-addr node_ip_address

--advertise-addr is used to tell docker swarm which ip address other node should use to join the cluster.

Initialize swarm cluster

So the command started the cluster and made the current machine the manager. You can see it also printed a token which other will use to join the cluster. You should keep this token secret.

Now let’s add a worker node to the cluster. First ssh into the second vm

$ vagrant ssh core-02 -- -A

And within the vm run following command

$ docker swarm join --token TOKEN manager_node_ip:2377

Attaching a worker node to the cluster

We can add the other vm as worker node in same way

Adding 3 worker node to the cluster

Now in the manager node we can see how many nodes are running by following command

$ docker node ls

List all the nodes

We can see 4 nodes are running currently, three of them as worker and a manger node. That means we have successfully started a swarm cluster of 4 nodes on our local machine.

Hopefully this was helpful. Happy Coding!

Comments:
If you found any error or have any suggestion let me know in the comments.