Step 1: Install Docker
Make sure Docker is installed on all machines that will be part of the Swarm cluster. You can follow the official Docker installation guide for your operating system.
Step 2: Initialize the Swarm
Choose one machine to be the Swarm manager and run the following command to initialize the Swarm:
docker swarm init --advertise-addr <MANAGER-IP>
Replace <MANAGER-IP>
with the IP address of the manager node. This command will initialize the Swarm and provide a token to join other nodes.
choose your manager-ip using ip add
command.
Step 3: Join Worker Nodes (optional)
If you have additional worker nodes that you want to include in your Docker Swarm cluster, you can join them by running the following command on each worker node:
docker swarm join --token <worker_token> <manager_ip>:<manager_port>
Replace <worker_token>
with the worker token generated during Docker Swarm initialization and <manager_ip>:<manager_port>
with the IP address and port of the Swarm manager node.
Step 4: Check your worker node and manger node token (optional)
If you have additional worker nodes you'd like to include in your Docker Swarm cluster, you can join them. Run the following command on each worker node to obtain the worker token:
docker swarm join-token worker
Similarly, to join manager nodes to the Docker Swarm cluster, run the following command on each manager node to obtain the manager token:
docker swarm join-token manager
Step 5: Check Swarm Status
To verify that all nodes have successfully joined the Swarm, run the following command on the manager node:
docker node ls
Step 6: Check if Docker Swarm is Active
Before deploying NGINX on Docker Swarm, let's verify whether Docker Swarm is active. Open a terminal and run:
docker info | grep -i "Swarm : active"
If the output shows "Swarm: active", Docker Swarm is active and you can proceed to the next step. If not, initialize Docker Swarm using docker swarm init
.
Step 7: Create NGINX service
To deploy NGINX on Docker Swarm, we'll create a Docker service using the docker service create
command. This command will specify the NGINX image and define port mapping.
docker service create --name nginx-service -p 8080:80 nginx:latest
Step 8: Verify NGINX Service
To verify that the NGINX service is running, execute the following command:
docker service ls
You should see the newly created NGINX service listed in the output.
Step 9: Access NGINX in a Web Browser
Open a web browser and navigate to http://<your_docker_host_ip>:8080
. If Docker is running locally, you can use http://localhost:8080
.
You should see the default NGINX landing page.
Step 10: Scale the NGINX Service (Optional)
If you want to scale the NGINX service to run multiple replicas, you can use the docker service scale
command. For example, to scale to 3 replicas:
docker service scale nginx-service=3
Step 11: Update NGINX Service (Optional)
To update the NGINX service, such as changing the image version or updating configuration, use the docker service update
command. For example:
docker service update --image nginx:newversion nginx-service
Step 12: Clean Up
If you want to remove the NGINX service, use the docker service rm
command:
docker service rm nginx-service
Conclusion:
we've learned how to deploy an NGINX server on Docker Swarm. By following these steps, you can easily deploy and manage NGINX services on Docker Swarm, ensuring scalability and reliability for your containerized applications.