基于容器的负载均衡
类别: Docker 标签: HAProxy 负载均衡目录
HAProxy
编写 HAProxy 配置文件
nano haproxy.cfg
global
log 127.0.0.1 local0
maxconn 4096
chroot /usr/local/sbin
daemon
nbproc 4
pidfile /usr/local/sbin/haproxy.pid
defaults
log global
mode http
option dontlognull
option redispatch
retries 2
maxconn 2000
balance roundrobin
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend haproxynode
bind *:8000
default_backend backendnodes
backend backendnodes
bind-process 2
balance roundrobin
option forwardfor
option httpchk
server APP1 APP1:80 check inter 2000 rise 2 fall 5
server APP2 APP2:80 check inter 2000 rise 2 fall 5
stats enable
stats uri /admin?stats
运行 hello-world 容器,生成两个实例。
docker run --rm -p 8001:80 --name APP1 --env NAME=APP1 vwarship/hello-world:latest
docker run --rm -p 8002:80 --name APP2 --env NAME=APP2 vwarship/hello-world:latest
运行 HAProxy 容器,负载均衡生效。
docker run --rm -d \
--link APP1:APP1 \
--link APP2:APP2 \
-p 8000:8000 \
-v haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg \
--name haproxy \
haproxy:latest
查看
参考资料
- vwarship/hello-world
- How to Use HAProxy for Load Balancing
- Hitless Reloads with HAProxy – HOWTO
- HAProxy Management Guide
- HAProxy Configuration Manual
- Docker Documentation
- haproxy.cfg
- Layer4 “Connection refused” with haproxy
- KubeCon EU 2016: Creating an Advanced Load Balancing Solution for Kubernetes with NGINX