Summary of the deployment of Tomcat cluster and Nginx load balancing based on Docker

Summary of the deployment of Tomcat cluster and Nginx load balancing based on Docker

Written in front

After reading books related to Doccer, I happened to have a project that required this, so I practiced it myself.

Treat it as a hundred lifetimes. The logic here is very clear: I think, therefore I am. Since I exist, I cannot pretend not to exist. No matter what, I have to take responsibility for myself. ——Wang Xiaobo, "At Thirty"


Structure diagram:

insert image description here

This is just for learning. Generally, for this kind of load, Nginx is placed on主機側, and JavaWeb(Tomcat) application is placed in the container.

Effect

insert image description here

Create a new folder.

D=uag;mkdir $D;cd $D;mkdir uag_nginx uag_tomcat8;
 ls
 uag_nginx uag_tomcat8

1. Ngixn image creation

cd uag_nginx/
# Used to store configuration files mkdir nginx
vim Dockerfile

Dockerfile content

FROM nginx
LABEL maintainer="uag"
ENV REFRESHED_AT 2021-08-27

EXPOSE 8099

Build the nginx configuration file content

This configuration file is shared with the container through the -v parameter when the container is running. Convenient for later parameter changes

cd ./nginx
vim nginx.conf

Contents of the nginx.conf configuration file

user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
daemon off;

events {
    worker_connections 1024;
}


http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$upstream_addr - $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;

    server {
        listen 8099;
        server_name localhost;

        root /var/www/html/;
        index index.html index.htm;

        access_log /var/log/nginx/default_access.log main;
        error_log /var/log/nginx/default_error.log;

        
        location / {
                proxy_pass http://backend;
        }

        location ~ .* {
                        proxy_pass http://backend;
                        proxy_set_header Host $http_host;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                }

        

}
    # Configure the load upstream backend here {
    server 172.23.231.190:8069;
    server 172.23.231.190:8079;
    server 172.23.231.190:8089;
}
}

Configure the load: 172.23.231.190 is the host IP, 8069, 8079, 8089 are the corresponding Java Web exposed application ports.

 # Configure the load upstream backend here {
    server 172.23.231.190:8069;
    server 172.23.231.190:8079;
    server 172.23.231.190:8089;
}

Build Nginx image

docker build -t uag/uag_nginx .

2. Java Web (Tomcat) application image construction

cd uag_tomcat8/

vim Dockerfile

Dockerfile content

FROM dordoka/tomcat
MAINTAINER LIRUILONG

COPY UAWeb.war /opt/tomcat/webapps/UAWeb.war

EXPOSE 8080

ENTRYPOINT [ "/opt/tomcat/bin/catalina.sh", "run" ]

Upload the corresponding War package

ls
Dockerfile UAWeb.war

Build the image

docker build -t uag/uag_tomcat .

3. Run the container Nginx image

docker run -d -p 8099:8099 --name uag_nginx -v $PWD/nginx/nginx.conf:/etc/nginx/nginx.conf uag/uag_nginx nginx

Java Web (Tomcat) Image

docker run -it -d -p 8089:8080 --name uag_app_1 uag/uag_tomcat
 docker run -it -d -p 8079:8080 --name uag_app_2 uag/uag_tomcat
 docker run -it -d -p 8069:8080 --name uag_app_3 uag/uag_tomcat

View running containers

insert image description here

Browser access

insert image description here

View the load mode: the mode of the new process

insert image description here

View the load mode: –volumes-from mode

Dockerfile

FROM nginx
LABEL maintainer="uag"
ENV REFRESHED_AT 2021-08-27

VOLUME /var/log/nginx/
EXPOSE 80
┌──(liruilong㉿Liruilong)-[/mnt/e/docker/uag/uag_nginx]
└─$ docker run -it --rm --volumes-from nginx_log centos cat /var/log/nginx/default_access.log
172.23.231.190:8069 - 172.17.0.1 - - [30/Aug/2021:12:55:02 +0000] "GET /UAWeb/services/listServices HTTP/1.1" 200 12660 "http://127.0.0.1:8099/UAWeb/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "-"

