Dealing with Docker Swarm will be easier if we are using portainer. The UI is very intuitive and easy to use.
Now we will focus at container level how to run HA Proxy and do forwarding to Nginx.
HA Proxy preparation:
- click Containers from menu
- Enter container name
- under image enter haproxy:latest
- network port
i deploy 2 ports:
port 8080 for http connection
port 8888 for HA Proxy stats
- Advance Container Settings
Under Volume select Bind instead of Volume
we need to have haproxy.cfg as main configuration for HA Proxy.
I do mapping from /home/ubuntu/docker/haproxy in host to /usr/local/etc/haproxy on container.
below is the snippet of haproxy.cfg
global log stdout format raw local0 stats socket /var/lib/haproxy/stats 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 frontend main bind *:8080 default_backend web01 http-request capture req.hdr(Host) len 64 http-request capture req.hdr(Referer) len 64 http-request capture req.hdr(Content-Lenght) len 64 http-request capture req.hdr(User-Agent) len 64 backend web01 server backend01 172.17.0.4:80 check server backend02 172.17.0.3:80 check listen stats bind *:8888 stats enable stats hide-version stats refresh 30s stats show-node stats auth rproxy:P@ssw0rd stats uri /stats
- Under Action click Deploy Container
- Â haproxy container created
Nginx container
In this scenario i will create 2 nginx container named nginx1 and nginx2
- add new container
- under network port create port mapping between host and container
- Click Deploy container
Repeat the step for nginx2, following container to be created
Now if we access the port 8080 on haproxy, request will be forwarded to both nginx.
if nginx1 or nginx2 is down, access to port 8080 is still working.