Docker Swarm Series: #2nd Create a highly available environment
Published: 2023-06-24 13:42:31

--- series: Swarm ---
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5dhscpvf2hb3mx334z13.png)
<br>
<br>


# Inception <br><br>

Hello everyone, This article is part of The Swarm series, The knowledge in this series is built in sequence, Check out my profile.

In the last article we covered a high overview about Orchestration tools and Swarm, what ports should to be opened between nodes, And setup the environment using [Play-with-docker](https://labs.play-with-docker.com/) labs.

In This article We will complete from the stand-up point, Will create a highly available Swarm environment using [Play-with-docker](https://labs.play-with-docker.com/) labs, and deploy a simple webapp application using DockerCLI commands.<br>

----------------------------------------------------------
<br><br>
# Lab Overview <br><br>

The MAIN point of having an **Orchestration tool** is to have a **highly available environment** and **highly available applications**, besides the other features that provide *(e.g. load balancer, monitoring, etc,,)* Because of that **Today's article** will **focus** on how to **create a high available environment** and **deploy** a simple webapp using DockerCLI. <br>
_enough talking and let's get started_

----------------------------------------------------------
<br><br>
# Highly available environment overview <br><br>

  - As we need our application to be highly available to avoid **single point of failure** if the application goes down. it's the same idea on the **environment level**, if we created a high available application and load balance the traffic between them but the application lives on the **same node server**. here we have a high available application *(from the traffic aspect)* however we **don't** on environment level, if the node server goes down the application will goes down too. To avoid this we should have a **highly available environment** and the application **lives on more than one node**, And this is the **idea behind** the **Orchestration tools** to deliver the **simplicity** to achieve this.<br><br>


  - To create a highly available environment on Swarm we should have at least three manager node but typically no more than seven, manager nodes container the necessary information to manage the cluster, if the manager node goes down this will cause **cluster failed function**, **How to determine the number of manager nodes:**
    - Three manager nodes tolerate one node failure.
    - Five manager nodes tolerate two node failures.
    - Sever manager nodes tolerate three node failures.<br>

Regarding worker nodes, at least two worker nodes for redundancy and fault tolerance, but you can add more nodes as needed to handle the workload.<br><br>

> Actually, The Swarm Manager node will host the applications like  worker node as well.<br><br>

Therefor, In this lab will create Three manager nodes with two worker nodes.

----------------------------------------------------------
<br><br>
# Create a highly available environment <br><br>

  - Open [Play-with-docker](https://labs.play-with-docker.com) labs.
  - Press **ADD NEW INSTANCE**, and **initiate Docker swarm mode** using: 
```bash
docker swarm init --advertise-addr eth0

# to get the interface name use
ip a s
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksshy6vorpj69e3zo7xa.png)<br><br>

  - copy and past _-ctrl+shift+v-_  the highlighted command at the screenshot to generate a token _-at the same node-_ that will use to join the other managers to the cluster.<br>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d69kffjufnprbv0b2hsj.png)

<br>

  - Press **ADD NEW INSTANCE** to create, And join another Manager node to the the cluster.
  - copy and past the highlighted command at the last screenshot to join the cluster as a manager node.<br>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wlumh3dniu195jhes6zq.png)

<br>
<br>
<br>


  - Repeat the last step to join another manager node at the same cluster.<br>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6iidtwmlxx097oluwoy2.png)

<br>
<br>
<br>


  - let's create the Worker nodes, Go back to the 1st manager node created and copy the command that join the worker nodes to the swarm cluster.<br><br>

  - Press **ADD NEW INSTANCE**, past the command to join the cluster as worker node:<br>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/synzvinugk9qsx1wyzzz.png)
<br>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9ejyu18klh6pp4vq13p.png)
<br>

  - Repeat the last step to join another worker node to the cluster.<br>
<br>

  - Let's check out our environment, Go to the any one of the manager nodes, and type the below command:
```bash
docker node ls
```
<br>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k98ie4foisqbayr5p8nl.png)
<br>

As listed there are three manager nodes Besides two worker nodes, The asterisk means the node number two that handle the command you've written ``` docker node ls ``` <br>
Also at the manager status column, There is a leader and the reachable, The leader is the manager node leader initiator and the reachable means that the node is currently available and can communicate with the other nodes in the Swarm cluster.
<br>

----------------------------------------------------------
<br><br>

# Deploy a webapp application overview <br><br>

Now as we have our cluster running and ready to deploy containers, To deploy a container we need to create a service, the service is and abstraction that represent multiple container of the same image deployed across the cluster, you can assume the service is the same concept as POD in K8s.<br>

run the service by using `docker service` command instead of using `docker run` or `start` in normal docker mode, try to differentiate between **normal docker mode** and **docker swarm** mode with these commands <br><br>

## Deploy a webapp application <br><br>

  - copy and past the following command to run your first app:

```bash
docker service create --detach=true --name nginx1 --publish 80:80 --mount source=/etc/hostname,target=/usr/share/nginx/html/index.html,type=bind,ro nginx:1.12

```
<br>

> This command statement is **declarative,** and Swarm will try to maintain the state declared, which means Swarm will compare the **desired state** of the application With **the Actual state,** However this is the first running of this application So the desired have been declared at the command however the actual Swarm will created for us.<br><br>

> The --mount flag is useful to have Nignx print out the hostname of the node its running on, will try this out at the next article.<br><br>


  - After run the application list see its status by using:

```bash
docker service ls
```
<br>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjwzbnow3osj4pw48h4n.png)
<br><br>

  - To take deeper look at the running tasks use:

```bash
docker service ps nginx1
```
<br>

This will print out more info about this application including the node that host this app.

> mention here that you can land on this node and simple run the normal node command: `docker container ls`

<br>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwnbdeq9vv6rafuld2y1.png)


  - Let's Test the app service, go to another node _-that not host the service-_ and run:

```bash
curl localhost:80
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eiv3ymxsqwo71enwd0od.png)

---------------------------------------------------
<br><br>

That's it, Hope this article inspired you and will appreciate your feedback. Thank you.