This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
preface
With the rise of microservices and containerization, you’ve all heard of Docker. Compared with virtual machines, Docker is a lightweight virtual technology, and its isolation and portability also allow it to have more application scenarios. In my spare time, I also learned Docker, so I tried to build a load balancing system of Nginx + 2 Tomcat on an ECS with Docker to deepen my understanding of Docker.
Generally, when setting up load balancing on one machine, two Tomcats with different ports are required, but the applications in Webapps must be consistent. One application needs to be copied to the two Tomcats after modification. There is no need for the container to run Tomcat, and tomcat in both containers can share a tomcat directory on the host machine.
Docker
Docker, like a lightweight VIRTUAL machine, also needs its own image to initialize. Dockerhub like hosting code github, hosting developers have built the image, we can use the Docker pull command can pull the target image, and then through the image to start a container. Docker acts as a guardian of the foreground process and exists for pre-set commands in the image from the moment it is started. So the image in DockerHub is built with the programs and commands to run integrated into it.
If you want to DIY your own image with a basic image, you need to write your own Dockerfile file. Dockerfile builds the target image from a basic image, and the image builds the docker container. Select centos for basic image.
Install the docker
yum -y install docker
Start the docker daemon
systemctl start docker.service
# pull the basic image
docker pull centos
Copy the code
Tomcat
Java8 is used to run tomcat.
Download tomcat
Wget https://mirrors.bfsu.edu.cn/apache/tomcat/tomcat-8/v8.5.57/bin/apache-tomcat-8.5.57.tar.gz tar ZXVF Apache tomcat - 8.5.57. Tar. GzCopy the code
dockerfile
Write a Dockerfile to build the image. Note here that the file name must be Dockerfile.
The first line must be FROM, specifying the base image
FROM centos
Specify JDK and Tomcat mount points in the container
VOLUME /usr/local/jdk
VOLUME /usr/local/tomcat
To set Java and Tomcat environment variables, you need to mount the JDK and tomcat directories in the host machine to the corresponding directory
ENV JAVA_HOME /usr/local/jdk
ENV CATALINA_HOME /usr/local/tomcat
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
The host port is mapped to the container when it is run
EXPOSE 8080
# docker command to run, this is the tomcat front run mode, can not use background run
CMD catalina.sh run
Copy the code
CMD is what a container runs for, there can only be one, and the latter overrides the former.
Build image
Use the docker build command in the directory where the Dockerfile resides to build the image. The command and procedure are as follows:
[root@ tomcat]# docker build -t tomcat:base .Sending build context to Docker daemon 2.048 kB Step 1/8: FROM centos --> 0d120b6ccAA8 Step 2/8: VOLUME /usr/local/jdk
---> Running in 358380feb38c
---> 3861d1ad84ae
Removing intermediate container 358380feb38c
Step 3/8 : VOLUME /usr/local/tomcat
---> Running in 530d504561ed
---> d31fe0243b5c
Removing intermediate container 530d504561ed
Step 4/8 : ENV JAVA_HOME /usr/local/jdk
---> Running in e135bbe0e419
---> 82c1725ef317
Removing intermediate container e135bbe0e419
Step 5/8 : ENV CATALINA_HOME /usr/local/tomcat
---> Running in 241108146f13
---> a16468b916c8
Removing intermediate container 241108146f13
Step 6/8 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
---> Running in 2b6ca6c98b05
---> 59c63f6dd4d7
Removing intermediate container 2b6ca6c98b05
Step 7/8 : EXPOSE 8080
---> Running in 91375d4ee543
---> 213c08fc99da
Removing intermediate container 91375d4ee543
Step 8/8 : CMD catalina.sh run
---> Running in a7bc2b3bad54
---> a1d156c4ad18
Removing intermediate container a7bc2b3bad54
Successfully built a1d156c4ad18
Copy the code
Use Docker images to view the newly built image Tomcat:
Run the container
Use the new image to build and start the first container:
Docker run -d --name tomcat1 \ -p 8081:8080 \ -v /usr/java/jdk1.8.0_131:/usr/local/jdk \
-v /usr/local/ apache tomcat - 8.5.57: / usr /local/tomcat \
tomcat:base
Copy the code
Build and start the second container:
Docker run -d --name tomcat2 \ -p 8082:8080 \ -v /usr/java/jdk1.8.0_131:/usr/local/jdk \
-v /usr/local/ apache tomcat - 8.5.57: / usr /local/tomcat \
tomcat:base
Copy the code
–name is used to specify the name of each container. -p is used to specify the mapping between the host port and port 8080 of the container, so that tomcat can be accessed through the host port. -v is used to mount the JDK and tomcat directory of the host to the container. Use docker PS to check container startup:If the container fails to start, you can view the startup logs through Docker logs Tomcat1.
Nginx
Install nginx
Wget http://nginx.org/download/nginx-1.18.0.tar.gz tar ZXVF nginx - 1.18.0. Tar. GzcdNginx - 1.18.0Check the installation environment and specify the installation directory
./configure --prefix=/usr/local/nginx
# compile and install
make && make install
Copy the code
dockerfile
Name it Dockerfile again, set the nginx mount point and open port 80.
FROM centos
VOLUME /usr/local/nginx
ENV NGINX_HOME /usr/local/nginx
ENV PATH $PATH:$NGINX_HOME/sbin
EXPOSE 80
CMD nginx
Copy the code
Build image
Build the image in the Dockerfile directory.
[root@iZuf6hmbw6l1tj3u3tzjsdZ nginx]# docker build -t nginx:base .Sending build context to Docker daemon 2.048 kB Step 1/6: FROM centos --> 0d120b6ccAA8 Step 2/6: VOLUME /usr/local/nginx
---> Running in e55653915b1f
---> 65e96f9c4d30
Removing intermediate container e55653915b1f
Step 3/6 : ENV NGINX_HOME /usr/local/nginx
---> Running in 6dccb09a2d84
---> 32f2e7563ffe
Removing intermediate container 6dccb09a2d84
Step 4/6 : ENV PATH $PATH:$NGINX_HOME/sbin
---> Running in e5b588d688b0
---> 9c23baa48124
Removing intermediate container e5b588d688b0
Step 5/6 : EXPOSE 80
---> Running in 362a6cbbffc9
---> 6f8ba20a1e48
Removing intermediate container 362a6cbbffc9
Step 6/6 : CMD nginx
---> Running in f0b96ed930d3
---> 64ee43ce3106
Removing intermediate container f0b96ed930d3
Successfully built 64ee43ce3106
Copy the code
View the built nginx image:
nginx.conf
Modify the nginx configuration to add two Tomcats to upstream as a set of services.
Docker can only handle foreground processesdaemon off; HTTP {upstream docker {server host IP :8081; Server host IP :8082; } server { listen 80; Server_name HOST IP address; location / { root html; index index.html index.htm;Add a reverse proxyproxy_pass http://docker; }}}Copy the code
Run the container
Map port 80 of the host machine to port 80 of the container, and mount nginx of the host machine to the container.
docker run -d \
--name nginx \
-p 80:80 \
-v /usr/local/nginx:/usr/local/nginx \
nginx:base
Copy the code
Docker ps view running containers:
test
throughHttp://host IP address access ngxin, tomcat is correctly accessed. Nginx logs are accessed from both Tomcat servers.
conclusion
When modifying an application, you only need to put the application in the Tomcat Webapps of the host computer and restart the Tomcat container. Restart command:
docker stop tomcat1
docker start tomcat1
Copy the code