Docker deploys Nginx for load balancing

Operating environment: Centos 7 Docker Nginx

Load balancing is done using the host and two Nginx containers, with no content on the host. Only one Nginx server is installed, which forwards requests to two Nginx containers for processing

Nginx load balancing nginx load balancing nginx load balancing

First, install the relevant environment

1. Install Docker (recommended configuration ali cloud source) 2

Disable the firewall and Selinux

[root@iZbp18vj2il5rgv6uy66xxZ ~]# systemctl stop firewalld.service //Centos7 disable firewall command [root@iZbp18vj2il5rgv6uy66xxZ ~]# Setenforce 0 // Temporarily close SELinux // If the pagoda is installed, [root@iZbp18vj2il5rgv6uy66xxZ ~]# yum install docker -y [root@iZbp18vj2il5rgv6uy66xxZ ~]# wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm [root@iZbp18vj2il5rgv6uy66xxZ ~]# yum install nginx -yCopy the code

Portal: What is selinux centos7 firewall command

Use the command to test whether the installation is successful and whether the service can be started

// If the pagoda is installed, [root@iZbp18vj2il5rgv6uy66xxZ ~]# systemctl start docker.service [root@iZbp18vj2il5rgv6uy66xxZ ~]# systemctl start Nginx. service Centos 7 Start service command is different from 6.xCopy the code

Test whether the Web page is displayed successfully

Totally ok!! Let’s delete the default HTML of nginx and create a blank index.html so that we can see the effect after the configuration is successful

[root@iZbp18vj2il5rgv6uy66xxZ ~]# cd /www/server/nginx/html/
[root@iZbp18vj2il5rgv6uy66xxZ ~]# rm index.html
[root@iZbp18vj2il5rgv6uy66xxZ ~]# touch index.html
Copy the code

Install the Nginx container

  1. Pull nginx mirror
[root@iZbp18vj2il5rgv6uy66xxZ www]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
07aded7c29c6: Pull complete 
bbe0b7acc89c: Pull complete 
44ac32b0bba8: Pull complete 
91d6e3e593db: Pull complete 
8700267f2376: Pull complete 
4ce73aa6e9b0: Pull complete 
Digest: sha256:765e51caa9e739220d59c7f7a75508e77361b441dccf128483b7f5cce8306652
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
Copy the code
  1. Pull completes the view using the Docker images command
[root@iZbp18vj2il5rgv6uy66xxZ www]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
nginx        latest    f8f4ffc8092c   7 days ago   133MB
Copy the code
  1. Docker run creates the container
Docker run -p 8080:80 --name nginx_web1 it nginx /bin/bashCopy the code

Go to the nginx directory of the nginx_web1 container and create an index.html

root@9f88cab79ac8:/# cd /usr/share/nginx/html/
root@9f88cab79ac8:/usr/share/nginx/html# ls
50x.html  index.html
root@9f88cab79ac8:/usr/share/nginx/html# rm index.html 
root@9f88cab79ac8:/usr/share/nginx/html# echo hello nginx_web1 ninetySeven > index.html
root@9f88cab79ac8:/usr/share/nginx/html# exit
Copy the code

Let’s create a new nginx container

Docker run -p 8081:80 --name nginx_web2 it nginx /bin/bashCopy the code

Go to the nginx directory of the nginx_web2 container and create an index.html

root@f1b7fcac2dad:/#  cd /usr/share/nginx/html/
root@f1b7fcac2dad:/usr/share/nginx/html# ls
50x.html  index.html
root@f1b7fcac2dad:/usr/share/nginx/html# rm index.html 
root@f1b7fcac2dad:/usr/share/nginx/html# echo hello nginx_web2 ninetySeven > index.html
root@f1b7fcac2dad:/usr/share/nginx/html# exit
Copy the code

Look at the container

[root@iZbp18vj2il5rgv6uy66xxZ www]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f1b7fcac2dad Nginx/docker - entrypoint.... "" 4 minutes ago Up About a minute 0.0.0.0:8081->80/ TCP, :::8081->80/ TCP nginx_web2 9f88cab79ac8 nginx "/docker-entrypoint...." 10 minutes ago Up 49 seconds 0.0.0.0:8080->80/ TCP, :::8080->80/ TCP nginx_web1Copy the code

Start two nginx containers before running the start service command

[root@iZbp18vj2il5rgv6uy66xxZ ~]# docker start f1b7fcac2dad
f1b7fcac2dad
[root@iZbp18vj2il5rgv6uy66xxZ ~]# docker start 9f88cab79ac8
9f88cab79ac8
[root@iZbp18vj2il5rgv6uy66xxZ ~]# docker exec -d f1b7fcac2dad service nginx start
[root@iZbp18vj2il5rgv6uy66xxZ ~]# docker exec -d 9f88cab79ac8 service nginx start
Copy the code

3. Configure the host nginx file

Method 1: CLI Operation (recommended)

[root@iZbp18vj2il5rgv6uy66xxZ /]# cd /www/server/nginx/conf/
[root@iZbp18vj2il5rgv6uy66xxZ conf]# vim nginx.conf
Copy the code

Method 2: Open the pagoda panel to find the NGINx administration

Add the following code to the HTTP section

Upstream nginx.kkcake. Cn {server 101.37.157.104:8080 weight=10; // nginx.kkcake. Server 101.37.157.104:8081 weight = 20; } server{ listen 80; server_name nginx.kkcake.cn; location / { proxy_pass http://nginx.kkcake.cn; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}Copy the code

Save the configuration and restart the host Nginx or pagoda panel Nginx management restart service

[root@iZbp18vj2il5rgv6uy66xxZ ~]# systemctl restart nginx.service
Copy the code

Check the running status of the Nginx container

Note: If the server is online, you need to enable the port in the server and pagoda interface

Server configuration port

Pagoda configuration port

Five, the test

How many more times?

Web1, web2, you see that? \

Docker-based nginx load success!!