乐闻世界logo
搜索文章和话题

How do you create a Docker Swarm cluster?

1个答案

1

Creating a Docker Swarm Cluster

Docker Swarm is Docker's native cluster management and orchestration tool. To create a Docker Swarm cluster, follow these steps:

1. Prepare the Environment

First, ensure that all participating machines have Docker Engine installed. Docker Engine version should be at least 1.12, as Docker introduced Swarm mode starting from this version.

Example: Assume we have three machines, named manager1, node1, and node2. These machines must be able to communicate with each other, preferably on the same network.

2. Initialize the Swarm Cluster

Select one machine to act as the manager node and run the docker swarm init command to initialize the Swarm cluster.

Example: On manager1, run the following command:

bash
docker swarm init --advertise-addr <MANAGER-IP>

Here, <MANAGER-IP> is the IP address of manager1. This command makes the machine a manager node.

3. Add Worker Nodes

After initializing the Swarm cluster, the docker swarm init command outputs a token to join the cluster. Use this token to run docker swarm join on other nodes to add them as worker nodes.

Example: Run on node1 and node2:

bash
docker swarm join --token <YOUR-TOKEN> <MANAGER-IP>:2377

<YOUR-TOKEN> is the token obtained from manager1, and <MANAGER-IP> is the IP address of the manager node.

4. Verify Cluster Status

Run docker node ls on the manager node to view the status of all nodes, ensuring they are active and properly connected to the Swarm cluster.

Example: On manager1, run:

bash
docker node ls

This command lists all nodes and their statuses, allowing you to see which are manager nodes and which are worker nodes.

5. Deploy Services

You can now deploy services on the Swarm cluster. Use the docker service create command to deploy.

Example: To run a simple nginx service on the cluster, run on manager1:

bash
docker service create --name my-nginx --replicas 3 --publish 80:80 nginx

This creates a service named my-nginx, deploying three replicas of nginx, and mapping port 80 to the host's port 80.

Summary

By following these steps, you can successfully create and manage a Docker Swarm cluster, and deploy and scale services on it. These are basic operations; for production environments, you also need to consider security, monitoring, and log management.

2024年8月9日 14:14 回复

你的答案