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

Detailed explanation of ECharts mouse event processing method

An event is an action performed by the user or th...

Vite+Electron to quickly build VUE3 desktop applications

Table of contents 1. Introduction 2. Create a Vit...

Example of disabling browser cache configuration in Vue project

When releasing a project, you will often encounte...

How to install and deploy zabbix 5.0 for nginx

Table of contents Experimental environment Instal...

Solution to the problem of slow docker pull image speed

Currently, Docker has an official mirror for Chin...

Vue+openlayer5 method to get the coordinates of the current mouse slide

Preface: How to get the coordinates of the curren...

ftp remotely connect to Linux via SSH

First install ssh in Linux, taking centos as an e...

Detailed explanation of the correct way to open em in CSS

Why do we say “usually 1em=16px”? The default tex...

Complete MySQL Learning Notes

Table of contents MyISAM and InnoDB Reasons for p...

Several ways of running in the background of Linux (summary)

1. nohup Run the program in a way that ignores th...

The most complete package.json analysis

Table of contents 1. Overview 2. Name field 3. Ve...

A brief discussion on MySQL event planning tasks

1. Check whether event is enabled show variables ...

React Native startup process detailed analysis

Introduction: This article takes the sample proje...