The Method and Step of Using Docker Compose to Realize nginx Load Balancing

Time:2019-10-7

Nginx load balancing based on Docker’s network management and IP settings of containers

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

*/

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

Specify container IP address

Official Document Address: https://docs.docker.com/compose/compose-file/#ipv4_address-ipv6_address

Continue to write docker-compose.yml in the previous article “12. Using Docker Compose Container Arrangement 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 with docker-compose


docker-compose up -d

Check whether the container is started and confirm whether the network nginx LSB is created

// You can view the container status in the container group currently configured by 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
*/

// The nginx-lsb network is created, named as the beginning of the file name of the container group project _network name

Check out the details of nginx-lsb

docker network inspect composetest_nginx-lsb

// In the details, you can see the IP of each container that uses this network

Such as:

/*
...
 "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 understood simply as: defining variables in docker-compose.yml and referencing variables in external.env files

Official Document Address: https://docs.docker.com/compose/compose-file/#env_file

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

// Modify docker-compose.yml file and add 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 that the container IP was set successfully

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

// View the details of the network
docker network inspect composetest_nginx-lsb

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

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

// Modify docker-compose.yml file and add 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 nginx.conf configuration file to 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

Access http://server IP address: 8000, test server load balancing!

Note: The last article has placed different web files on two httpd servers

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.