├── .gitignore ├── Dockerfile ├── README.md ├── haproxy.cfg └── restart.bash /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.swo 3 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM dockerfile/haproxy 2 | MAINTAINER Andrea Reginato 3 | 4 | VOLUME /certs:/certs 5 | 6 | # Add personalized configuration 7 | ADD haproxy.cfg /etc/haproxy/haproxy.cfg 8 | 9 | # Add restart commands 10 | ADD restart.bash /haproxy-restart 11 | 12 | # Define working directory. 13 | WORKDIR /etc/haproxy 14 | 15 | # Define default command. 16 | CMD ["bash", "/haproxy-start"] 17 | 18 | # Expose ports. 19 | EXPOSE 80 20 | EXPOSE 443 21 | EXPOSE 1883 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MQTT HAProxy (docker container) 2 | 3 | Fast and reliable solution (based on HAProxy) offering high availability, load balancing and proxying for MQTT. 4 | 5 | ## Installation 6 | 7 | * [How to Build an High Availability MQTT Cluster for the Internet of Things](https://medium.com/@lelylan/how-to-build-an-high-availability-mqtt-cluster-for-the-internet-of-things-8011a06bd000#.vc1pdedpm) 8 | 9 | ## Resources 10 | 11 | * [Lelylan Dev Center](http://dev.lelylan.com) 12 | 13 | 14 | ## Contributing 15 | 16 | Fork the repo on github and send a pull requests with topic branches. 17 | 18 | 19 | ## Feedback 20 | 21 | Use the [issue tracker](http://github.com/lelylan/haproxy-mqtt/issues) for bugs or [stack overflow](http://stackoverflow.com/questions/tagged/lelylan) for questions. 22 | [Mail](mailto:dev@lelylan.com) or [Tweet](http://twitter.com/lelylan) us for any idea that can improve the project. 23 | 24 | 25 | ## Links 26 | 27 | * [GIT Repository](http://github.com/lelylan/haproxy-mqtt) 28 | * [Lelylan Dev Center](http://dev.lelylan.com) 29 | * [Lelylan Site](http://lelylan.com) 30 | 31 | 32 | ## Authors 33 | 34 | [Andrea Reginato](https://www.linkedin.com/in/andreareginato) 35 | 36 | 37 | ## Contributors 38 | 39 | Special thanks to [all people](https://github.com/lelylan/haproxy-mqtt/graphs/contributors) helping to make the project real. 40 | 41 | 42 | ## Changelog 43 | 44 | See [CHANGELOG](https://github.com/lelylan/haproxy-mqtt/blob/master/CHANGELOG.md) 45 | 46 | 47 | ## License 48 | 49 | Lelylan is licensed under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0). 50 | -------------------------------------------------------------------------------- /haproxy.cfg: -------------------------------------------------------------------------------- 1 | global 2 | ulimit-n 99999 3 | maxconn 99999 4 | maxpipes 99999 5 | tune.maxaccept 500 6 | log 127.0.0.1 local0 7 | log 127.0.0.1 local1 notice 8 | chroot /var/lib/haproxy 9 | user haproxy 10 | group haproxy 11 | 12 | defaults 13 | log global 14 | mode http 15 | option dontlognull 16 | timeout connect 5000ms 17 | timeout client 50000ms 18 | timeout server 50000ms 19 | errorfile 400 /etc/haproxy/errors/400.http 20 | errorfile 403 /etc/haproxy/errors/403.http 21 | errorfile 408 /etc/haproxy/errors/408.http 22 | errorfile 500 /etc/haproxy/errors/500.http 23 | errorfile 502 /etc/haproxy/errors/502.http 24 | errorfile 503 /etc/haproxy/errors/503.http 25 | errorfile 504 /etc/haproxy/errors/504.http 26 | 27 | listen stats :80 28 | stats enable 29 | stats uri / # must be present to see the logs 30 | stats auth admin:admin 31 | 32 | listen mqtt 33 | bind *:1883 34 | bind *:8883 ssl crt /certs/lelylan-mqtt.pem 35 | mode tcp 36 | #Use this to avoid the connection loss when client subscribed for a topic and its idle for sometime 37 | option clitcpka # For TCP keep-alive 38 | timeout client 3h #By default TCP keep-alive interval is 2hours in OS kernal, 'cat /proc/sys/net/ipv4/tcp_keepalive_time' 39 | timeout server 3h #By default TCP keep-alive interval is 2hours in OS kernal 40 | option tcplog 41 | balance leastconn 42 | server mosca_1 178.62.122.204:1883 check 43 | server mosca_2 178.62.104.172:1883 check 44 | -------------------------------------------------------------------------------- /restart.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 4 | # restart.bash 5 | # 6 | 7 | HAPROXY="/etc/haproxy" 8 | OVERRIDE="/haproxy-override" 9 | PIDFILE="/var/run/haproxy.pid" 10 | 11 | CONFIG="haproxy.cfg" 12 | ERRORS="errors" 13 | 14 | cd "$HAPROXY" 15 | 16 | # Symlink errors directory 17 | if [[ -d "$OVERRIDE/$ERRORS" ]]; then 18 | mkdir -p "$OVERRIDE/$ERRORS" 19 | rm -fr "$ERRORS" 20 | ln -s "$OVERRIDE/$ERRORS" "$ERRORS" 21 | fi 22 | 23 | # Symlink config file. 24 | if [[ -f "$OVERRIDE/$CONFIG" ]]; then 25 | rm -f "$CONFIG" 26 | ln -s "$OVERRIDE/$CONFIG" "$CONFIG" 27 | fi 28 | 29 | exec haproxy -f /etc/haproxy/haproxy.cfg -D -p "$PIDFILE" 30 | 31 | --------------------------------------------------------------------------------