Home >Operation and Maintenance >Linux Operation and Maintenance >How to use Docker to manage and expand multi-node clusters

How to use Docker to manage and expand multi-node clusters

WBOY
WBOYOriginal
2023-11-07 10:06:581258browse

How to use Docker to manage and expand multi-node clusters

In today's cloud computing era, containerization technology has become one of the most popular technologies in the open source world. The emergence of Docker has made cloud computing more convenient and efficient, and has become an indispensable tool for developers and operation and maintenance personnel. The application of multi-node cluster technology is widely used based on Docker. Through multi-node cluster deployment, we can utilize resources more efficiently, improve reliability and scalability, and also be more flexible in deployment and management. Next, we will introduce how to use Docker to manage and expand multi-node clusters.

Step one: Build a multi-node cluster environment
In order to achieve the management and expansion of a multi-node cluster, we need to build multiple nodes first. In this article we use Docker Swarm technology, which can help us manage multi-node clusters more conveniently.

1) Install Docker and Docker Compose on multiple machines and ensure that they can communicate with each other.

2) Select one of the machines as the "management node" and run the following command:

docker swarm init --advertise-addr <管理节点IP>

After execution, a token will be returned. This token is used to add other nodes to this in Swarm. At the same time, after executing this command, your local machine becomes the management node of Swarm.

3) Add other machines to Swarm. Execute the following command on other machines:

docker swarm join --token <token> <管理节点IP>:2377

At this time, other machines will be added to Swarm. We can use the following command to view the nodes in Swarm:

docker node ls

Step 2: Build the service and expand the capacity
After setting up a multi-node cluster environment, we can start building the service. A service refers to a group of container instances running in Swarm. They share host, network and storage resources and can automatically scale in a horizontal expansion manner. Here, we take a simple web application as an example to introduce how to build and expand services.

1) Write Dockerfile. First, we need to write a Dockerfile file, specify the application's base image and running commands, etc.

FROM node:latest
WORKDIR /app
COPY . /app
RUN npm install
CMD ["npm", "start"]

Among them, we chose node:latest as the base image, added our application files to the /app directory, installed dependencies and started the program.

2) Write docker-compose.yml. docker-compose is a tool for defining and running multi-container Docker applications. Here, we can use docker-compose to define a service called web.

version: '3'
services:
  web:
    build: .
    ports:
      - "8080:8080"
    environment:
      - NODE_ENV=production
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure

This docker-compose file specifies our application building method, port mapping, environment variables, number of copies, resource limits, etc. In step three we will use the docker stack deploy command to build and run the service.

3) Build and expand services. After completing the above two steps, we can start building and scaling our service. Please execute the following command:

docker-compose build
docker stack deploy --compose-file docker-compose.yml web

This command will deploy our service into Swarm and specify 5 instances. Visit http://:8080 in the browser, and you can view our web application.

Step Three: Management and Expansion Service
After successfully building and deploying the service, we need to manage and expand it. Service management and expansion in Swarm is very simple, you only need to execute a few commands.

1) Check the service status. We can use the following command to view the status of the service:

docker service ls
docker service ps web

This command will display all running services and service instances. At this point, we should see 5 web service instances running.

2) Expansion service. We can use the following command to expand the number of instances of the service:

docker service scale web=8

This command will increase the number of instances of the web service to 8.

3) Update service. In order to update the service, we need to modify the previous docker-compose.yml file and run the docker stack deploy command again. After the modification is completed, execute the following command to update the service:

docker stack deploy --compose-file docker-compose.yml web

Summary
Multi-node cluster technology is a very practical technology used in Docker, which can help us manage and expand our applications more conveniently . In this article, we introduce how to use Docker Swarm to build a multi-node cluster environment, and use a web application as an example to demonstrate how to build, deploy, manage and expand services. I believe that through the introduction of this article, everyone has a deeper understanding of Docker Swarm multi-node cluster technology.

The above is the detailed content of How to use Docker to manage and expand multi-node clusters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn