How to use Docker Compose to implement nginx load balancing

How to use Docker Compose to implement nginx load balancing

Implement Nginx load balancing based on Docker network management and container IP settings

View all docker networks

docker network ls

/*
NETWORK ID NAME DRIVER SCOPE
b832b168ca9a bridge bridge local
373be82d3a6a composetest_default bridge local
a360425082c4 host host local
154f600f0e90 none null local

*/

// composetest_default is the directory name where the docker-compose.yml file is located when introducing Compose in the previous article.
// Therefore, the container created with docker-compose will create a network with the directory name as the network name by default, and it is of dridge (bridge) type

Specify the container IP address

Official website document address: https://docs.docker.com/compose/compose-file/#ipv4_address-ipv6_address

Continue writing the docker-compose.yml file from the previous article "12. Using Docker Compose Container Orchestration Tool"

version: "3"
services:
  web1:
    container_name: web1
    image: "centos:httpd"
    ports:
      - "8080:80"
    privileged: true
    volumes:
      - "/app/www/web1/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: 192.169.0.3
  web2:
    container_name: web2
    image: "centos:httpd"
    ports:
      - "8081:80"
    privileged: true
    volumes:
      - "/app/www/web2/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: 192.169.0.2
networks:
  nginx-lsb:
    driver: bridge
    ipam:
      config:
        - subnet: 192.169.0.0/16

Start the container using docker-compose

docker-compose up -d

Check whether the container is started and whether the network nginx-lsb is created

// You can view the container status in the container group configured by the current docker-compose.yml docker-compose ps

docker network ls

/*
NETWORK ID NAME DRIVER SCOPE
b832b168ca9a bridge bridge local
373be82d3a6a composetest_default bridge local
de6f5b8df1c8 composetest_nginx-lsb bridge local
a360425082c4 host host local
154f600f0e90 none null local
*/

// Created the nginx-lsb network, named after the beginning of the container group project file name_network name

View network nginx-lsb details

docker network inspect composetest_nginx-lsb

// In the details, you can see the IP address of each container using this network.

like:

/*
...
 "Containers": {
      "039aa860ef04f20a7566fdc943fb4398a61d2ad6dd3e373b17c86ac778af89e3": {
        "Name": "web2",
        "EndpointID": "1bc206661179e65999015f132c2889d3d4365b8d42b8a89cf9c260016fedd5ee",
        "MacAddress": "02:42:c0:a9:00:02",
        "IPv4Address": "192.169.0.2/16",
        "IPv6Address": ""
      },
      "437ad7a07da8e46c0abaf845c4b08425338009fbe972bde69478cf47c75c315b": {
        "Name": "web1",
        "EndpointID": "5a36e602a5364ee9ad06e9816d09e3966d56ebf06b9394ebc25b8bcee9546607",
        "MacAddress": "02:42:c0:a9:00:03",
        "IPv4Address": "192.169.0.3/16",
        "IPv6Address": ""
      }
    },
...
*/

Use env_file environment file:

It can be simply understood as: define variables in docker-compose.yml and reference them in external .env files

Official documentation address: https://docs.docker.com/compose/compose-file/#env_file

// Or define a .env file in the composetest directory to store the variable web1_addr=192.169.0.2
web2_addr=192.169.0.3

// Modify the docker-compose.yml file and add the variable definition version: "3"
services:
  web1:
    container_name: web1
    image: "centos:httpd"
    ports:
      - "8080:80"
    privileged: true
    volumes:
      - "/app/www/web1/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: ${web1_addr}
  web2:
    container_name: web2
    image: "centos:httpd"
    ports:
      - "8081:80"
    privileged: true
    volumes:
      - "/app/www/web2/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: ${web2_addr}
networks:
  nginx-lsb:
    driver: bridge
    ipam:
      config:
        - subnet: 192.169.0.0/16

Restart the composetest project and check the network details to confirm whether the container IP is set successfully

// Restart the composetest project docker-compose up -d

// View network details docker network inspect composetest_nginx-lsb

Add an nginx server as a load balancing server in the composetest project

// Add a variable nginx_lsb in the .env file
web1_addr=192.169.0.2
web2_addr=192.169.0.3
nginx_lsb=192.169.0.100

// Modify the docker-compose.yml file and add the variable definition version: "3"
services:
  nginx-lsb:
    container_name: nginx-lsb
    image: "centos:nginx"
    ports: 
      - "8000:80"
    privileged: true
    volumes:
      - "/app/nginx/nginx.conf:/etc/nginx/nginx.conf"
    networks:
      nginx-lsb:
        ipv4_address: ${nginx_lsb}
  web1:
    container_name: web1
    image: "centos:httpd"
    ports:
      - "8080:80"
    privileged: true
    volumes:
      - "/app/www/web1/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: ${web1_addr}
  web2:
    container_name: web2
    image: "centos:httpd"
    ports:
      - "8081:80"
    privileged: true
    volumes:
      - "/app/www/web2/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: ${web2_addr}
networks:
  nginx-lsb:
    driver: bridge
    ipam:
      config:
        - subnet: 192.169.0.0/16

// Restart the composetest project docker-compose up -d

Modify the nginx.conf configuration file and configure load balancing

upstream mydocker {
  server 192.169.0.2;
  server 192.169.0.3;
}

server {
  listen 80;
  server_name mydocker;
  location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_buffering off;
    proxy_pass http://mydocker;
  }
}

Restart nginx-lsb and load the configuration file

docker-composer restart nginx-lsb

Visit http://serverIP address:8000 to test server load balancing!

Note: In the previous article, different web files have been placed on two httpd servers.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker Nginx container and Tomcat container to achieve load balancing and dynamic and static separation operations
  • How to use Docker to build a tomcat cluster using nginx (with pictures and text)
  • Detailed explanation of using nginx and docker to achieve a simple load balancing
  • Summary of the deployment of Tomcat cluster and Nginx load balancing based on Docker

<<:  JavaScript to achieve Taobao product image switching effect

>>:  Detailed explanation of execution context and call stack in JavaScript

Recommend

Robots.txt detailed introduction

Robots.txt is a plain text file in which website ...

A brief discussion of four commonly used storage engines in MySQL

Introduction to four commonly used MySQL engines ...

Using loops in awk

Let's learn about different types of loops th...

Solution to ElementUI's this.$notify.close() call not working

Table of contents Requirement Description Problem...

How to create LVM for XFS file system in Ubuntu

Preface lvm (Logical Volume Manager) logical volu...

Several ways to change MySQL password

Preface: In the daily use of the database, it is ...

How to completely uninstall node and npm on mac

npm uninstall sudo npm uninstall npm -g If you en...

Tutorial on processing static resources in Tomcat

Preface All requests in Tomcat are handled by Ser...

Detailed explanation of basic operation commands for Linux network settings

Table of contents View network configuration View...

How to build a new image based on an existing image in Docker

Building new images from existing images is done ...

Learn Node.js from scratch

Table of contents url module 1.parse method 2. fo...

Explanation on whether to choose paging or loading in interactive design

The author of this article @子木yoyo posted it on hi...

A brief discussion on the understanding of TypeScript index signatures

Table of contents 1. What is an index signature? ...

Summary of the use of MySQL date and time functions

This article is based on MySQL 8.0 This article i...