乐趣区

13使用Docker-Compose-实现nginx负载均衡

以 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 地址

官网文档地址:https://docs.docker.com/compo…

继续编写上一篇《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 文件中进行变量定义

官方文档地址:https://docs.docker.com/compo…

// 还是在 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,测试服务器负载均衡!

注意:上一篇已经在两台 httpd 服务器上放置了不同的 web 文件

原文链接:http://www.mi360.cn/articles/20

退出移动版