How to set up a Docker Swarm with three nodes
We'll assume you have Docker installed on all three machines following the steps outlined in the previous blog post. For clarity, let's designate the nodes as follows:
Node 1: Will be our initial Swarm Manager (IP Address: MANAGER_IP_1)
Node 2: Will be a Worker Node (IP Address: WORKER_IP_2)
Node 3: Will be another Worker Node (IP Address: WORKER_IP_3)
Step-by-Step Guide:
1. Initialize the Swarm on the First Manager Node (Node 1):
- Open your terminal or SSH into Node 1.
- Execute the docker swarm init command, specifying the advertisement address for the manager. This IP address should be reachable by the other nodes in your network.
- Bash
docker swarm init --advertise-addr
- Replace with the actual IP address of Node 1's network interface that the other nodes will use to communicate with it. For example, if Node 1's IP address on your internal network is 192.168.1.10, the command would be:
- Bash
docker swarm init --advertise-addr 192.168.1.10
- Upon successful initialization, Docker will output a docker swarm join command that worker nodes can use to join the Swarm. Carefully copy this entire command. It will look similar to this:
- Bash
docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy :2377 The SWMTKN-1-... part is the worker join token, and :2377 specifies the manager's address and the default Swarm management port.
2. Join the First Worker Node (Node 2) to the Swarm:
Open your terminal or SSH into Node 2.
Paste and execute the docker swarm join command that you copied from Node 1.
- Bash
docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy :2377
- Ensure that Node 2 can network-wise reach Node 1 on the specified IP address and port (default: TCP port 2377). Firewalls on Node 1 might need to be configured to allow incoming traffic on this port.
- If the join is successful, you should see a message indicating that the node has joined the Swarm as a worker.
3. Join the Second Worker Node (Node 3) to the Swarm:
- Open your terminal or SSH into Node 3.
- Again, paste and execute the same docker swarm join command that you copied from Node 1.
- Bash
docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy :2377
- Similarly, ensure that Node 3 can communicate with Node 1 on the specified IP address and port.
- A successful join will be indicated by a confirmation message.
4. Verify the Swarm Setup (on the Manager Node - Node 1):
- Go back to the terminal on Node 1 (the manager node).
- Execute the following command to list the nodes in the Swarm:
- Bash
docker node ls
- HOSTNAME: The hostname of the Docker daemon on the node.
- STATUS: The current status of the node (e.g., Ready, Down).
- AVAILABILITY: Indicates if the node is available to schedule tasks (Active, Drain).
- MANAGER STATUS: For manager nodes, it shows their role (Leader, Reachable, Unreachable). In our case, node1 should show as Leader (the initial manager). Worker nodes will have this column empty.
- REACHABILITY: Indicates if the manager can reach the node.
- Optional: Adding Another Manager Node (Highly Recommended for Production):
- While we initially aimed for two worker nodes, adding another manager provides fault tolerance. Let's say you want to make Node 2 a manager as well.
- Get the Manager Join Token (on Node 1):
- Bash
docker swarm join-token manager
- This will output a specific join command for adding manager nodes. Copy this command.
- Join Node 2 as a Manager (on Node 2):
- Open your terminal on Node 2.
- Paste and execute the manager join command you copied from Node 1.
- Bash
docker swarm join --token SWMTKN-1-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA-BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB :2377 Ensure Node 2 can communicate with Node 1.
- Verify the Updated Swarm (on Node 1):
- Bash
docker node ls
- Now you should see Node 2 with a MANAGER STATUS of Reachable or Leader.
Important Considerations:
- Network Connectivity: Ensure that all three nodes can communicate with each other over the network, especially on TCP port 2377 (for Swarm management) and any ports your applications might expose. Firewalls might need configuration.
- Unique Hostnames: It's good practice to have unique hostnames for each node in your Swarm for easier identification.
- IP Addresses: Use stable IP addresses for your manager nodes, especially the one you use to initialize the Swarm. Dynamic IP addresses can cause issues.
- Security: In a production environment, you should configure TLS encryption for communication between Swarm nodes to secure your cluster.
By following these steps, you will have successfully set up a Docker Swarm with three nodes (one manager and two workers). You can now start deploying and managing containerized applications across this distributed environment using Docker Swarm commands. Remember to explore further commands like docker service create, docker service scale, and docker stack deploy to leverage the full power of Docker Swarm.
|