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:
|
<<: JavaScript to achieve Taobao product image switching effect
>>: Detailed explanation of execution context and call stack in JavaScript
Slow query log related parameters MySQL slow quer...
The key features of the InnoDB storage engine inc...
mysqladmin is an official mysql client program th...
1. Storage Engine In the last section, we mention...
Sometimes you need to set several areas on a pict...
Preface There is a scenario where, for the sake o...
Table of contents 1. Description of functions in ...
The reuse of code in vue provides us with mixnis....
I often need to change nginx configuration at wor...
Table of contents Install mysql Configuring envir...
Using the official MySQL image requires some modi...
In the process of learning web design, I did not ...
This article shares the specific code of Vue to r...
This article shares the specific code for JavaScr...
An index is a sorted data structure! The fields t...