How to create an nginx image file, and then use docker-compse to start nginx and Tomcat together. The application is deployed on both Tomcats. The user accesses the machine where Nginx is located, and Nginx forwards the request to Tomcat001 or Tomcat002:
Nginx load balancing is implemented by configuring nginx.conf.
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; #include /etc/nginx/conf.d/*.conf; upstream tomcat_client { server t01:8080 weight=1; server t02:8080 weight=1; } server { server_name ""; listen 80 default_server; listen [::]:80 default_server ipv6only=on; location / { proxy_pass http://tomcat_client; proxy_redirect default; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }}}Copy the code
Proxy_pass http://tomcat_client; proxy_pass http://tomcat_client; Tomcat_client is configured to handle the request:
upstream tomcat_client {
server t01:8080 weight=1;
server t02:8080 weight=1;
} Copy the code
That is, two servers with URLS T01:8080 and T02:8080 are given, and each server handles a weight of 1. What are t01:8080 and T02:8080? This is an alias that matches the alias of the link parameter. This is an alias that matches the alias of the link parameter. This is an alias that matches the link parameter.
# First Docker file from BolingCavalry # VERSION 0.0.1 # Author: Bolingcavalry # Base image FROM nginx:stable # Author MAINTAINER BolingCavalry <[email protected]> # Define working directory ENV WORK_PATH /etc/nginx RUN rm $WORK_PATH/$CONF_FILE_NAME /$CONF_FILE_NAME /$CONF_FILE_NAME $WORK_PATH/ # RUN chmod a+r $WORK_PATH/$CONF_FILE_NAMECopy the code
As shown above, the Dockerfile file is very simple, just delete the original system nginx.conf file and replace it with our own file.
Now create a new directory, image_nginx. This directory contains only two files, nginx.conf and Dockerfile, as shown below:
Open the terminal, go to this directory, and execute the command line:
Docker build -t bolingcavalrynginx:0.0.1.Copy the code
After executing the docker images file, you can see the new image file as shown below:
For tomcat image, please directly use the previous article “Actual Docker, write Dockerfile customized Tomcat image, achieve online Deployment of Web applications” through the Makefile customized Tomcat image, the advantages of this image are: After the deployment is complete, you can use maven plug-in to deploy application packages to the Tomcat container to verify the load balancing capability.
Ok, now that we have the image of Nginx and Tomcat, what we need to do is to run one nginx container and run two Tomcat containers for load balancing. The three containers can be implemented in one of the following ways: 1. Execute three Docker run commands to start three containers; 2. Use Docker compose to batch start multiple containers;
1. Start the first Tomcat container, name it Tomcat001, and enter:
Docker run - name = tomcat001 -p 8081:8080 - e TOMCAT_SERVER_ID = tomcat_server_001 - idt bolingcavalrytomcat: 0.0.1Copy the code
- Start the second Tomcat container, name it Tomcat002, and enter:
Docker run - name = tomcat002 -p 8082:8080 - e TOMCAT_SERVER_ID = tomcat_server_002 - idt bolingcavalrytomcat: 0.0.1Copy the code
- When docker ps is executed, you can see that two containers have been started, as shown in the following figure:
Localhost :8081 / localhost:8082 / localhost:8082 / localhost:8081 / localhost:8082 The diagram below:
- Remember the last article”Actual Docker, write Dockerfile customized Tomcat image, realize online Deployment of Web applications”Using Maven to deploy war packages to Tomcat? Follow the steps in this article to download the code from Git and then modify the parameters in pom.xml, as shown below:
After changing the port in the red box to 8081, execute in the directory where pom.xml is located:
mvn clean package -U -Dmaven.test.skip=true tomcat7:redeployCopy the code
And then change the port to 8082 to perform the same command, so you can put the war file is deployed to the two respectively on tomcat container, this time using the browser to http://localhost:8081/loadbalancedemo/hello you can see below:
The effect of http://localhost:8082/loadbalancedemo/hello are as follows:
- To start nginx, enter the following command on the terminal:
Docker run --name=ngx001 --link=tomcat001: T01 --link=tomcat002: T02 -P 80:80-IDT bolingcavalrynginx:0.0.1Copy the code
— link=tomcat001:t01 — link indicates that the container ngx001 started with the current command should be connected to another container named Tomcat001. T01 in tomcat001: T01 indicates that T01 is the alias of Tomcat001 after the connection is established. It can also be read as follows: After ngx001 is started, a record is added to /etc/host. The IP address is the IP address of Tomcat001 and the name is T01.
Docke exec it ngx001 /bin/bash docke exec it ngx001 /bin/bash
In the ngx001 container, everything accessing t01 is actually accessing the same IP address as Tomcat001.
upstream tomcat_client {
server t01:8080 weight=1;
server t02:8080 weight=1;
} Copy the code
T01 and T02 correspond to t01 and t02 in the link parameter, so that nginx can forward requests using T01 as the domain name to tomcat001 and Tomcat002.
So far, we have implemented the load balancing effect of Nginx + Tomcat, but it seems to be troublesome to start three containers and execute three commands. Is there any way to execute commands in batches? Write your own shell and put all your commands in it? Well, that’s fine, but what about other batch operations? For example, stop, restore, build images, view messages, etc., so using compose is a better choice. Compose is a tool for defining and running complex Docker applications that can process multiple containers in batches.
Docker-comemage. yml: docker-comemage. yml: docker-comemage. yml
Version: '2' services: nginx001: image: bolingcavalrynginx:0.0.1 Links: - Tomcat001: T01 - Tomcat002: T02 Ports: - "80-80" restart: always tomcat001: image: bolingcavalrytomcat: 0.0.1 ports: - "8081-8080" the environment: TOMCAT_SERVER_ID: tomcat_server_001 restart: always tomcat002: image: bolingcavalrytomcat: 0.0.1 ports: - "8082:8080" environment: TOMCAT_SERVER_ID: tomcat_server_002 restart: alwaysCopy the code
Each container is defined as a node with a specific parameter key-value pair, which contains the parameters we carried with the Docker Run.
Now you can try to execute docker-comemage. yml. Before executing, please execute the following command to stop and delete the three containers we started earlier:
docker stop tomcat001 tomcat002 ngx001; docker rm tomcat001 tomcat002 ngx001Copy the code
Then go to the directory where the docker-comemess. yml file is located and execute the following command:
docker-compose up -dCopy the code
After the command is executed, run docker ps to view the current container information:
The container has been started once, but the name of the container has been changed, and the prefix has been added. The prefix is related to the current directory.
Skip =true tomcat7:redeploy “MVN clean package -u-dmaven.test. skip=true tomcat7:redeploy” To access the “http://localhost/loadbalancedemo/hello” to verify whether nginx request distribution to different tomcat.
Docker compose is composed for link and docker compose server load balancing, which is composed for Link and Docker compose server load balancing. You can also use the -v parameter to mount the actual directory of the current computer to the Tomcat webapps directory during Docker run. Both methods are easy to implement. If you are interesting, you can try them.