I have haproxy running as my load-balancer and from the stats web interface that comes with haproxy, I can put a web server into maintenance mode (and bring it back out again) - which is great!
However, I also want to be able to perform that same action from the command line (for use in some automated deployment workflows). Is this possible, and if so how?
Many thanks
Update (28 Aug 2012): I now tend to use haproxyctl nowadays, which utilizes the methods described below.
I've fixed it after a little more research, for anyone else with the same issue:-
You can add a unix socket in your config, then interact with that (here are the possible commands).
To set up:
In your 'global' section add in:
Restart your haproxy daemon (e.g.
sudo service haproxy restart
)Now you need socat (if you don't have it, just
apt-get install socat
on Ubuntu).Now all you need to do is fire off this command to take down a node:
To bring it back up replace disable with enable in the command above.
In addition to beardwizzle's echo method, you can also do this interactively:
On the off chance that you only have access to netcat (
nc
) you can use it to interact with HAProxy's socket file in a similar fashion tosocat
.To disable a server:
Take care to make sure that the socket file has the appropriate level of access to perform the above. Mainly something like this:
Otherwise you'll get permission denied errors:
References
The easy way is:
1 - Configure your web server to return 503 code if a file named maintenance.html (for example) exists. With apache you can do it as follows:
2 - Configure your haproxy backend to check an URL instead of only checking a port as follows:
3 - Restart your webserver and load balancer.
4 - Put your web server in maintenance mode.
5 - Remove your web server from maintenance mode.
You can also temporarily "remove" the health check page from one server, to get the endpoint down and then publish your application.
In HAProxy 1.8 you can also use set server state:
put the server in maintenance (or drain) mode:
put the server back to ready mode:
If you have debian, readline does not work, netcat is a better option for interactive:
Non Interactive: