🐋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!
☁️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 is a viable alternative, but your environment lasts for only four hours. There's also Multipass, but I'm already using Windows Subsytem for Linux 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, or 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.
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.
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. 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 and skipping to step 4.
If choosing DigitalOcean, 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.
On the left toolbar, click Droplets. Once you have done that, click create. DigitalOcean calls its servers droplets.
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.
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.
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 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.
Install Docker Engine by navigating to 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.
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
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
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.
🔥Results
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.
Top comments (0)