Docker Swarm Series: #1st Setup the Environment
Tags: docker, swarm
Published: 2023-06-21 09:37:19

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

# Introduction to Container Orchestration<br><br>

Assuming that we have a containerized Python application and wanna to **deploy** on **production**. However to deploy on production we have some **prerequisites** to handle first:<br>

  - service discovery, *(i.e. connections between containers)*.<br>

  - Auto Scaling.<br>

  - High availability and fault tolerance.<br>

  With Traditional servers *(i.e. virtual machines)* All of these prerequisites was handled by bunch of tools (e.g. Vmware,) But **how to achieve that while using containers!?**
   
Here's the value behind **The Container orchestration tools**, Container orchestration is the process of **managing** and **deploying containers**, Orchestration helps to automate the deployment, scaling, and management of containerized applications.

Containers provide a lightweight and portable way to package and deploy applications, but managing them at scale can be challenging. Container orchestration tools help to **simplify this process** by providing a platform for deploying and managing containers across **multiple hosts**.<br><br>

--------------------------------------------------------------------

# Orchestration tools typically provide the following features:<br>

  - Service discovery: Containers need to communicate with each other, and orchestration tools provide a way to automatically discover other containers in the cluster.<br><br>

  - Load balancing: Orchestration tools can automatically distribute incoming traffic across multiple containers, ensuring that the workload is evenly distributed.<br><br>

  - Scaling: Orchestration tools can automatically scale the number of containers up or down based on the demand for the application.<br><br>

  - Health monitoring: Orchestration tools can monitor the health of containers and automatically restart them if they fail.<br><br>

  - Declare desired state: Orchestration tools will work to compare the desired state -*that your provided*- with the actual state -*The real state of the running applications*- and works to ensure that the actual state matches that desired state. 

Some popular container orchestration tools include **Docker Swarm**, **Kubernetes**, **Mesos**. Each tool has its own strengths and weaknesses, and the choice of tool will depend on your specific requirements.<br><br>

--------------------------------------------------------------------

## Container ecosystem layers:<br>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x8ymxvz3vva7gyw0szl5.png)
*The image provided by IBM docker essentials course*

In this series will be using Docker swarm, Docker Swarm is a powerful and **easy-to-use** tool for managing containers at scale, and it has become a popular choice for organizations looking to deploy containerized applications in production environments.<br><br>

--------------------------------------------------------------------

# Docker swarm Overview<br><br>

Docker swarm is a container orchestration tool That allows you to Create, Deploy, Scale, and manage Cluster of Docker Hosts using declarative configuration file called Docker Compose or DockerCLI.<br>

**Docker swarm** As Defined by [Docker Docs](https://docs.docker.com/engine/swarm/key-concepts/#what-is-a-swarm) *"The cluster management and orchestration features **embedded in the Docker Engine** are built using swarmkit, Swarmkit is a separate project which implements Docker’s orchestration layer and is used directly within Docker."* Docker Swarm is the Docker-native container orchestration platform that uses SwarmKit as its core library, **That means you don't need any extra installation.**


**Docker swarm** is consists of two main **components**: Manager nodes, and worker Nodes, The Manager node server is responsible on managing the entire cluster and schedule the tasks, While The Worker node is the host that run the container; That means we must have couple of servers to initiate the Swarm cluster., And that is the idea to deliver high availability to your applications<br><br>

Swarm provide very powerful features with self managed and easy to use, Press [Feature highlights](https://docs.docker.com/engine/swarm/#feature-highlights) for more.<br><br>

kindly consider to assign a static IP for Nodes, Also consider [Open protocols and ports between the hosts](https://docs.docker.com/engine/swarm/swarm-tutorial/#open-protocols-and-ports-between-the-hosts)

The following ports must be available. On some systems, these ports are open by default.

  - Port 2377 TCP for communication with and between manager nodes
  - Port 7946 TCP/UDP for overlay network node discovery
  - Port 4789 UDP (configurable) for overlay network traffic<br><br><br><br>


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

# Setup the environment<br><br>

To initiate the Swarm Cluster we need couple of servers which is hard to achieve for learning purpose, In this lab will going to use [Play with Docker](https://labs.play-with-docker.com/#), Play with docker is provided by Docker Inc to provide your the ability to initiate Nodes that have docker preinstalled and ready to use.<br>

Enough talking, Down we go!!
<br>


**Setup the Environment:**<br>
  - Press to open [Play with docker](https://labs.play-with-docker.com/#)
  - Sign-in with your docker account.
  - Press Start, to start creating your workspace.
  
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtbh5r001thw2ma3cdox.jpg)<br>

  - This will open workspace like the below, Press Add new instance to initiate a server.
 

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

  - Initiate Swarm Mode:

```bash 
# get the NIC name "network interface card"
# in our case its eth0
ip a s

# initiate docker swarm
$ docker swarm init --advertise-addr eth0

```

  - This will print out the below:

```bash
Swarm initialized: current node (yz3vrc2w1hwnrwkr5dfsctxkj) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-4xj2egkkxq8ofqkeg0s3zblrdpzcpqokgjyl5zpc1pja100641-3eqtbf6doialoa2spbr1o4dp0 192.168.0.28:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

```
*I think it's defined well*

  - Now, Let's create Two instances and join them to our Swarm cluster.


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

  - Press Node2, Copy and past the command provided by Swarm initiation

```bash
docker swarm join --token SWMTKN-1-4xj2egkkxq8ofqkeg0s3zblrdpzcpqokgjyl5zpc1pja100641-3eqtbf6doialoa2spbr1o4dp0 192.168.0.28:2377
```


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

  - Do the same on Node3


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



  - Here we go, list worker nodes:
    - Press node1 which is the manager node, and type the below.

```bash
 docker node ls 
```


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



here we have three nodes with Ready status, Active availability, and one act as leader "Manager".
The asterisk means this node1 it's the node that handle this command``` docker node ls```

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

# The Underline information <br><br>
  - Container orchestration tools help to automate the deployment, scaling, high availability, and management of containerized applications.<br><br>

  - Orchestration tools typically provide Service discovery, Load balancing, Scaling, Health monitoring, Declare desired state.<br><br>

  - Docker Swarm is a powerful and easy-to-use tool for managing containers.<br><br>

  - initiate Swarm mode Dose not need any extra installation.<br><br>

  - Docker swarm is consists of two main components: Manager nodes, and worker Nodes, The Manager node server is responsible on managing the entire cluster and schedule the tasks, While The Worker node is the host that run the container.<br><br>

  - use this command to initiate the swarm mode: 
```bash
docker swarm init --advertise-addr eth0
```<br><br>

  - use this command on the manager node to list the node:
```bash
docker node ls
```<br><br>





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


That's it, Very straightforward, very fast🚀. 
Hope this article inspired you and will appreciate your feedback. Thank you.