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、测试