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 http://nginx.org/packages/rhel/7/x86_64/RPMS/nginx-1.16.0-1.el7.ngx.x86_64.rpm

2. Install docker
Binary package download address: https://download.docker.com/linux/static/stable/x86_64/

#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": ["http://bc437cce.m.daocloud.io"]
}

Two point two docker.service Document content

 [Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service containerd.service
Wants=network-online.target

[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
StartLimitBurst=3
StartLimitInterval=60s
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
Delegate=yes
KillMode=process

[Install]
WantedBy=multi-user.target

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              "catalina.sh run"   2 hours ago         Up 2 hours          0.0.0.0:8081->8080/tcp   tomcat1
a96d092ce945        tomcat              "catalina.sh run"   17 hours ago        Up 2 hours          0.0.0.0:8082->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 192.168.100.216:8081 weight=1;
        server 192.168.100.216:8082 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 http://192.168.100.216

Tags: Operation & Maintenance Docker Tomcat Nginx RPM

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