docker-compose实现nginx负载均衡

1、环境

Ubuntu16.04,已经安装了docker、docker-compose。执行docker-compose -h查询:

2、编写yml文件

在当前目录下创建composetest目录,进入创建compose.yml文件,并创建web1,web2两个文件夹用于挂在到容器,haproxy防止代理配置,文件结构如下: 

1、docker-compose.yml文件如下:

web1:
        image: nginx
        expose:
                - 80
        volumes:
                - ./web1:/usr/share/nginx/html

web2:
        image: nginx
        expose:
                - 80
        volumes:
                - ./web2:/usr/share/nginx/html

haproxy:
        image: haproxy
        volumes:
                - ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
        links:
                - web1
                - web2
        ports:
                - "80:80"
  • web1、web2为nginx服务,haproxy为负载均衡服务。
  • 当前目录得web1、web2挂在到nginx容器,负载均衡配置文件挂在到haproxy容器
  • web1、web2暴漏80端口给haproxy服务使用,haproxy映射容器端口到宿主机80端口

2、haproxy.cfg配置文件如下:

global
        log 127.0.0.1 local0
        log 127.0.0.1 local1 notice

defaults
        log global
        mode http
        option httplog
        option dontlognull
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms
        stats uri /status

frontend balancer
        bind 0.0.0.0:80
        default_backend web_backends

backend web_backends
        balance roundrobin
        server server1 web1:80 check
        server server2 web2:80 check

3、启动并测试

1、主动docker-compose up启动服务栈,控制台打印所有容器的日志:

root@ubuntu:~/composetest# docker-compose up
Pulling web1 (nginx:)...
latest: Pulling from library/nginx
6ec8c9369e08: Pulling fs layer
d3cb09a117e5: Pulling fs layer
7ef2f1459687: Downloading [===================================>               ] d3cb09a117e5: Downloading [>                                                  ] d3cb09a117e5: Downloading [=>                                                 ] d3cb09a117e5: Downloading [==>                                                ] d3cb09a117e5: Downloading [===>                                               ] 6ec8c9369e08: Downloading [>                                                  ]  281.9kB/27.1MBB
6ec8c9369e08: Downloading [=>                                                 ] 6ec8c9369e08: Downloading [=>                                                 ]  846.3kB/27.1MB


d3cb09a117e5: Downloading [=======>                                           ] 6ec8c9369e08: Pull complete
d3cb09a117e5: Pull complete
7ef2f1459687: Pull complete
e4d1bf8c9482: Pull complete
795301d236d7: Pull complete
Digest: sha256:0e188877aa60537d1a1c6484b8c3929cfe09988145327ee47e8e91ddf6f76f5c
Status: Downloaded newer image for nginx:latest
Pulling haproxy (haproxy:)...
latest: Pulling from library/haproxy
6ec8c9369e08: Already exists
f1677bc71036: Pull complete
0eae8ba1a6bd: Pull complete
Digest: sha256:d773b81fb6376380c5dfa78fb70a1f3d15869d18a7afb455a87bf5f609a7c160
Status: Downloaded newer image for haproxy:latest
Creating composetest_web1_1 ... done
Creating composetest_web2_1 ... done
Creating composetest_haproxy_1 ... done
Attaching to composetest_web2_1, composetest_web1_1, composetest_haproxy_1
web1_1     | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
web1_1     | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
web1_1     | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
web1_1     | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
web1_1     | 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
web1_1     | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
web1_1     | /docker-entrypoint.sh: Configuration complete; ready for start up
web2_1     | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
web2_1     | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
web2_1     | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
web2_1     | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
web2_1     | 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
web2_1     | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
web2_1     | /docker-entrypoint.sh: Configuration complete; ready for start up
haproxy_1  | [NOTICE] 207/041918 (1) : New worker #1 (6) forked

2、执行docker ps查看容器

3、测试

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值