搜索
首页运维Nginx怎么使用Docker Compose实现nginx负载均衡

怎么使用Docker Compose实现nginx负载均衡

May 15, 2023 pm 09:37 PM
dockernginxcompose

以docker的网络管理,容器的ip设置为基础知识实现nginx负载均衡

查看所有docker网络

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 是上一篇介绍compose时,docker-compose.yml文件所在的目录名,
// 所以,用docker-compose创建的容器会默认创建一个以目录名为网络名的网络,并且是dridge(桥接)类型

指定容器ip地址

官网文档地址:

继续编写上一篇《12.使用docker compose容器编排工具》文章中的docker-compose.yml

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

使用docker-compose启动容器

docker-compose up -d

查看容器是否启动,并确认是否创建了网络 nginx-lsb

// 可以查看当前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
*/

// 创建了nginx-lsb网络,命名是容器组项目的 文件名开头_网络名

查看网络 nginx-lsb的详情

docker network inspect composetest_nginx-lsb

// 详情里面可以看到使用这个网络的每个容器的ip

如:

/*
...
 "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": ""
      }
    },
...
*/

使用 env_file环境文件:

简单可以理解为:在docker-compose.yml中定义变量,引用在外部.env文件中进行变量定义

官方文档地址:

// 还是在composetest目录中定义个 .env文件,用来存放变量
web1_addr=192.169.0.2
web2_addr=192.169.0.3

// 修改docker-compose.yml文件,加入变量定义
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

重新启动composetest项目,并查看网络详情,确认容器ip是否设置成功

// 重新启动composetest项目
docker-compose up -d

// 查看网络详情
docker network inspect composetest_nginx-lsb

在composetest项目中添加一台nginx服务器作为负载均衡服务器

// 在.env文件里添加一个变量 nginx_lsb
web1_addr=192.169.0.2
web2_addr=192.169.0.3
nginx_lsb=192.169.0.100

// 修改docker-compose.yml文件,加入变量定义
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

// 重新启动composetest项目
docker-compose up -d

修改nginx.conf配置文件,配置负载均衡

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;
  }
}

重新启动nginx-lsb,加载配置文件

docker-composer restart nginx-lsb

访问 http://服务器ip地址:8000,测试服务器负载均衡!

以上是怎么使用Docker Compose实现nginx负载均衡的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
NGINX与Apache:性能,可伸缩性和效率NGINX与Apache:性能,可伸缩性和效率Apr 19, 2025 am 12:05 AM

NGINX和Apache都是强大的Web服务器,各自在性能、可扩展性和效率上有独特的优势和不足。1)NGINX在处理静态内容和反向代理时表现出色,适合高并发场景。2)Apache在处理动态内容时表现更好,适合需要丰富模块支持的项目。选择服务器应根据项目需求和场景来决定。

终极摊牌:nginx vs. apache终极摊牌:nginx vs. apacheApr 18, 2025 am 12:02 AM

NGINX适合处理高并发请求,Apache适合需要复杂配置和功能扩展的场景。1.NGINX采用事件驱动、非阻塞架构,适用于高并发环境。2.Apache采用进程或线程模型,提供丰富的模块生态系统,适合复杂配置需求。

nginx行动:示例和现实应用程序nginx行动:示例和现实应用程序Apr 17, 2025 am 12:18 AM

NGINX可用于提升网站性能、安全性和可扩展性。1)作为反向代理和负载均衡器,NGINX可优化后端服务和分担流量。2)通过事件驱动和异步架构,NGINX高效处理高并发连接。3)配置文件允许灵活定义规则,如静态文件服务和负载均衡。4)优化建议包括启用Gzip压缩、使用缓存和调整worker进程。

NGINX单元:支持不同的编程语言NGINX单元:支持不同的编程语言Apr 16, 2025 am 12:15 AM

NGINXUnit支持多种编程语言,通过模块化设计实现。1.加载语言模块:根据配置文件加载相应模块。2.应用启动:调用语言运行时执行应用代码。3.请求处理:将请求转发给应用实例。4.响应返回:将处理后的响应返回给客户端。

在Nginx和Apache之间进行选择:适合您的需求在Nginx和Apache之间进行选择:适合您的需求Apr 15, 2025 am 12:04 AM

NGINX和Apache各有优劣,适合不同场景。1.NGINX适合高并发和低资源消耗场景。2.Apache适合需要复杂配置和丰富模块的场景。通过比较它们的核心特性、性能差异和最佳实践,可以帮助你选择最适合需求的服务器软件。

nginx怎么启动nginx怎么启动Apr 14, 2025 pm 01:06 PM

问题:如何启动 Nginx?答案:安装 Nginx启动 Nginx验证 Nginx 是否已启动探索其他启动选项自动启动 Nginx

怎么查看nginx是否启动怎么查看nginx是否启动Apr 14, 2025 pm 01:03 PM

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎么关闭nginx怎么关闭Apr 14, 2025 pm 01:00 PM

要关闭 Nginx 服务,请按以下步骤操作:确定安装类型:Red Hat/CentOS(systemctl status nginx)或 Debian/Ubuntu(service nginx status)停止服务:Red Hat/CentOS(systemctl stop nginx)或 Debian/Ubuntu(service nginx stop)禁用自动启动(可选):Red Hat/CentOS(systemctl disable nginx)或 Debian/Ubuntu(syst

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具