Wondering how to use docker swarm haproxy? Our experts have put together this guide to help you get started. Our Docker Support team is here to lend a hand with your queries and issues.
How to use docker swarm haproxy?
Use HAProxy to add routing, load balancing, and DNS service discovery to Docker Swarm
Today, let us see the steps followed by our support techs to setup swarm haproxy.
HAProxy Configuration
Prior to creating your HAProxy service in Swarm, prepare your configuration file.
Please note that you have to store this file at the same path on all of your cluster nodes that could run your HAProxy container. We use /etc/haproxy/haproxy.cfg.
Firstly, create the file and add a
global
section to the top:
global log fd@2 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy stats socket /var/lib/haproxy/stats expose-fd listeners master-worker
Here, there are two important settings.
- The first is the
line. This will provide the logs to Docker, which can process them depending on the log driver you’ve set.log fd@2 local2
- The second is the
line: It allows you to reload the HAProxy configuration without needing to restart the container itself.master-worker
Next, add a
resolvers
section:
resolvers docker nameserver dns1 127.0.0.11:53 resolve_retries 3 timeout resolve 1s timeout retry 1s hold other 10s hold refused 10s hold nx 10s hold timeout 10s hold valid 10s hold obsolete 10s
The Docker Swarm DNS service is always available at 127.0.0.11. So, this section configures
HAProxy to direct DNS queries to there. This is essential for DNS-based service discovery.
Next, add a
defaults
section so that there are some sensible timeouts and other settings:
defaults timeout connect 10s timeout client 30s timeout server 30s log global mode http option httplog
Finally, finish with some
frontend
and backend
sections:
frontend fe_web bind *:80 use_backend stat if { path -i /my-stats } default_backend be_apache_service backend be_apache_service balance roundrobin server-template apache- 6 apache-Service:80 check resolvers docker init-addr libc,none backend be_apache_service_wrong_case balance roundrobin server-template apache- 6 apache-service:80 check resolvers docker init-addr libc,none backend stat stats enable stats uri /my-stats stats refresh 15s stats show-legends stats show-node
This configuration is very basic, but take a closer look at the
server-template
lines in the backend
sections. A server-template
generates server
lines.
Create an HAProxy Service
At this point, you have:
- an Apache service defined, but not yet running on any nodes
- an HAProxy configuration file that’s been copied to each node into the folder /etc/haproxy/haproxy.cfg
Now you can create the HAProxy service in Swarm. There are three ways to do this:
- Firstly, create only one HAProxy container and let Swarm’s ingress routing mesh forward clients’ requests to it.
- Then, create only one HAProxy container, but don’t use the ingress routing mesh; HAProxy will receive clients’ requests directly.
- Finally, create a replica of HAProxy on each node; Each will receive clients’ requests directly.
Placement Constraints
A quick word about placement constraints: You may, as an example, have a cluster of 20 nodes and you would like to control where your HAProxy containers could be started when using
--mode replicated
.
In this case, you should use placement constraints.
They allow you to restrict which nodes Swarm can choose to run your containers.
Let’s say you would like to run HAProxy only on nodes 2 and 3; Run these commands on any manager:
$ sudo docker node update –label-add LB-NODE=yes dock2 $ sudo docker node update –label-add LB-NODE=yes dock3
Then, specify the constraint at service creation:
$ sudo docker service create \ –mode global \ –name haproxy-service \ –network apache-network \ –publish published=80,target=80,protocol=tcp,mode=host \ –publish published=443,target=443,protocol=tcp,mode=host \ –mount type=bind,src=/etc/haproxy/,dst=/etc/haproxy/,ro=false \ –dns=127.0.0.11 \ –constraint node.labels.LB-NODE==yes \ haproxytech/haproxy-debian:2.0 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -L local_haproxy
Or on an existing service with the docker service update command:
$ docker service update –constraint-add node.labels.LB-NODE==yes haproxy-service
The container will only started on nodes with a matching label.
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
To conclude, our Support Engineers demonstrated how to how to use docker swarm haproxy.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
0 Comments