Using docker network management and container ip settings as basic knowledge to implement nginx load balancing
View all docker networks
Specify the container ip address
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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
|
Copy after login
Use docker-compose to start the container
Check whether the container is started and confirm whether the network nginx-lsb is created
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | docker-compose ps
docker network ls
|
Copy after login
View the details of the network nginx-lsb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | docker network inspect composetest_nginx-lsb
如:
|
Copy after login
Use env_file environment file:
It can be simply understood as: define variables in docker-compose.yml, and reference variables in external .env files
Official document address:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | web1_addr=192.169.0.2
web2_addr=192.169.0.3
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
|
Copy after login
Restart the composetest project, and check the network details to confirm whether the container ip is set successfully
1 2 3 4 5 | docker-compose up -d
docker network inspect composetest_nginx-lsb
|
Copy after login
Add an nginx server as a load balancing server in the composetest project
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | web1_addr=192.169.0.2
web2_addr=192.169.0.3
nginx_lsb=192.169.0.100
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
docker-compose up -d
|
Copy after login
Modify the nginx.conf configuration file, configure load balancing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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:
}
}
|
Copy after login
Restart nginx-lsb, load the configuration file
1 | docker-composer restart nginx-lsb
|
Copy after login
Visit http://server ip address:8000, test server load balancing!
The above is the detailed content of How to use Docker's Compose to implement nginx load balancing. For more information, please follow other related articles on the PHP Chinese website!