Docker Swarm Setup on a VPS
This markdown guide provides a step-by-step process for setting up Docker Swarm on a VPS. It covers initializing a Swarm cluster, adding worker nodes, verifying the cluster, and deploying services across the nodes. Additionally, it includes optional steps for managing nodes and scaling services. Ideal for users looking to create a scalable, distributed system using Docker Swarm.
Prerequisites
- Docker Installed on all VPS nodes (both manager and worker nodes). If Docker is not installed, follow the installation steps.
- Multiple VPS Nodes (minimum 2: one as the manager and one or more as worker nodes).
- Ensure Communication Between Nodes: All nodes must be able to communicate over the network.
Step 1: Initialize Docker Swarm on the Manager Node
Login to the manager VPS where you want to initialize the Swarm.
Initialize the swarm with the following command:
sudo docker swarm init --advertise-addr <MANAGER-IP>Replace
<MANAGER-IP>with the public or private IP address of your manager node.After initializing, Docker will provide you with a command that looks like this:
docker swarm join --token <TOKEN> <MANAGER-IP>:2377This command is what you'll need to run on each worker node to join the swarm.
Step 2: Join Worker Nodes to the Swarm
Login to each worker VPS.
Run the command provided by the manager node in Step 1. It will look like this:
sudo docker swarm join --token <TOKEN> <MANAGER-IP>:2377- Replace
<TOKEN>with the token from the manager node. - Replace
<MANAGER-IP>with the manager's IP address.
After running this command, the worker nodes will join the swarm.
- Replace
Step 3: Verify the Swarm Cluster
On the manager node, run the following command to see the list of nodes in the swarm:
sudo docker node lsYou should see the manager and all worker nodes listed.
Step 4: Deploy Services in the Swarm
To deploy services in the Docker Swarm cluster, you can use the docker service command. Here is a simple example of running an Nginx service in the swarm.
sudo docker service create --name my-nginx --replicas 3 -p 80:80 nginx
This command creates a service called my-nginx with 3 replicas, exposing port 80. Docker will automatically distribute the replicas across the nodes in your Swarm.
Additional Steps (Optional)
1. Promote Worker to Manager (Optional):
If you want to promote one of the worker nodes to a manager, you can do so with the following command on the manager node:
sudo docker node promote <WORKER-NODE-ID>
2. Remove a Node from the Swarm:
If you need to remove a node from the swarm, run the following on the node to be removed:
sudo docker swarm leave
Forcing a manager node to leave the swarm can be done like this:
sudo docker swarm leave --force
Step 5: Manage Services and Stacks (Optional)
To manage services and scale them, you can use commands like:
- Scale a service:
sudo docker service scale my-nginx=5
- Remove a service:
sudo docker service rm my-nginx
With these steps, Docker Swarm should be set up and running on your VPS cluster.