top of page

Setup Nginx in a Docker Swarm

Docker Swarm is a container orchestration platform that allows you to manage and scale Docker containers across multiple nodes. In this blog, we're going to understand how to Setup Nginx in a Docker Swarm.

Here's a basic guide on how you can setup Nginx in a Docker Swarm:


Step 1: Initialize Docker Swarm

If you haven't already, initialize Docker Swarm on your manager node:

[root@siddhesh ~]# docker swarm init
 Swarm initialized: current node (m14h183kq72mnfkdpmbrkhj74) is now a manager.

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

     docker swarm join \
     --token SWMTKN-1-1ljk1feoqha1aqqu23xfegronyvp2zujxrlonad3iet1qfdttz-76st5axosgwj00tp9kcv3k2q6 \
     192.168.1.2:2377

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

 [root@siddhesh ~]#

This command will provide you with a token that you can use to join worker nodes to the swarm.


Step 2: Deploy Nginx Service


Create a Docker service for Nginx. You can customize the Nginx configuration by mounting a volume or providing a custom configuration file.

[root@siddhesh ~]# docker service create --name nginx-service -p 80:80 nginx:latest
 0u0nnvxg4k4tu1q6swcewfv21
[root@siddhesh ~]#

This command deploys an Nginx service with the name "nginx" and publishes port 80 on the host to port 80 on the service.


Step 3: Scale Nginx Service

You can scale the Nginx service to run on multiple nodes:

[root@siddhesh ~]# docker service scale nginx-service=3
 nginx-service scaled to 3
[root@siddhesh ~]#

This command scales the Nginx service to run on three replicas. Adjust the number according to your requirements.


Step 4: Check Service Status


Check the status of your service:

[root@siddhesh ~]# docker service ls
 ID            NAME           MODE        REPLICAS  IMAGE
 0u0nnvxg4k4t  nginx-service  replicated  3/3       nginx:latest
[root@siddhesh ~]#

Where,

ID: The unique identifier for the Docker service. In this case, the service has the ID 0u0nnvxg4k4t.


NAME: The name given to the service. Here, it's named nginx-service.


MODE: This column indicates the mode in which the service is running. In your case, it is set to replicated, which means that the service is running in replicated mode. In replicated mode, the service is scaled to the specified number of replicas across the nodes in the Docker Swarm.


REPLICAS: Indicates the current number of running replicas and the desired number of replicas. In your case, it shows 3/3, which means that there are currently three replicas running, and the desired number of replicas is also set to three. This indicates that the service is fully scaled to the desired number of instances.


IMAGE: Specifies the Docker image used by the service. Here, it's using the nginx:latest image, indicating that the service is running Nginx based on the latest version of the Nginx image available on Docker Hub.


Step 5: Access Nginx

You can access Nginx through any of the nodes in your swarm by using the public IP or hostname of a manager node. If you've scaled the service, it will load balance requests across the replicas.

Docker Swarm

Step 6: List the nodes in a Docker Swarm

[root@siddhesh ~]# docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
u5w5bxl60acappacs0u30w9bu *  siddhesh  Ready   Active        Leader
[root@siddhesh ~]#

In the above output:


The node has the ID u5w5bxl60acappacs0u30w9bu.

The hostname is siddhesh.

The node is in "Ready" status, meaning it is ready to run containers.

The availability is "Active," indicating the node is actively participating in the swarm.

The manager status is "Leader," showing that this node is the leader in the swarm.


Step 7: List Running Tasks

[root@siddhesh ~]# docker node ps u5w5bxl60acappacs0u30w9bu
ID            NAME             IMAGE         NODE      DESIRED STATE  CURRENT STATE           ERROR  PORTS
8pangywrrjez  nginx-service.1  nginx:latest  siddhesh  Running        Running 10 minutes ago
fbhop0a2tarr  nginx-service.2  nginx:latest  siddhesh  Running        Running 10 minutes ago
wbu2tj9umgwi  nginx-service.3  nginx:latest  siddhesh  Running        Running 10 minutes ago
[root@siddhesh ~]#

In the above output:


Three tasks are listed, each with a unique ID.

The tasks are part of the "nginx-service" service.

The tasks are running on the node with the hostname "siddhesh."

All tasks are in the "Running" state, indicating they are currently active and running the Nginx service.

There are no errors reported for any of the tasks.

The tasks have been running for 10 minutes.

Step 8: Leave the Swarm

[root@siddhesh ~]# docker swarm leave --force
Node left the swarm.
[root@siddhesh ~]#

This command is used to force a node to leave a Docker Swarm.


The docker swarm leave --force command is used to forcefully make a node leave the Docker Swarm. The --force option ensures that the node leaves the swarm without requiring confirmation.


When a node leaves the swarm, it means that it is no longer part of the Docker Swarm cluster. This can be intentional, for example, if you are decommissioning a node or if there are issues with the node.


The message "Node left the swarm" confirms that the operation was successful.

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page