Uncover the infinite in IT

Categories
Table of Contents
< All Topics

HAProxy Config Sync & Reload Automation

HAProxy Config Sync is essential for high availability and consistent traffic routing. This guide covers setting up an automated system using inotifywait, rsync, and systemd.

Why Automate HAProxy Config Sync?

Manual configuration synchronization between HAProxy nodes is prone to human error, delays, and inconsistencies. This can lead to unexpected issues, misconfigurations, or even service disruptions, especially in complex network environments. Automating the sync process eliminates these risks by ensuring that configuration changes are immediately propagated and consistently applied across all global nodes. This not only minimizes downtime but also prevents configuration drift, maintains load balancing efficiency, and enhances overall system reliability. Additionally, automation reduces administrative overhead, allows for easier scaling of infrastructure, improves security by maintaining consistent rules and access controls, and ensures seamless failover operations across geographically distributed nodes.

Prerequisites for HAProxy Config Sync

Before starting, ensure the following are in place:

  • Master and Slave HAProxy nodes with SSH key-based authentication configured
  • inotify-tools, rsync, and systemd installed on both nodes

Setting up Automated HAProxy Config Sync

Install Required Packages:

sudo apt update
sudo apt install inotify-tools rsync

Configure SSH Key-Based Authentication: Ensure passwordless SSH between the master and slave nodes:

ssh-keygen -t ed25519
ssh-copy-id user@slave-node-ip

Create Sync Script on Master Node:

#!/bin/bash
inotifywait -m /etc/haproxy/haproxy.cfg -e close_write | while read events; do
  rsync -avz /etc/haproxy/haproxy.cfg user@slave-node-ip:/etc/haproxy/haproxy.cfg
  ssh user@slave-node-ip "sudo systemctl reload haproxy"
done

Create systemd Service:

[Unit]
Description=Automated HAProxy Config Sync

[Service]
ExecStart=/path/to/sync-script.sh
Restart=always

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable haproxy-sync.service
sudo systemctl start haproxy-sync.service

Best Practices

  • Test the sync and reload process in a staging environment before production deployment.
  • Implement monitoring to detect sync failures or HAProxy reload errors.
  • Secure SSH communication with restricted keys and firewall rules.

Troubleshooting

  • Check journalctl -u haproxy-sync.service for service logs.
  • Verify network connectivity and SSH access between nodes.

Conclusion

This tutorial ensures consistent and efficient load balancing across nodes. This reduces downtime and administrative overhead while maintaining high availability.

Additional Resources

Related Articles

This guide provides a robust solution for automated HAProxy config synchronization and reloads. Implementing it enhances reliability and consistency in your network infrastructure.

RSS
Pinterest
fb-share-icon
LinkedIn
Share
WhatsApp
Copy link
URL has been copied successfully!