172.23.231.190:8079 - 172.17.0.1 - - [30/Aug/2021:12:55:02 +0000] "GET /UAWeb/axis2-web/css/axis-style.css HTTP/1.1" 200 1587 "http://127.0.0.1:8099/UAWeb/services/listServices" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "-"
172.23.231.190:8069 - 172.17.0.1 - - [30/Aug/2021:12:55:02 +0000] "GET /UAWeb/axis2-web/images/asf-logo.gif HTTP/1.1" 200 5866 "http://127.0.0.1:8099/UAWeb/services/listServices" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "-"
172.23.231.190:8079 - 172.17.0.1 - - [30/Aug/2021:12:55:02 +0000] "GET /UAWeb/axis2-web/images/axis_l.jpg HTTP/1.1" 200 12340 "http://127.0.0.1:8099/UAWeb/services/listServices" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "-"
172.23.231.190:8089 - 172.17.0.1 - - [30/Aug/2021:12:55:03 +0000] "GET /UAWeb/services/listServices HTTP/1.1" 200 12660 "http://127.0.0.1:8099/UAWeb/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "-"
172.23.231.190:8069 - 172.17.0.1 - - [30/Aug/2021:12:55:03 +0000] "GET /UAWeb/axis2-web/images/asf-logo.gif HTTP/1.1" 200 5866 "http://127.0.0.1:8099/UAWeb/services/listServices" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92

Build the image and upload it to the repository:

insert image description here

Well, you need to register a Docker Hub賬號, then log in, and add賬戶名/

┌──(liruilong㉿Liruilong)-[/mnt/e/docker/uag/uag_nginx]
└─$ docker push liruilong/nginx_log
The push refers to repository [docker.io/liruilong/nginx_log]
An image does not exist locally with the tag: liruilong/nginx_log

┌──(liruilong㉿Liruilong)-[/mnt/e/docker/uag/uag_nginx]
└─$ docker tag 9c9af0362eb9 liruilong/nginx_log

┌──(liruilong㉿Liruilong)-[/mnt/e/docker/uag/uag_nginx]
└─$ docker push liruilong/nginx_log
The push refers to repository [docker.io/liruilong/nginx_log]
fb04ab8effa8: Pushed
8f736d52032f: Pushed
009f1d338b57: Pushed
678bbd796838: Pushed
d1279c519351: Pushed
f68ef921efae: Pushed
latest: digest: sha256:2af7e8aeab84e8a816caf6b0342e1a45f95c7089ff52578040ea3a4c28a943c7 size: 1570

┌──(liruilong㉿Liruilong)-[/mnt/e/docker/uag/uag_nginx]
└─$ docker push liruilong/nginx_log:tagname # Pull the image 

insert image description here

This is the end of this article about deploying Tomcat cluster and Nginx load balancing based on Docker. For more information about deploying Tomcat Nginx load balancing with Docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of Nginx+Tomcat load balancing cluster installation and configuration case
  • Implementation example of Nginx+Tomcat load balancing cluster
  • Implementation method of Nginx+tomcat load balancing cluster
  • Build Tomcat9 cluster through Nginx and realize session sharing
  • How to build Tomcat cluster with Nginx

<<:  Analysis and redesign of music player apps (application software) How to design a beautiful music player interface

>>:  Master the CSS property display:flow-root declaration in one article

Recommend

Comprehensive understanding of Node event loop

Table of contents Node Event Loop Event loop diag...

WeChat applet implements search box function

This article example shares the specific code for...

Detailed explanation of VUE Token's invalidation process

Table of contents Target Thought Analysis Code la...

Detailed explanation of the WeChat applet request pre-processing method

question Because some of our pages request data i...

11 Reasons Why Bootstrap Is So Popular

Preface Bootstrap, the most popular front-end dev...

How to make a website look taller and more designed

“How to make a website look high-end? Or more des...

Three uses and differences of MySQL not equal

Judgment symbols are often used in MySQL, and not...

Quick solution for forgetting MySQL8 password

Preface When we forget the MySQL database passwor...

Install Apple Mac OS X in VMWare12 Graphic Tutorial

1. Introduction: Because my friend wanted to lear...

Causes and solutions for cross-domain issues in Ajax requests

Table of contents 1. How is cross-domain formed? ...

Analysis of the usage of Xmeter API interface testing tool

XMeter API provides a one-stop online interface t...