How to use Docker to build a tomcat cluster using nginx (with pictures and text)

How to use Docker to build a tomcat cluster using nginx (with pictures and text)

First, create a tomcat folder. To facilitate the configuration of docker, I will create it directly in the root directory. Step 1: Create a folder: Publish folder

mkdir -p /docker/tomcat/webapp8081

mkdir -p /docker/tomcat/webapp8082

mkdir -p /docker/tomcat/webapp8083 

Step 2: Create a Tomcat container (the port can be changed according to your actual situation)

docker run -d --name tomcat8081 -p 8081:8080 -v /docker/tomcat/webapp8081:/usr/local/tomcat/webapps/ tomcat
docker run -d --name tomcat8082 -p 8082:8080 -v /docker/tomcat/webapp8082:/usr/local/tomcat/webapps/ tomcat
docker run -d --name tomcat8083 -p 8083:8080 -v /docker/tomcat/webapp8083:/usr/local/tomcat/webapps/ tomcat

After the creation is complete, use the docker ps command to check whether the creation is successful and use

Step 3: Check the IP address of Tomcat. Use the following commands to query the IP address of Tomcat. Here we only use the first example.

docker inspect tomcat8081

Step 4: For the convenience of testing, I will not upload the war package here, but directly create a hello/index.html file in it

Note: If Nginx is a Docker container, you must use the Tomact container IP, otherwise you will not be able to connect

First download the official version of nginx from the official website

Official website: http://nginx.org/en/

Click download in the right navigation bar, enter the download interface and select the corresponding version to download. I will use nginx-1.6.2.tar here.

After the download is complete, put the file in a custom folder, I put it in /usr/local/tools/nginx-1.6.2

Unzip nginx using this command:

tar vxf nginx-1.6.2.tar.gz

After the decompression is complete, I return to the root directory and create a host folder in the root directory to create files so that nginx can mount (you can also customize it)

Create a host folder here

mkdir -p /docker/nginx/
vim /docker/nginx/nginx.conf
mkdir -p /docker/nginx/html

Copy the index.html 50x.html in the html folder of the unzipped negix to the /docker/nginx/html folder

Here is a negix conf file. The format may change due to annotations. Remember to delete the annotations.

Nginx.conf:

user root;

worker_processes 2; #Set your number of threads here

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024; #Maximum number of connections}
http {
include mime.types;
default_type application/octet-stream;
upstream mytomcat{
server 172.17.0.3:8080 weight=10;
# In addition, the name of mytomcat should be consistent with the name below. It should be consistent with your tomcat IP. server 172.17.0.4:8080 weight=50;
server 172.17.0.5:8080 weight=10;
}
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name mytomcat;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
# index index.html index.htm;
proxy_connect_timeout 50;
proxy_read_timeout 10;
proxy_send_timeout 20;
proxy_pass http://mytomcat;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
#root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
#ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
#root html;
# index index.html index.htm;
# }
#}
}

Start using docker

Create and run the container

81: This is the port for external network access. You can modify it according to your actual situation.

/docker/nginx/nginx.conf local host file

/etc/nginx/nginx.conf The directory where the zip file is extracted (you can also leave it unchanged)

/docker/nginx/html local host file

/usr/share/nginx/html Unzip directory

docker run -d --name nginx81 -p 81:80 -v /docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /docker/nginx/html:/usr/share/nginx/html nginx

test

http://39.106.147.162:8085/hello/index.html I have configured port 8085 here

Direct access

Summarize

The above is the tutorial on how to use Docker to build a tomcat cluster using nginx. I hope it will be helpful to you!

You may also be interested in:
  • Docker Nginx container and Tomcat container to achieve load balancing and dynamic and static separation operations
  • How to use Docker Compose to implement nginx load balancing
  • Detailed explanation of using nginx and docker to achieve a simple load balancing
  • Summary of the deployment of Tomcat cluster and Nginx load balancing based on Docker

<<:  Two query methods when the MySQL query field type is json

>>:  Node and Python two-way communication implementation code

Recommend

How Web Designers Create Images for Retina Display Devices

Special statement: This article is translated bas...

5 ways to migrate Docker containers to other servers

Migration is unavoidable in many cases. Hardware ...

Ubuntu 16.04 kernel upgrade steps

1. Environment Ubuntu 16.04 running on a virtual ...

How to solve the error "ERROR 1045 (28000)" when logging in to MySQL

Today, I logged into the server and prepared to m...

Summary of bootstrap learning experience-css style design sharing

Due to the needs of the project, I plan to study ...

Top 10 Js Image Processing Libraries

Table of contents introduce 1. Pica 2. Lena.js 3....

How to handle MySQL numeric type overflow

Now, let me ask you a question. What happens when...

Example code for implementing hollowing effect with CSS

Effect principle Mainly use CSS gradient to achie...

Collection of 25 fonts used in famous website logos

This article collects the fonts used in the logos...

HTML is something that web page creators must learn and master.

What are the benefits of learning HTML? 1: Easily...

Example code for element multiple tables to achieve synchronous scrolling

Element UI implements multiple tables scrolling a...

Code analysis of user variables in mysql query statements

In the previous article, we introduced the MySQL ...

HTML special character conversion table

character Decimal Character Number Entity Name --...

IIS configuration of win server 2019 server and simple publishing of website

1. First remotely connect to the server 2. Open S...