本文共 1624 字,大约阅读时间需要 5 分钟。
yum -y install python-devel
pip install docker-compose
编写docker-compose.yml文件
cd /opt
vim docker-compose.yum
web1: #ID image: nginx #使用镜像 volumes: - /opt/index1.html:/usr/share/nginx/html/index.html #把宿主机/opt/index1.html文件挂载到容器/usr/share/nginx/html/index.html expose: #容器内使用端口 - 80web2: image: nginx volumes: - /opt/index2.html:/usr/share/nginx/html/index.html expose: - 80haproxy: image: haproxy volumes: - /opt/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg links: #依赖于web1,web2只有这两个容器正常启动 haproxy才会启动 - web1 - web2 ports: - "7777:1080" #端口映射,宿主机7777端口映射容器1080端口 宿主机80端口映射容器80端口 - "80:80"
把haproxy.cfg文件和index1,index2文件放入/opt中
haproxy.cfg
global log 127.0.0.1 local0 log 127.0.0.1 local1 notice maxconn 4096defaults log global mode http option httplog option dontlognull timeout connect 5000ms timeout client 5000ms timeout server 5000mslisten stats bind 0.0.0.0:1080 mode http stats enable stats hide-version stats uri /status stats auth admin:adminfrontend balance bind 0.0.0.0:80 default_backend web_backendsbackend web_backends mode http option forwardfor balance roundrobin server web1 web1:80 check server web2 web2:80 check
index1.html
This is linux node1
index2.html
This is linux node2
启动docker-compose
docker-compose -f /opt/docker-compose.yml up
关闭docker-compose
docker-compose -f /opt/docker-compose.yml stop
这样只是停掉容器,并不会删除
docker-compose -f /opt/docker-compose.yml down
这样关闭会把容器删除掉
浏览器测试负载
查看haproxy状态
转载地址:http://amepb.baihongyu.com/