nginx+docker load balancing

Experiment: install nginx on a server, and enable two docker processes on the server to simulate load balancing
1. Install nginx

# rpm -vih

2. Install docker
Binary package download address:

#tar zxvf docker-18.09.6.tgz
mkdir /etc/docker
# mv docker/* /usr/bin
# mkdir /etc/docker
# mv daemon.json /etc/docker
# mv docker.service /usr/lib/systemd/system
# systemctl start docker
# systemctl enable docker

Two point one daemon.json Document content

    "registry-mirrors": [""]

Two point two docker.service Document content

Description=Docker Application Container Engine
Documentation= firewalld.service containerd.service

ExecReload=/bin/kill -s HUP $MAINPID


Pull the docker image of tomcat and start two docker processes

# doker pull tomcat
#docker run --name tomcat1 -p 8081:8080 -v /data/tomcat1/webapps1:/usr/local/tomcat/webapps -d tomcat
#docker run --name tomcat2 -p 8082:8080 -v /data/tomcat1/webapps1:/usr/local/tomcat/webapps -d tomcat
#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
284264b13148        tomcat              " run"   2 hours ago         Up 2 hours>8080/tcp   tomcat1
a96d092ce945        tomcat              " run"   17 hours ago        Up 2 hours>8080/tcp   tomcat

Note: the webapps directory of tomcat pull ed down is empty and needs to be customized index.html File content, otherwise access will report Error 404
I filled in tomcat1 and tomcat2 respectively

Configure nginx
Enter nginx profile directory and create a new one zs.conf File, file name customization. Just add the following

# cd /etc/nginx/conf.d/
vi zs.conf
upstream localhost{
        server weight=1;
        server weight=1;
server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
        proxy_pass   http://localhost;

The name after upstream should be consistent with the proxy_ The names after pass are consistent. server_name can add host locally to resolve IP to any domain name.
The purpose of this is to facilitate management. One file corresponds to one application. When you need to add load balancing for other applications, create a new. conf directly in this directory. To stop the load of an application, delete the. conf directly.

Restart nginx
#systemctl restart nginx

Browser access

Tags: Operation & Maintenance Docker Tomcat Nginx RPM

Posted on Tue, 26 May 2020 07:13:17 -0700 by goclimb