How to Install and Configure Docker Swarm on Linux.
Published: 2022-08-03 10:23:37

**Docker Swarm is a clustering tool that turns a group of Docker hosts into a single virtual server.** 
**Docker Swarm ensures availability and high performance for your application by distributing it over the number of Docker hosts inside a cluster.** 
**Docker Swarm also allows you to increase the number of container instance for the same application.** 

**Docker Swarm is made up of two main components:**

**1-Manager Nodes / Master Nodes**
**2-Worker Nodes**

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

**Manager Nodes Manager nodes are used to handle cluster management tasks such as maintaining cluster state, scheduling services, and serving swarm mode HTTP API endpoints.**
**If any Manager node dies unexpectedly, other one can pick up the tasks and restore the services to a stable state.**

**Worker Nodes Worker nodes are used to execute containers. 
You can create a swarm of one Manager node, but you cannot have a Worker node without at least one Manager node.** 
**You can also promote a worker node to be a Manager when you take a Manager node offline for maintenance.**

**To do this lab we need a master node and some workers node.**
**Lets launch 3 instance and name one of it as master and other as workers as shown below**

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

**Now install docker package in all the above instances.**

```
yum install docker* -y
systemctl start docker
systemctl enable docker
```
**Here I have used centos...**
**For Ubnuntu - apt install ..**

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

**Now install docker swarm in master node only**

```
docker swarm init
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m4o3kq1u6r3u8ezpufxk.png)
**Now this instance becomes your master node.**
**After running that command ,it  will give a token.. run that command in all your workers node**

**Worker-1**

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

**worker-2**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/220k1qgwewm6vt5i63qf.png)
**as you can see both the instances becomes worker nodes**

## How to host a website using docker file

**Ist create a docker file using vi editor inside master node**

```
vi Dockerfile
```

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

**Our docker file is ready now lets build this using command**

```
docker image build -t appimage:1 .
```

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

**It will build your docker file**

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

**Note**
**Follow the same process to deploy a website in workers node too..**
**or you can use docker registry or using docker hub push that image into docker hub and pull it in worker node and build it..**

## Launch web service in Docker Swarm

**Run a service**

```
docker service ls
```

```
docker service create --name app-server -p 80:80 appimage:1
```

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

**You can also scale up as shown below**

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

**Test Docker Swarm**
**Apache web server is now running on Manager Node. You can now access web server by pointing your web browser to the Manager Node IP**

**or Worker Node IP as shown below:**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/snbreuar85a3vl1hmc0u.png)
**Here I have attached a domain to my IP and used LetsEncryt to secure my website**.