Docker Swarm Series: #3rd Deploy a highly available Container
Tags: docker, swarm
Published: 2023-06-27 18:12:37

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


# Inception <br><br>

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

At the [last article](https://dev.to/mohamedeleraki/docker-swarm-series-2nd-create-a-highly-available-environment-4p1k) we covered How to create a highly available environment, And deployed a simple Nignx web-app using DockerCLI on [Play-with-docker](https://labs.play-with-docker.com/) lab.

In This article We will complete from the stand-up point, Will  scaling up our Container, Update the Container and roll-back again, Also will submit some production scenarios for deep understanding how Swarm deal with, in this lab will use [Play-with-docker](https://labs.play-with-docker.com/) lab and DockerCLI commands.<br>

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

After finalize Creation of The Infrastructure and collect the required info,  Today's Article will focus on The Container application section, How to scale-up and scale-down, cover the limits of routing mesh _-Load balancer of swarm-_, Rolling-Update and Rolling-back, After all will test the high availability of our environment by removing node from our cluster that host the Container and view how Swarm will handle this.<br>

----------------------------------------------------------
<br><br>
# Build the infrastructure <br><br>

At the [last article](https://dev.to/mohamedeleraki/docker-swarm-series-2nd-create-a-highly-available-environment-4p1k) we went through the hardest way of creating up our environment, However it's the appropriate way to build up your environment at any platform _(i.e. On-prem, Cloud, any where)_, Today's will go through the easiest way to build up our environment on [Play-with-docker](https://labs.play-with-docker.com/) platform.<br><br>

  - Open up [Play-with-docker](https://labs.play-with-docker.com/) platform.
  - Press on the template button as below:<br>


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

  - Let's specify the template that similar to what we used at the past articles.<br>


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

  - This will build the environment as below:<br>


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

----------------------------------------------------------
<br><br>
# Build the Past Container<br><br>


At the [last article](https://dev.to/mohamedeleraki/docker-swarm-series-2nd-create-a-highly-available-environment-4p1k) we built a Service that have an Nginx container by running the following command on any one of the manager nodes:<br>

```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><br>



And you can print out the Running services by running the following:
```bash
docker service ls
```<br>


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

----------------------------------------------------------
<br><br>
# Scale-up and Down <br><br>

Till Now we didn't deploy a highly available container, Let's try out to scale our container with extra 6 replicas by running the following:<br>

```bash
docker service update --replicas 6 --detach=true nginx1
```<br><br>



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


When this command is run the following events occur:
  - The service state is update with 6 replicas, Which in stored in Swarm internal storage.
  - Swarm recognize that the number of replicas that is scheduled now Does not equal The Actual state.
  - Swarm Schedule a new task with 6 replicas to match the desired state.



<br><br>
Use the following commands to discover the deployed service:<br>

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

```bash
docker service ps nginx1
```


<br><br>
Use the update command to **Scale-Down** as well, By running the following Will Scale-Down the replicas to be 5 instead of 6:

<br>
```bash
docker service update --replicas 5 --detach=true nginx1
```


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


----------------------------------------------------------
<br><br>
# Routing mesh limits <br><br>


Now on our scenario, When you send a request on port 80, The Routing mesh has multiple containers _-working on this port-_ in which to route requests to, Routing mesh act as a load balancer for these containers, Therefor the Routing mesh will send the coming requests randomly to these containers.

<br>
let's test this out by running the following multiple times on any Node:

<br>
```bash
curl localhost:80
```


<br>
You should see which node is serving each request because of the useful `--mount` command you used earlier.

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


<br><br>
Another easy way to check which node is serving the requests is to checking the **aggregated** logs by using the `docker service logs [service-name]` command. This aggregates the output from every running container and get the output from `docker container logs [container-name]` command.

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

<br><br>
**Limits of the routing mesh:** The routing mesh can publish only one service on port 80. If you want multiple services exposed on port 80, you can use an external application load balancer outside of the swarm to accomplish this.


----------------------------------------------------------
<br><br>
# Rolling update and Rolling back<br><br>


Now you have your service deployed and running successfully, and you're going to update the version of Nginx container. You want to update the pulled image and the deployed container that use this image version, with `Docker service update` command you can update the pulled image and the service as well.

<br>
Let's update our Nginx service to version 1.13 by using the following:

<br>
```bash
docker service update --image nginx:1.13 --detach=true nginx1
```

<br>
This triggers a rolling update of the swarm, Run the following command to view the updates in real time:

```bash
# using watch command
watch -n 1 docker service ps nginx1

# Or you can run this command over and over again
docker service ps nginx1
```

<br>
You can fine-tune the rolling update by using these options:
  - `--update-parallelism`: specifies the number of containers to update immediately (defaults to 1).
  - `--update-delay`: specifies the delay between finishing updating a set of containers before moving on to the next set.

<br>
After a few seconds you figure out that your Nginx application has update successfully and the old version has shunted-down.

<br>

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



<br>
## roll-back

What if find out that the latest update have an issue and your application didn't work as expected and you want to roll-back you can achieve that by running the following:

```bash
# roll back
docker service rollback nginx1

# remove the in useful services
docker service rm 
```

----------------------------------------------------------
<br><br>
# Test the Swarm environment high available functionality <br><br>

Now we have our services running on the distrusted nodes, What if a node with it's containers hosted goes down, In this section will test the Swarm high availability Function by make the node 4 leaving the Swarm cluster.

<br>
  - Go to node 1 for example and run the following command to watch the cluster change in real time:

```bash
watch -n 1 docker service ps nginx2
```

  - Now, land on node 4 and run the following to leave the cluster:

```bash
docker swarm leave
```

  - Get back to node 1 and Note that the Swarm will rebuild the containers in another node to match the desired state.

<br>

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



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