From 04631800f29af06d44f214f14e6174f32e7f5abd Mon Sep 17 00:00:00 2001 From: Christopher Winslett Date: Mon, 16 Mar 2015 15:46:09 -0700 Subject: [PATCH] add a haproxy router for a single connection point --- README.md | 8 ++++++++ haproxy.cfg | 21 +++++++++++++++++++++ haproxy_status.sh | 16 ++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 haproxy.cfg create mode 100755 haproxy_status.sh diff --git a/README.md b/README.md index 95edeb83..697ac050 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,14 @@ From there, you will see a high-availability cluster start up. Test different settings in the YAML files to see how behavior changes. Kill some of the different components to see how the system behaves. +To get a haproxy load balancing between these two hosts, run: + +``` +> haproxy -f haproxy.cfg +> sh haproxy_status.sh 127.0.0.1 5432 15432 +> sh haproxy_status.sh 127.0.0.1 5433 15433 +``` + # Requirements on a Mac Run the following on a Mac to install requirements: diff --git a/haproxy.cfg b/haproxy.cfg new file mode 100644 index 00000000..2f794acd --- /dev/null +++ b/haproxy.cfg @@ -0,0 +1,21 @@ +global + maxconn 100 + +defaults + log global + mode tcp + retries 2 + timeout client 30m + timeout connect 4s + timeout server 30m + timeout check 5s + +frontend ft_postgresql + bind *:5000 + default_backend bk_db + +backend bk_db + option httpchk GET + + server postgresql_127.0.0.1_5432 127.0.0.1:5432 maxconn 100 check port 15432 + server postgresql_127.0.0.1_5433 127.0.0.1:5433 maxconn 100 check port 15433 diff --git a/haproxy_status.sh b/haproxy_status.sh new file mode 100755 index 00000000..570af9d6 --- /dev/null +++ b/haproxy_status.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +while true +do { + response=$(echo "SELECT pg_is_in_recovery();" | psql -t postgres --port $2 --host $1 2> /dev/null) + + if [ "${response}" == " f" ] + then + echo "HTTP/1.1 200 OK" + echo "X-XLOG-POSITION: $(echo "SELECT pg_current_xlog_location();" | psql -t postgres 2> /dev/null | tr -d ' ' | head -n 1)" + else + echo "HTTP/1.1 503 Service unavailable" + echo "X-XLOG-POSITION: $(echo "SELECT pg_last_xlog_replay_location();" | psql -t postgres 2> /dev/null | tr -d ' ' | head -n 1)" + fi + +} | nc -l $3; done