Creating a Cluster with Docker Swarm
Tags: showdev, docker, beginners, tutorial
Published: 2022-12-13 03:11:24

##🐋Why Docker Swarm?
In the wild world of managing clusters of containers, Docker Swarm is an awesome introduction. As a fledgling DevOps student, as far as I know, Kubernetes is the go to software of choice for managing clusters. In order to get a quick foundational understanding of clusters, I am not using Kubernetes yet. I will be making the leap to K8S in the near future though!

![managing a cluster(mob) of meerkats](https://d3i3l3kraiqpym.cloudfront.net/wp-content/uploads/2016/05/25191803/Meerkat.jpg)

##☁️What is 'Cluster Management?'
The real question to ask though is 'What really cluster management?' Why can we not just make changes to our containers as needed and deploy, break down, and redeploy as needed? As "the cloud" continues to grow and evolve, traffic grows by the day, and organizations focus on bypassing technical limitations, the goal of the IT staff is always to minimize operational overhead.

Something to help manage the hundreds, if not thousands of containers being used across an organization at any point in time. At this very moment, your dev.to session is likely using a container. In fact, Netflix, Amazon, LinkedIn, Facebook, all use containers in their day to day operations. On the scale of companies like this, managing containers and ensuring they're operational becomes nigh impossible.

By creating a manager and telling it how many "nodes" we want (think of a node as a container), the manager manages these nodes for us. If a node crashes, no biggie, the manager will automagically create a new one. If we need to scale the amount of nodes needed up or down, who cares? Tell your manager you need more or less replicas and BANG! It's done!

Our server will be hosted by DigitalOcean. (I've been told) it is most similar to a production setup. [play with docker](https://labs.play-with-docker.com/) is a viable alternative, but your environment lasts for only four hours. There's also [Multipass](https://multipass.run/), but I'm already using [Windows Subsytem for Linux](https://techcommunity.microsoft.com/t5/windows-11/how-to-install-the-linux-windows-subsystem-in-windows-11/m-p/2701207) and I wanted to get up and running ASAP.

##🥅Goal
The goal is to create an operational Docker Swarm with three replicas.

##✅Success Criteria
Successful if three replicas within the same Swarm can ping Google's DNS server at 8.8.8.8.

##🧑‍🍳Prerequisites
- Linux, MacOS, [WSL](https://learn.microsoft.com/en-us/windows/wsl/install), or [PuTTY](https://docs.digitalocean.com/products/droplets/how-to/add-ssh-keys/create-with-putty/)
- Terminal Access
- Credit/Debit card (no charges will be made, but one must be on file)

---

###Part 1: Generate an SSH key
######Why?
Remote access to DigitalOcean servers via our command line.

####How?
Open up your terminal and begin the SSH key generation process.

`ssh-keygen`

Terminal will ask you where and what to name file, just hit enter.
![public/private save location](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukgk8n85jcrtqcly2sum.png)

The terminal will then ask for you to enter a passphrase. If someone had your private/public key and the IP address of the DigitialOcean server(s) you create, they could remotely login to your server

Our public key needs to be stored locally on our device for a short period of time. Run the following command, then copy and paste it into a .txt file. The one I provided below is a fake public key.

![example public ssh key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k34jtga9aiwzh58w116u.png) 

###Step 2: Create Three Nodes
######Why?
We want to have three separate servers that will work together as a cluster of containers.

####How?
Visit [DigitalOcean](https://www.digitalocean.com/). For new accounts (promo code = activate60), you can get $200 in free credits. In order to create an account, you will need a credit card. If you don't have one, I recommend using [play with docker](https://labs.play-with-docker.com/) and skipping to step 4.

If choosing [DigitalOcean](https://www.digitalocean.com/), go create an account. After creating an account, click _New Project_ on the left toolbar and give it a name. The name I gave mine was _Docker Swarm Practice_.

![DigitalOcean Projects Bar](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dkisi6969535twwrqd7g.png)

On the left toolbar, click _Droplets_. Once you have done that, click create. DigitalOcean calls its servers droplets.

![Droplets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l7b2qzl4wwdoaut7bbmw.png)

Create an Ubuntu basic server with a regular SSD for $6/month. Next, make sure you're in the SSH tab under _Authentication_. Click _New SSH Key_. Paste the public key into the box we had previously created and saved.

![SSH Public Key Entry](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wuecricy2oqmue1eid3o.png)

Change the number of droplets to three at the bottom of the screen. After that, name the top droplet, _node1_, and _node2_+ _node3_ should autofill. Afterwords, click _Create Droplet_.


![named droplets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qw10oyqvck9jzors5q0e.png)

###Step 3: SSH in & Install Docker on a Single VM
######Why?
Docker needs to be installed on a node in order use Docker Swarm

####How?
You have to connect to the servers via the command line by connecting (SSHing) to them. To do that, boot up your Linux, MacOS, WSL, or [PuTTY](https://docs.digitalocean.com/products/droplets/how-to/add-ssh-keys/create-with-putty/) terminal.

Open up terminal and connect to node1.

`ssh root@'node1-ip-address'`

Omit the quotation marks. Where `node1-ip-address` is, put your node1's IP address. You'll also need to enter your password if you created it when creating your SSH key.


![Three Droplets + IP Addresses](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6q76si6rfmb3s27pzw82.png)

Install Docker Engine by navigating to [get.docker.com](get.docker.com) and run the script that Docker provides. You can find it below, but be aware that it may change in the future.

```
curl -fsSL https://get.docker.com -o get-docker.sh \
&& sh get-docker.sh
```

###Step 4: Repeat Installing Docker on Node2 + Node3
######Why?
All the nodes need Docker installed to join the swarm.

####How?
Repeat step 3 two more times. Once for node2 and once for node3. Do these simultaneously in separate terminal windows to save time. _Leave the three terminal windows open, we will need to use them in step 5._


![SSH into Three Nodes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1m1d3dxlgnxqirk69md9.png)

###Step 5: Initialize Docker Swarm & Add Worker Nodes
Docker Swarm is actually disabled by default so we have to initialize it.

On node1 - `docker swarm init --advertise-addr 'node-ip-address-here'`

Afterwards, we have to join node2 + node3 into the swarm. In order to do this, we need a join token from node1 (which is now the leader of the swarm since it is where we initialized it.)

`docker swarm join-token manager`

![swarm join token](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pllpjl1e4ym0k7vsiinq.png)

Copy and paste the join token into node2 and node3's terminal windows.

###Step 6: Using the Swarm
######Why?
What's the point of going through this work if we're not going to test it?

####How?
Let's get a small service that is spread across our three nodes equally.

`docker service create alpine --replicas 3 ping 8.8.8.8`

`docker service create alpine` is similar to the basic Docker command to get a single container running, `docker container run`, except because we're using Swarm we are now creating a service rather than a single container. 

The flag `--replicas 3` tells the service we want Docker Swarm to manage three containers between our three nodes.

`ping 8.8.8.8` is just a simple command to continually have the nodes ping the IPv4 address of 8.8.8.8.


Now, check to see the name of the cluster.

`docker service ls`

![service name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hh0mw1w58tx9he5kwsvs.png)

Check to see if the nodes are pinging Google's IPv4 address.

`docker service logs 'enter-service-name-here'`

You can use tab completion if this is your own service. If it's working, you should see more than one node pinging Google in the log.

![node2 + node3 working](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o5e58eo293qk57fkzsnb.png)


##🔥Results
<img width="100%" style="width:100%" src="https://media.giphy.com/media/wwK1cdxXADARO/giphy.gif">
At this point, we have met our success criteria and accomplished our goal of creating a Docker Swarm with three replicas. I learned how to create a small, simple cluster of containers with Docker Swarm and have learned how to utilize DigitalOcean to create a quick-and-easy production-grade developer environment.