docker三剑客:docker-compose做负载均衡
程序员文章站
2022-07-01 11:26:28
...
docker-compose
一、认识compose
二、compose实践
- 使用 docker-compose做负载均衡集群
docker-compose官方文档https://docs.docker.com/compose/
[aaa@qq.com ~]#mkdir compose
[aaa@qq.com ~]#cd compose/
[aaa@qq.com ~/compose]# touch docker-compose.yml #使用 docker-compose做负载均衡集群
[aaa@qq.com ~/compose]#cat docker-compose.yml
version: "3.7"
services:
web1:
image: nginx
volumes:
- ./web1:/usr/share/nginx/html
networks:
- haproxy-net
web2:
image: nginx
volumes:
- ./web2:/usr/share/nginx/html
networks:
- haproxy-net
haproxy:
image: haproxy
volumes:
- ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
ports:
- "80:80"
networks:
- haproxy-net
networks:
haproxy-net:
[aaa@qq.com ~/compose]#mkdir web1
[aaa@qq.com ~/compose]#mkdir web2
[aaa@qq.com ~/compose]#mkdir haproxy
[aaa@qq.com ~/compose]#cd haproxy/
[aaa@qq.com ~/compose/haproxy]#yum install haproxy
[aaa@qq.com ~/compose/haproxy]#mv /etc/haproxy/haproxy.cfg .
[aaa@qq.com ~/compose/haproxy]#ls
haproxy.cfg
[aaa@qq.com ~/compose/haproxy]#vim haproxy.cfg
log 127.0.0.1 local2
pidfile /var/run/haproxy.pid
maxconn 4000
daemon
# turn on stats unix socket
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
stats uri /status
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
bind *:80
default_backend app
backend app
balance roundrobin
server app1 web1:80 check
server app2 web2:80 check
[aaa@qq.com ~/compose/haproxy]#docker-compose up
在web1和web2中写入index.html
[aaa@qq.com ~/compose]#cat web1/index.html
hello web1!!!
[aaa@qq.com ~/compose]#cat web2/index.html
hello web2
[aaa@qq.com ~/compose]#docker-compose start
Starting web1 ... done
Starting web2 ... done
Starting haproxy ... done
测试:
2. 共享卷
[aaa@qq.com ~/compose]#docker-compose stop
Stopping compose_haproxy_1 ... done
Stopping compose_web1_1 ... done
Stopping compose_web2_1 ... done
[aaa@qq.com ~/compose]#cat docker-compose.yml
version: "3.7"
services:
web1:
image: nginx
volumes:
- webdata:/usr/share/nginx/html
networks:
- haproxy-net
web2:
image: nginx
volumes:
- webdata:/usr/share/nginx/html
networks:
- haproxy-net
haproxy:
image: haproxy
volumes:
- ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
ports:
- "80:80"
networks:
- haproxy-net
networks:
haproxy-net:
volumes:
webdata:
[aaa@qq.com ~/compose]#docker-compose up -d # 打入后台
[aaa@qq.com ~/compose]#docker-compose logs # 查看日志
测试:
健康检查:停掉web2
[aaa@qq.com ~/compose]#docker stop compose_web2_1
compose_web2_1
[aaa@qq.com ~/compose]#docker start compose_web2_1
compose_web2_1