In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use Docker Compose to achieve nginx load balancing. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Realize Nginx load balance based on the network management of Docker and the IP setting of container
View all docker networks
Docker network ls/*NETWORK ID NAME DRIVER SCOPEb832b168ca9a bridge bridge local373be82d3a6a composetest_default bridge locala360425082c4 host host local154f600f0e90 none null local*/// composetest_default is the directory name where the docker-compose.yml file was located when Compose was introduced in the previous article. / / so containers created with docker-compose will create a network with the directory name as the network name by default. And it is a dridge (bridge) type
Specify the container IP address
Official website document address: https://docs.docker.com/compose/compose-file/#ipv4_address-ipv6_address
Continue to write the previous article 12. Docker-compose.yml in the article "using the Docker Compose Container orchestration tool"
Version: "3" services: web1: container_name: web1 image: "centos:httpd" ports:-"8080 usr/sbin/init' 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 privileged 80" privileged: true volumes:-"/ app/www/web2/:/var/www/html/" command: ['/ usr/sbin/init'] networks: nginx-lsb: ipv4_address: 192.169.0.2networks: nginx-lsb: driver: bridge ipam: config:-subnet: 192.169.0.0 16
Start the container using docker-compose
Docker-compose up-d
Check to see if the container is started and confirm that a network nginx-lsb has been created
/ / you can view the container status in the container group of the current docker-compose.yml configuration. Docker-compose psdocker network ls/*NETWORK ID NAME DRIVER SCOPEb832b168ca9a bridge bridge local373be82d3a6a composetest_default bridge localde6f5b8df1c8 composetest_nginx-lsb bridge locala360425082c4 host host local154f600f0e90 none null local*/// creates the nginx-lsb network. The name is the beginning of the file name of the container group project _ network name
View the details of the network nginx-lsb
In the docker network inspect composetest_nginx-lsb// details, you can see the ip of each container using this network, such as: / *. "Containers": {"039aa860ef04f20a7566fdc943fb4398a61d2ad6dd3e373b17c86ac778af89e3": {"Name": "web2", "EndpointID": "1bc206661179e65999015f132c2889d3d4365b8d42b8a89cf9c260016fedd5ee", "MacAddress": "02:42:c0:a9:00:02", "IPv4Address": "192.169.0.2 IPv6Address", "IPv6Address": "}," 437ad7a07da8e46c0abaf845c4b08425338009fbe972bde69478cf47c75c315b ": {" Name ":" web1 " "EndpointID": "5a36e602a5364ee9ad06e9816d09e3966d56ebf06b9394ebc25b8bcee9546607", "MacAddress": "02:42:c0:a9:00:03", "IPv4Address": "192.169.0.3 Maple 16", "IPv6Address": ""}},. * /
Use env_file environment files:
It is simple to understand that variables are defined in docker-compose.yml and references are defined in external .env files.
Official document address: https://docs.docker.com/compose/compose-file/#env_file
/ / define a .env file in the composetest directory to store the variable web1_addr=192.169.0.2web2_addr=192.169.0.3// and modify the docker-compose.yml file. Add variable definition version: "3" services: web1: container_name: web1 image: "centos:httpd" ports:-"8080 usr/sbin/init' 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 privileged 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 Universe 16
Restart the composetest project and check the network details to confirm that the container ip is set successfully
/ / restart the composetest project docker-compose up-dplink / view network details docker network inspect composetest_nginx-lsb
Add a nginx server as a load balancing server in the composetest project
/ / add a variable nginx_lsbweb1_addr=192.169.0.2web2_addr=192.169.0.3nginx_lsb=192.169.0.100// to the .env file to modify the docker-compose.yml file Add variable definition version: "3" services: nginx-lsb: container_name: nginx-lsb image: "centos:nginx" ports:-"8000 container_name 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 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 privileged 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 networks / restart the composetest project docker-compose up-d
Modify nginx.conf configuration file to configure load balancer
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 the http:// server IP address: 8000 to test the server load balance!
This is the end of the article on "how to use Docker Compose to achieve nginx load balancing". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.