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: #容器内使用端口
- 80
web2:
image: nginx
volumes:
- /opt/index2.html:/usr/share/nginx/html/index.html
expose:
- 80
haproxy:
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 4096
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000ms
timeout client 5000ms
timeout server 5000ms
listen stats
bind 0.0.0.0:1080
mode http
stats enable
stats hide-version
stats uri /status
stats auth admin:admin
frontend balance
bind 0.0.0.0:80
default_backend web_backends
backend web_backends
mode http
option forwardfor
balance roundrobin
server web1 web1:80 check
server web2 web2:80 check
index1.html
<h1>This is linux node1</h1>
index2.html
<h1>This is linux node2</h1>
启动docker-compose
docker-compose -f /opt/docker-compose.yml up
- -f 指定配置文件
- up 启动
- 生成环境中加 -d 后台运行
关闭docker-compose
docker-compose -f /opt/docker-compose.yml stop
这样只是停掉容器,并不会删除
docker-compose -f /opt/docker-compose.yml down
这样关闭会把容器删除掉
浏览器测试负载
查看haproxy状态