获取docker-compose,并添加该命令至可执行目录
mv docker-compose-Linux-x86_64-1.22.0 /usr/local/bin/docker-compose
chmod 755 /usr/local/bin/docker-compose
获取基础镜像haproxy.tar、nginx.tar并加载
docker load -i nginx.tar
docker load -i haproxy.tar
创建目录,编写负载均衡调度策略
mkdir -p /mnt/compose
cd /mnt/compose/
vim docker-compose.yml
文件编辑内容如下:
//网页web1及其相应的挂载点及其暴露端口
web1:
image: nginx:latest
expose:
- 80
volumes:
- ./web1:/usr/share/nginx/html
//web2同web1
web2:
image: nginx:latest
expose:
- 80
volumes:
- ./web2:/usr/share/nginx/html
//添加haproxy
haproxy:
image: haproxy:latest
volumes:
- ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
links:
- web1
- web2
ports:
- "80:80"
expose:
- 80
创建web1、web2网页主文件
mkdir web1 web2
echo web1 >web1/index.html
echo web2 >web2/index.html
创建haproxy目录,添加haproxy的配置文件(Version 2)
mkdir haproxy
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
启动
docker-compose up -d
测试
curl 172.25.33.2
关闭web1进行测试
docker container stop compose_web1_1
测试
curl 172.25.33.2