Detailed guide to containerized deployment and cluster management of Nginx server

PHPz
Release: 2023-08-06 11:03:15
Original
1690 people have browsed it

Detailed Guide to Containerized Deployment and Cluster Management of Nginx Server

Introduction:
With the development of cloud computing and container technology, containerized deployment has become a common way of enterprise application development and deployment. As a high-performance web server and reverse proxy server, Nginx can also be deployed and managed through containerization. This article will introduce in detail how to containerize the Nginx server and improve high availability through cluster management.

1. Preparation
First, we need to install the Docker environment and ensure that the Docker service is started. Next, we need to write a Dockerfile file to build the Nginx Docker image. The following is a simple Dockerfile example:

FROM nginx:latest
COPY nginx.conf /etc/nginx/nginx.conf
COPY default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Copy after login

This Dockerfile first selects the latest Nginx image as the base image, and then copies the Nginx configuration file and default virtual host configuration file we prepared in advance. Finally, port 80 of the container is exposed and the Nginx server is run in foreground mode.

2. Build the Docker image
After preparing the Dockerfile, we can use the docker build command to build the Docker image. Assuming that we save the Dockerfile in the current directory, we can build it with the following command:

docker build -t my_nginx .
Copy after login

This command will build a Docker image named my_nginx based on the Dockerfile. After the build is completed, you can use the docker images command to view the existing image list to confirm that the my_nginx image has been successfully built.

3. Run a single Nginx container
Now, we can create an Nginx container based on the my_nginx image and run it. You can use the docker run command to perform this operation:

docker run -d -p 80:80 my_nginx
Copy after login

This command will run a new Nginx container in the background and map the container's port 80 to the host's port 80. You can verify whether the Nginx server is working properly by accessing http://localhost through your browser.

4. Building an Nginx cluster
In order to improve the high availability of the Nginx server, we can use Docker's cluster management tool to build an Nginx cluster. In this article, we use Docker Swarm to implement cluster management.

First, we need to initialize a Swarm management node. You can set the current node as the Swarm management node by running the following command:

docker swarm init
Copy after login

Then, we can create two worker nodes (hosts) by running the following command:

docker swarm join-token worker
Copy after login

After running the above command , an output similar to the following will be generated:

docker swarm join --token xxxxxxxxxxxxxxxx
Copy after login
Copy after login

We need to use this output to add two worker nodes to the Swarm cluster:

docker swarm join --token xxxxxxxxxxxxxxxx
Copy after login
Copy after login

In this way, we have successfully added the two worker nodes Join the Swarm cluster. Next, we need to create an Nginx service. You can use the following command to create an Nginx service:

docker service create --name nginx --replicas 3 -p 80:80 my_nginx
Copy after login

This command will create a service named nginx in the cluster and specify 3 replicas. The service automatically creates and distributes these replicas on different nodes in the cluster, thus building an Nginx cluster. You can use the docker service ls command to view all services in the cluster and their status.

5. Cluster management operations
Once we have established the Nginx cluster, we can perform some basic cluster management operations.

  1. Expansion and reduction
    You can use the following commands to achieve expansion and reduction of the Nginx service:
docker service scale nginx=5
docker service scale nginx=2
Copy after login

The first command will be the nginx service. The number of replicas is expanded to 5, and the second command reduces the number of replicas to 2.

  1. Service update
    When we need to update the Nginx image or configuration file, we can use the following command to update the service:
docker service update --image my_nginx:latest nginx
Copy after login

This command will update The image of nginx service is the latest version. Similarly, we can also update other configuration parameters of the service through the docker service update command.

  1. Service scalability management
    You can view and manage the scalability of the service through the following command:
docker service ps nginx
docker service inspect --pretty nginx
Copy after login

The first command will display all nginx service The status and information of the replica. The second command will display the detailed information of the nginx service, including node allocation and replica running status.

Conclusion:
By containerizing the Nginx server for deployment and cluster management, we can achieve higher availability and flexibility. This article introduces in detail the use of Docker to build Nginx images, run a single container, and use Docker Swarm to build and manage Nginx clusters. I hope readers can learn about Nginx container deployment and cluster management through this article, and be able to apply and expand it in actual scenarios.

The above is the detailed content of Detailed guide to containerized deployment and cluster management of Nginx server. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!